her*_*yme 11 java osgi bundle classpath
我正在使用aQute Bnd工具集来创建一个OSGi包,并打包了一些相关的"资源"文件.这包括我创建的资源目录中的*.css文件和*.xsd文件.
我在bundle.bnd文件中包含以下内容:
Include-Resource: resources/=resources/
Run Code Online (Sandbox Code Playgroud)
当我进行构建时,生成的*.jar文件在jar包文件的顶级目录的resources目录中包含*.css和*.xsd文件.
但是,在实际代码中,我很难尝试将其作为类路径的一部分引用:
我尝试过以下方法:
new File("resources/example.css");
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
URL cssFile = this.getClass().getResource("resources/example.css");
try
{
file = new File(cssFile.toURI()));
}
catch(Exception e)
{
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我得到一个NullPointException错误或无法找到文件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"));
}
Run Code Online (Sandbox Code Playgroud)
我认为上面的内容会起作用,但意味着我将不得不传递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();
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道如果它不是一个插件,如果我使用不同的OSGi环境,这条路径是否相同,例如.Equinox Eclipse与Apache Felix?例如url = new URL("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt");
该捆绑接口具有getEntry(java.lang.String path)其返回一个地址和记录为方法:
返回此捆绑包中指定路径的条目的URL.此捆绑包的类加载器不用于搜索条目.仅搜索此包的内容以查找该条目.指定的路径始终相对于此捆绑包的根,可以以"/"开头.路径值"/"表示此捆绑包的根.
| 归档时间: |
|
| 查看次数: |
30728 次 |
| 最近记录: |