无法访问捆绑资源/文件 (OSGi)
目前我正在使用 Jetty 和 Equinox 开发一个基于 OSGi 的 Web 应用程序(请参阅:http:// wiki.eclipse.org/Jetty/Tutorial/EclipseRT-Jetty-Starter-Kit)。 到目前为止一切都很好,但我无法访问我自己的捆绑包的某些文件/资源。 位置/路径是“configuration/data/config.csv”和“configuration/data/data.zip”。 我已经测试了所有内容:
context.getBundleContext().getBundle().getEntry("config.csv");
context.getBundleContext().getBundle().getResource("config.csv");
this.getClass().getClassLoader().getResource("config.csv");
context.getBundleContext().getDataFile("config.csv");
当然还有所有可能的路径变体,例如:“configuration/data/config.csv”、“/configuration/data/config.csv”、“\configuration/data/config.csv”、“/config.csv”。 csv”。 此外,我已将文件夹添加到 OSGi 类路径(在 MANIFEST.MF 中):
Bundle-ClassPath: .,
configuration/data/
生成的 URL 看起来总是这样(或为 null):“configuration/CBR-Data/config.csv”,当我将其传输到 File 对象时“D:\configuration\CBR-Data\config.csv”。
但我真正不明白的是,我的 DS 之一的属性文件已完美加载:
有人有想法/提示或其他东西吗?我快要疯了...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在正确地从捆绑包中检索资源。我建议熟悉 getEntry()、getResource() 和 getDataFile() 之间的区别。
因为方法会返回正确的 URL,这意味着资源已正确定位,问题在于您如何读取它们。
使用它们的两种方法是:
URL
打开InputStream
:URL
转换为File
。不建议使用此方法,因为它假设 Bundle 部署为目录(而不是存档中)。但是,如果您必须处理需要使用File
对象的遗留库,那么它会很有帮助。要转换为文件,您不能使用URL.getPath()
方法,因为 Equinox 有自己的 URL 格式。您应该使用 org.eclipse.core.runtime.FileLocator 类来解析为 File。FileLocator.getBundleFile(Bundle)
正是您想要的。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:
InputStream
from theURL
directly: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.解决了!!!
感谢 Danail Nachev(见评论),他带我走上了正确的道路!经过一番搜索“bundleentry://xyz”和“bundleresource://”后,我发现了这个邮件列表帖子:http://www.mail-archive.com/[email protected]/msg02410.html
所以答案如下(使用(Equinox)FileLocator):
但是(也在该邮件列表中询问)有趣的是如果是否存在其他不仅适用于 Equinox 的解决方案?
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/[email protected]/msg02410.html
So the answer is the following (Usging (Equinox) FileLocator):
But (also asked in that mailinglist) it would be interesting if there exists other solutionss wich are not only applicable for Equinox?