访问另一个 osgi 包中的资源?
我使用 eclipse 插件项目向导(使用 eclipse Helios)创建了两个 OSGI 包 A 和 B。
在bundle BI 的清单文件中添加了bundle A 作为依赖项。 我希望该文件对捆绑包 B 可见。在捆绑包 AI 的 build.properties 窗格中已指定:
source.. = src/
bin.includes = META-INF/,\
.,\
bundle_A.properties
此外,我已导出 A 中的包,以便它们对 B 可见。我在捆绑包 A 中还有一个 .properties 文件, 捆绑BI尝试使用以下方式加载.properties文件:
private Properties loadProperties() {
Properties properties = new Properties();
InputStream istream = this.getClass().getClassLoader().getResourceAsStream(
"bundle_A.properties");
try {
properties.load(istream);
} catch (IOException e) {
logger.error("Properties file not found!", e);
}
return properties;
}
但这会产生空指针异常(在类路径上找不到该文件)。
是否可以从捆绑包 A 导出资源(就像导出包一样)或以其他方式从 B 访问 A 中的文件(从捆绑包 B 访问捆绑包 A 的类加载器)?
I have created two OSGI bundles A and B using the eclipse Plug-in project wizard (using eclipse Helios).
In the manifest file of bundle B I have added bundle A as a dependency. Further I have exported the packages in A so they are visible for B. I also have a .properties file in bundle A that I would like to make visible for bundle B. In the build.properties pane in bundle A I have specified:
source.. = src/
bin.includes = META-INF/,\
.,\
bundle_A.properties
Now in bundle B I try to load the .properties file using:
private Properties loadProperties() {
Properties properties = new Properties();
InputStream istream = this.getClass().getClassLoader().getResourceAsStream(
"bundle_A.properties");
try {
properties.load(istream);
} catch (IOException e) {
logger.error("Properties file not found!", e);
}
return properties;
}
But that gives a nullpointer exception (the file is not found on the classpath).
Is it possible to export resources from bundle A (just like when you export packages) or somehow access the file in A from B in another way (accessing the classloader for bundle A from bundle B)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Bundle
上的getEntry(String)
方法就是用于此目的。您可以使用它从任何捆绑包中加载任何资源。如果您不知道包内资源的确切路径,另请参阅findEntries()
和getEntryPaths()
方法。不需要获取包的类加载器来执行此操作。
The
getEntry(String)
method onBundle
is intended for this purpose. You can use it to load any resource from any bundle. Also see the methodsfindEntries()
andgetEntryPaths()
if you don't know the exact path to the resource inside the bundle.There is no need to get hold of the bundle's classloader to do this.
如果您正在编写 Eclipse 插件,您可以尝试以下操作:
If you're writing an Eclipse plug-in you could try something like:
您是否考虑过向捆绑包 A 的 API 添加一个加载和返回资源的方法?
许多人可能认为这是一个更好的设计,因为它允许更改资源的名称或存储方式,而不会破坏捆绑包 A 的客户端。
Have you considered adding a method to bundle A's API that loads and returns the resource?
Many might consider this a better design as it allows the name or means of storage of the resource to change without breaking clients of bundle A.
您是否尝试过使用bundle A的BundleContext来加载资源?
Have you tried using the BundleContext of bundle A to load resources?
尝试使用
/
;如果您不放置/
,类加载器将尝试从同一个包中加载资源。Try with
/
; if you don't put/
, the class loader will try to load the resource from the same bundle.