简体   繁体   English

从OSGi包导入资源

[英]Importing resources from OSGi bundle

With the import mechanism in OSGi, it is straightforward to import packages from another bundle. 使用OSGi中的导入机制,可以直接从另一个包中导入包。 However, I have been unsuccessful in importing resources that exist in the "root" of the bundle. 但是,我没有成功导入存在于bundle的“root”中的资源。

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 A在“root”中有一个文件资源

Bundle B imports bundle A:s packages and resources. Bundle B导入bundle A:s包和资源。 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. 通过bundle B:s ClassLoader,我希望能够在bundle A中加载资源,就好像它存在于Bundle B中一样。

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. 否则,您可以使用Bundle.getEntry()从任何捆绑包的任何位置读取资源。

You can use OSGi Fragment bundles. 您可以使用OSGi Fragment包。 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. 对于您的情况:bundle B是一个主机,bundle A是bundle B的一个片段。但是bundle B可以访问bundle A的所有类和资源(文件夹)。

More details in OSGi Core Spec #3.13 Fragment bundles OSGi Core Spec#3.13 Fragment包中的更多细节

Create a new thread and then create a new classloader that points to the files needed. 创建一个新线程,然后创建一个指向所需文件的新类加载器。

Look at this fragment: 看看这个片段:

ClassLoader c = new URLClassLoader(urls);
thread.setContextClassLoader(c);

The thread classloader will then be able load the files within the package where the URLs include the absolute location to the bundle. 然后,线程类加载器将能够加载包中的文件,其中URL包括束的绝对位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM