我正在使用 aQute Bnd 工具集创建 OSGi 捆绑包,并打包了一些依赖的“资源”文件。这包括我创建的资源目录中的 *.css 文件和 *.xsd 文件。
我已将以下内容包含在捆绑包.bnd file:
Include-Resource: resources/=resources/
当我进行构建时,生成的 *.jar 文件在 jar 包文件顶部目录的 resources 目录中包含 *.css 和 *.xsd 文件。
然而,在实际代码中,我很难尝试将其引用为我的类路径的一部分:
我已经尝试过以下方法:
new File("resources/example.css");
我也尝试过:
URL cssFile = this.getClass().getResource("resources/example.css");
try
{
file = new File(cssFile.toURI()));
}
catch(Exception e)
{
e.printStackTrace();
}
我要么收到 NullPointException 错误,要么收到 File can not be find IOException 错误(具体取决于我使用哪一个)。在调试配置模式下的 Eclipse Equinox 以及 Apache Felix(我们用于部署)中运行时,我收到此错误。注意我试图在 BundleActivator 之外的 Java 类中执行此操作。
我是否需要始终参考 BundleActivator 的上下文,例如?
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
@Override
public void start(BundleContext context) throws Exception
{
/* Service to host the Bundle interface. */
ServletContainerService service = new ServletContainerService();
service.addServlet(new ServletContainer(new AxisServlet(), true));
this.serverReg = context.registerService(ServletContainerService.class.getName(), service, null);
cssFile = new File(context.getClass.getResource("resource/example.css"));
}
我认为上面的方法可行,但意味着我必须传递 cssFile 引用,这看起来并不优雅。
有没有什么方法可以引用作为bundle/.jar 文件一部分的任何给定Java 类中包含在bundle jar 文件中的“resources”目录的路径?如果涉及到BundleContext,有没有办法在任何Java类中引用它?
任何帮助都感激不尽。
我已经看过并且包含 OSGi 捆绑包中的附加资源但看来您需要 BundleContext。
我可能已经找到了一个可能的解决方案:http://www.vogella.de/blog/tag/plugin/
看起来 Vogella 有一些示例代码:
URL url;
try {
url = new URL("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt");
InputStream inputStream = url.openConnection().getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
有谁知道如果它不是插件并且我使用不同的 OSGi 环境(例如),该路径是否相同。 Equinox Eclipse 与 Apache Felix?
例如url = new URL("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt");