在 OSGi 下使用 SnakeYaml?
SnakeYaml 是否在 OSGi 框架内工作?我修改了 MANIFEST &这样它就可以正确部署,但是尝试将文档加载到 JavaBean 对象结构中会失败,并出现“找不到类”异常。
谢谢。
Does SnakeYaml work within an OSGi framework? I've modified the MANIFEST & such so that it deploys correctly, but but trying to load a document into a JavaBean object structure is failing with "Class not found" exceptions.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有时,只需添加清单标头即可让 jar 在 OSGi 沙箱中正常运行。有时,jar/库会在 OSGi 环境中做一些“顽皮”的事情。由于 OSGi 使用类加载器的方式,一条黄金法则是避免使用“Class.forName()”,否则在单个类加载器环境中完全有效。我下载了 SnakeYaml 的源代码,它们是基于 Bean 的加载器,使用了 Class.forName。
好消息是,似乎有一个构造函数 CustomClassLoaderConstructor,它允许您使用自己的类加载器,并且在创建核心 Yaml 解析器对象时使用它。关键是获得正确的类加载器。您需要使用使用 Yaml 的包的类加载器,但您需要确保将创建的任何类都导入到该包中。导入将确保所需的所有对象都位于 OSGi 创建的类加载器树中。
查看这个问题,了解基于捆。
Sometimes it is as simple as adding manifest headers to make a jar play nice in the OSGi sandbox. Sometimes jars/libraries do "naughty" things in the context of OSGi. A golden rule is to avoid using "Class.forName()" due to the way OSGi uses classloaders, otherwise perfectly valid in a single class loader environment. I pulled down the source to SnakeYaml and they're bean based loader makes use of Class.forName.
The good news is that there appears to be a constructor, CustomClassLoaderConstructor, that let's you use your own classloader and you use this when you make the core Yaml parser object. The key is getting the right class loader. You'll want to use the classloader of the bundle in which you're using Yaml BUT you'll need to make sure than ANY CLASS that will be created is imported into that bundle. The import will make sure all of the objects needed are in the classloader tree that OSGi creates.
See this question for created a classloader based on a bundle.
对于任何偶然发现这一点的人来说,新版本的 snakeyaml 已经是 osgi 捆绑包。无需摆弄文件 MANIFEST.MF。
您必须只使用像这样的 CustomClassLoaderConstructor:
Code tested with org.yaml.snakeyaml;bundle-version="1.25.0"
For anyone that stumbles across this, newer versions of snakeyaml are already a osgi bundle. No need to fiddle at file MANIFEST.MF.
You must just use a CustomClassLoaderConstructor like this:
Code tested with org.yaml.snakeyaml;bundle-version="1.25.0"