执行 java.io.File 或 FileInputStream 时如何引用 OSGi 包中包含的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3810303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reference an included file in OSGi bundle when performing java.io.File or FileInputStream
提问by herbyme
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.
我正在使用 aQute Bnd 工具集来创建 OSGi 包并打包了一些相关的“资源”文件。这包括我创建的资源目录中的 *.css 文件和 *.xsd 文件。
I have included the following in the bundle.bndfile:
我在bundle.bnd文件中包含了以下内容:
Include-Resource: resources/=resources/
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.
当我进行构建时,生成的 *.jar 文件在 jar 包文件的顶级目录的资源目录中有 *.css 和 *.xsd 文件。
However, in the actual code I am having difficulty in trying to refer to this as part of my class path:
但是,在实际代码中,我很难将其作为类路径的一部分进行引用:
I have tried the following:
我尝试了以下方法:
new File("resources/example.css");
I have also tried:
我也试过:
URL cssFile = this.getClass().getResource("resources/example.css");
try
{
file = new File(cssFile.toURI()));
}
catch(Exception e)
{
e.printStackTrace();
}
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.
我要么得到一个 NullPointException 错误,要么一个 File cannot be found IOException 错误(取决于我使用的是哪一个)。在调试配置模式下的 Eclipse Equinox 以及 Apache Felix(我们用于部署)中运行时,我收到此错误。注意我试图在 BundleActivator 之外的 Java 类中执行此操作。
Do I need to always refer to the context of the BundleActivator e.g.?
我是否需要总是参考 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"));
}
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.
我认为上面的方法会起作用,但这意味着我将不得不传递 cssFile 引用,它看起来并不优雅。
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?
有什么方法可以引用包含在捆绑 jar 文件中的任何给定 Java 类中的“资源”目录的路径,该类是捆绑/.jar 文件的一部分?如果它涉及 BundleContext,有没有办法在任何 Java 类中引用它?
Any help will be much appreciated.
任何帮助都感激不尽。
I have had a look at and Including additional resources with OSGi bundlesbut it appears that you need the BundleContext.
我已经查看并包含 OSGi 包中的其他资源,但似乎您需要 BundleContext。
I might have found a possible solution to this: http://www.vogella.de/blog/tag/plugin/
我可能已经找到了一个可能的解决方案:http: //www.vogella.de/blog/tag/plugin/
Looks like Vogella has some example code for this:
看起来 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();
}
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");
有谁知道如果它不是插件并且我使用不同的 OSGi 环境,则此路径是否相同,例如。Equinox Eclipse 与 Apache Felix?例如url = new URL("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt");
采纳答案by Manuel Selva
The Bundle interfacehas a getEntry(java.lang.String path)
method which return an Url and is documented as:
该捆绑接口具有getEntry(java.lang.String path)
其返回一个地址和记录为方法:
Returns a URL to the entry at the specified path in this bundle. This bundle's class loader is not used to search for the entry. Only the contents of this bundle are searched for the entry. The specified path is always relative to the root of this bundle and may begin with "/". A path value of "/" indicates the root of this bundle.
返回此包中指定路径中条目的 URL。此包的类加载器不用于搜索条目。仅搜索此包的内容以查找条目。指定的路径总是相对于这个包的根目录,并且可以以“/”开头。路径值“/”表示此包的根。
回答by tbone
If you are building with Eclipse/Equinox, then you can get the location of a Bundle from outwith the BundleContext contained within the BundleActivator by calling something like:
如果您使用 Eclipse/Equinox 进行构建,那么您可以通过调用以下内容从 BundleActivator 中包含的 BundleContext 中获取 Bundle 的位置:
Bundle yourBundle = Platform.getBundle("bundleSymbolicName");
Path relativePathToBundle = new Path("/relativePath");
FileLocator.openStream(yourBundle, relativePathToBundle, false);
The Platform and FileLocator classes come from the org.eclipse.core.runtime plug-in so if you have a dependency on that then the above should allow you to get access to your resource.
Platform 和 FileLocator 类来自 org.eclipse.core.runtime 插件,所以如果您依赖它,那么上面应该允许您访问您的资源。
回答by Neil Bartlett
I think the answer should be as simple as the following:
我认为答案应该很简单:
URL cssFile = this.getClass().getResource("/resources/example.css");
I.e. make sure there is a leading slash in the path. You had it without a leading slash, which would make it "relative" to the class you're calling from.
即确保路径中有一个前导斜杠。你有它没有前导斜杠,这将使它与你正在调用的类“相对”。
Alternatively the following should work:
或者,以下应该有效:
this.getClass().getClassLoader().getResource("resources/example.css");