无法访问捆绑资源/文件(OSGi) [英] No access to Bundle Resource/File (OSGi)

查看:114
本文介绍了无法访问捆绑资源/文件(OSGi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用Jetty和Equinox开发基于OSGi的WebApp(请参阅: http ://wiki.eclipse.org/Jetty/Tutorial/EclipseRT-Jetty-Starter-Kit ). 到目前为止,一切都还不错,但我无法访问我自己的捆绑包中的某些文件/资源​​. 位置/路径是"configuration/data/config.csv"和"configuration/data/data.zip". 我已经测试了所有内容:

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");

当然还有所有可能的路径变体,例如:"configuration/data/config.csv","/configuration/data/config.csv","\ configuration/data/config.csv","/config.csv" . 此外,我已经将文件夹添加到OSGi类路径(在MANIFEST.MF中):

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/

生成的URL看起来总是这样(或为null):"configuration/CBR-Data/config.csv",当我将其传输到文件对象"D:\ configuration \ CBR-Data \ config.csv"时

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".

但是我真正不明白的是,我的一个DS的属性文件已完美加载:
<properties entry="configuration/dsconfig.properties"/>

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...

推荐答案

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

You are correctly retrieving the resource from the bundle. I'll suggest to get familiar with the difference between getEntry(), getResource() and getDataFile().

因为方法返回正确的URL,这意味着资源位置正确,问题出在读取它们的方式上.

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. 直接从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)完全满足您的要求.
  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.

这篇关于无法访问捆绑资源/文件(OSGi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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