No access to Bundle Resource/File (OSGi)
at the moment i'm developing an OSGi based WebApp with Jetty and Equinox (see: http://wiki.eclipse.org/Jetty/Tutorial/EclipseRT-Jetty-Starter-Kit). Everything ist fine so far but i can't get access to some files/resources of my own bundle. The location/path is "configuration/data/config.csv" and "configuration/data/data.zip". I have tested everything:
context.getBundleContext().getBundle().getEntry("config.csv");
context.getBundleContext().getBundle().getResource("config.csv");
this.getClass().getClassLoader().getResource("config.csv");
context.getBundleContext().getDataFile("config.csv");
And of course all possible path variants like: "configuration/data/config.csv", "/configuration/data/config.csv", "\configuration/data/config.csv", "/config.csv". Moreover i have added the folders to the OSGi classpath (in MANIFEST.MF):
Bundle-ClassPath: .,
configuration/data/
The resulting URL looks always somthing like this (or null): "configuration/CBR-Data/config.csv" and when i transfer it to an File object "D:\configuration\CBR-Data\config.csv".
But what i really don't understand is that the 开发者_如何学运维properties file for one of my DS is loaded perfectly:
<properties entry="configuration/dsconfig.properties"/>
Has someone an idea/tip or something else? I'm driving crazy...
You are correctly retrieving the resource from the bundle. I'll suggest to get familiar with the difference between getEntry(), getResource() and getDataFile().
Because methods returns you correct URLs, this means that the resource are correctly located and the problem is in how you read them.
The two ways to use them are:
- Open
InputStream
from theURL
directly:
URL configURL = context.getBundleContext().getBundle().getEntry("configuration/data/config.csv");
if (configURL != null) {
InputStream input = configUrl.openStream();
try {
// process your input here or in separate method
} finally {
input.close();
}
}
- Convert the
URL
toFile
. This approach is not recommended, because it makes the assumption that the Bundle is deployed as directory (and not in archive). It is however helpful if you must deal with legacy libraries which requires you to useFile
objects. To convert to File you cannot useURL.getPath()
method, because Equinox has its own format for the URLs. You should useorg.eclipse.core.runtime.FileLocator
class to resolve to aFile
. TheFileLocator.getBundleFile(Bundle)
does exactly what you want.
Solved!!! Thanks to Danail Nachev (see comment) who brought me to the right way! After some searching after "bundleentry://xyz" and "bundleresource://" i found this mailinglist post: http://www.mail-archive.com/felix-dev@incubator.apache.org/msg02410.html
So the answer is the following (Usging (Equinox) FileLocator):
URL configURL = context.getBundleContext().getBundle().getResource("config.csv");
File configFile = new File(FileLocator.toFileURL(configURL).getPath());
But (also asked in that mailinglist) it would be interesting if there exists other solutionss wich are not only applicable for Equinox?
精彩评论