从 OSGi 包导入资源
利用 OSGi 中的导入机制,可以直接从另一个包导入包。但是,我未能成功导入捆绑包“根”中存在的资源。
是否可以将不在包范围内的资源导入到另一个包中?
我想要实现的是:
捆绑包 A 在“根”中有一个文件资源
捆绑包 B 导入捆绑包 A:s 包和资源。 通过bundle B:s ClassLoader,我希望能够加载bundle A中的资源,就像它存在于Bundle B中一样。
With the import mechanism in OSGi, it is straightforward to import packages from another bundle. However, I have been unsuccessful in importing resources that exist in the "root" of the bundle.
Is it at all possible to import resources that aren't package scoped in to another bundle?
What I would like to achieve is this:
Bundle A has a file resource in the "root"
Bundle B imports bundle A:s packages and resources.
Through bundle B:s ClassLoader, I'd like to be able to load the resource in bundle A as if it existed in Bundle B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
捆绑包根目录中的资源位于“默认”包中,无法导入或导出。
如果您确实必须通过类加载器访问资源,则需要将它们移至包中并导出该包。否则,您可以使用
Bundle.getEntry()
从任何包的任何位置读取资源。Resources in the root of a bundle are in the "default" package, which cannot be imported or exported.
If you really must access the resources via classloader, you need to move them into a package and export that package. Otherwise you can use
Bundle.getEntry()
to read resources from any location of any bundle.您可以使用 OSGi Fragment 捆绑包。对于您的情况:捆绑包 B 是主机,捆绑包 A 是捆绑包 B 的片段。但是捆绑包 B 可以访问捆绑包 A 的所有类和资源(文件夹)。
更多详细信息请参阅 OSGi Core Spec #3.13 片段捆绑包
You can use OSGi Fragment bundles. For your case: bundle B is a host and bundle A is a fragment of the bundle B. But bundle B has access to all classes and resources (folders) of bundle A.
More details in OSGi Core Spec #3.13 Fragment bundles
创建一个新线程,然后创建一个指向所需文件的新类加载器。
看一下这个片段:
然后,线程类加载器将能够加载包内的文件,其中 URL 包含包的绝对位置。
Create a new thread and then create a new classloader that points to the files needed.
Look at this fragment:
The thread classloader will then be able load the files within the package where the URLs include the absolute location to the bundle.