简体   繁体   English

从osgi bundle访问资源:URL中的bundle方案导致格式错误的异常

[英]Accessing resource from osgi bundle: bundle scheme in the URL lead to a malformed exception

Question

Is there a means to build a java.net.URL object from its constructor with "bundle" as scheme in an osgi context ? 有没有办法从其构造函数构建一个java.net.URL对象,在osgi上下文中使用“bundle”作为scheme?

Exemple 为例

URL url = new URL("bundle://<bundle-id>/path/file.txt");
InputStream in = url.openStream();
...

This code lead to a malformed exception (unknown protocol) when the stream is open. 当流打开时,此代码会导致格式错误的异常(未知协议)。 I don't know how the osgi platform register new schemes to the java.net.URL 我不知道osgi平台如何向java.net.URL注册新方案

Context and ideal solution 背景和理想解决方案

I want to configure a service thanks to Configuration Admin. 我希望通过Configuration Admin配置服务。 This configuration contains a property to set a file path. 此配置包含用于设置文件路径的属性。 I would like the consumer to be able to store the file where he want (file system, inside bundle or even internet). 我希望消费者能够将文件存储在他想要的位置(文件系统,内部捆绑甚至互联网)。

The previous exemple could be great because the code is simple and the consumer could provide path of the form: "bundle:///path/file.txt", "file:///path/file.txt", "http:///path/file.tx", but infortunatly it doesn't work. 前面的例子可能很棒,因为代码很简单,消费者可以提供表单的路径:“bundle:///path/file.txt”,“file:///path/file.txt”,“http: ///path/file.tx“,但实际上它不起作用。

No. Only the framework can construct those URLs. 不可以。只有框架才能构建这些URL。 You can use a Bundle object to return URLs for content in a bundle. 您可以使用Bundle对象返回包中内容的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM