无法访问捆绑资源/文件 (OSGi)

发布于 2024-11-14 01:39:30 字数 1144 浏览 1 评论 0原文

目前我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与之呼应 2024-11-21 01:39:30

您正在正确地从捆绑包中检索资源。我建议熟悉 getEntry()、getResource() 和 getDataFile() 之间的区别。

因为方法会返回正确的 URL,这意味着资源已正确定位,问题在于您如何读取它们。

使用它们的两种方法是:

  1. 直接从 URL 打开 InputStream

    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();
        }
    }
   
  1. 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:

  1. Open InputStream from the URL 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();
        }
    }
   
  1. Convert the URL to File. 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 use File objects. To convert to File you cannot use URL.getPath() method, because Equinox has its own format for the URLs. You should use org.eclipse.core.runtime.FileLocator class to resolve to a File. The FileLocator.getBundleFile(Bundle) does exactly what you want.
月朦胧 2024-11-21 01:39:30

解决了!!!
感谢 Danail Nachev(见评论),他带我走上了正确的道路!经过一番搜索“bundleentry://xyz”和“bundleresource://”后,我发现了这个邮件列表帖子:http://www.mail-archive.com/[email protected]/msg02410.html

所以答案如下(使用(Equinox)FileLocator):

URL configURL = context.getBundleContext().getBundle().getResource("config.csv");
File configFile = new File(FileLocator.toFileURL(configURL).getPath());

但是(也在该邮件列表中询问)有趣的是如果是否存在其他不仅适用于 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):

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文