如何在执行java.io.File或FileInputStream时引用OSGi包中的包含文件 [英] How to reference an included file in OSGi bundle when performing java.io.File or FileInputStream

查看:150
本文介绍了如何在执行java.io.File或FileInputStream时引用OSGi包中的包含文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用aQute Bnd工具集来创建一个OSGi包,并打包了一些相关的资源文件。这包括我创建的资源目录中的* .css文件和* .xsd文件。

I am using the aQute Bnd toolset to create an OSGi bundle and have packaged with some dependant 'resource' files. This includes *.css files and *.xsd files in a resources directory I have created.

我在 bundle.bnd中包含以下内容 file:

Include-Resource: resources/=resources/ 

当我进行构建时,生成的* .jar文件在jar包的top目录的resources目录中有* .css和* .xsd文件文件。

and when I do a build, the generated *.jar file has the *.css and *.xsd files in the resources directory in the top directory of the jar bundle file.

然而,在实际代码中,我很难尝试将其作为类路径的一部分引用:

However, in the actual code I am having difficulty in trying to refer to this as part of my class path:

我尝试过以下方法:

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错误或找不到文件IOException错误(取决于我使用哪一个)。我在调试配置模式下运行Eclipse Equinox以及Apache Felix(我们用于部署)时遇到此错误。注意我试图在BundleActivator之外的Java类中执行此操作。

I either get a NullPointException error or a File cannot be found IOException error (depending which one I use). I get this error when running in both Eclipse Equinox in Debug Configuration mode as well as Apache Felix (which we are using for our deployment). Note I am trying to do this in Java classes outside of the BundleActivator.

我是否需要始终引用BundleActivator的上下文,例如?

Do I need to always refer to the context of the BundleActivator e.g.?

 /*
 * (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引用似乎并不优雅。

I think the above will work, but will mean I will have to pass the cssFile reference around which does not appear to be elegant.

有没有办法引用包含在jar包文件中的'resources'目录的路径任何给定的Java类是bundle / .jar文件的一部分?如果它涉及BundleContext,有没有办法在任何Java类中引用它?

Is there any way to refer to the path of the 'resources' directory that is included in the bundle jar file in any given Java class that is part of the bundle/.jar file? If it involves the BundleContext, is there any way to reference this in any Java class?

任何帮助将不胜感激。

我看了一下在OSGi包中包含其他资源但似乎需要BundleContext。

I have had a look at and Including additional resources with OSGi bundles but it appears that you need the BundleContext.

我可能找到了一个可能的解决方案: http://www.vogella.de/blog/tag/plugin/

I might have found a possible solution to this: http://www.vogella.de/blog/tag/plugin/

看起来Vogella有一些示例代码:

Looks like Vogella has some example code for this:

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 =新网址(platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt);

Does anyone know if this path is the same if it isn't a plugin and if I am using different OSGi environments eg. Equinox Eclipse vs. Apache Felix? e.g. url = new URL("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt");

推荐答案

Bundle接口有一个 getEntry(java.lang.String path) 返回Url的方法,记录为:

The Bundle interface has a getEntry(java.lang.String path) method which return an Url and is documented as:

返回此捆绑包中指定路径的条目的URL。此捆绑包的类加载器不用于搜索条目。仅搜索此包的内容以查找该条目。
指定的路径始终相对于此捆绑包的根,可以以/开头。路径值/表示此捆绑包的根目录。

这篇关于如何在执行java.io.File或FileInputStream时引用OSGi包中的包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆