我是 OSGi 新手,创建了一个 OSGi 捆绑包,并在 Apache Felix OSGi 容器中运行。
捆绑包中包含一个文件资源,我需要将其作为 java.io.File
传递给方法。要实例化文件对象,“文件”方案中的 URI 或字符串路径是必需的。我如何以干净的方式检索其中的任何一个?
我尝试使用
context.getBundle().getResource("/myfile")
(其中 context 的类型为 org.osgi.framework.BundleContext
),它返回 URI bundle: //6.0:0/myfile
。
但此 URI 无法使用 File(URI uri)
构造函数转换为文件实例,因为它具有“bundle”方案。
人们可以尝试构建一条到达该位置的路径,了解工作目录并利用我的包的bundleId,但我怀疑这是最佳实践。
有什么想法吗?
I am new to OSGi and created an OSGi-bundle which I run in the Apache Felix OSGi-container.
There is a file resource contained in the bundle, which I need to pass to a method as a java.io.File
. To instantiate a File-object, either an URI in the "file"-scheme or the path as string is necessary. How do I retrieve any of those in a clean way?
I tried using the
context.getBundle().getResource("/myfile")
(where context is of type org.osgi.framework.BundleContext
) which returns the URI bundle://6.0:0/myfile
.
But this URI can't be converted to a File-instance using the File(URI uri)
constructor since it has the "bundle"-scheme.
One could try to construct a path to the location knowing the working directory and exploiting the bundleId of my bundle, but I doubt this is the best practice.
Any ideas?
发布评论
评论(3)
由于该文件位于您的包内,因此您无法使用标准的文件来访问它。您从 URL rel="noreferrer">
Bundle.getResource()
是获取这些资源的正确方法,因为 OSGi API 也适用于没有实际文件系统的系统。我总是尝试坚持使用 OSGi API,而不是使用特定于框架的解决方案。因此,如果您可以控制该方法,我会更新它以采用
URL
,甚至可能是InputStream
(因为您可能只想从中读取)。为了方便起见,您始终可以提供一个确实接受文件
的辅助方法。如果您无法控制该方法,则必须编写一些辅助方法来获取
URL
,将其流式传输到文件(例如,File.createTempFile()
可能会成功。Since the file is inside your bundle, there is no way for you to get to it using a standard
File
. TheURL
you get fromBundle.getResource()
is the correct way to get to these resources, since the OSGi APIs are intended to also work on systems without an actual file system. I would always try to stick to the OSGi API instead of using framework-specific solutions.So, if you have control over the method, I would update it to take a
URL
, or maybe even anInputStream
(since you probably just want to read from it). For convenience, you can always provide a helper method that does take aFile
.If you don't have control over the method, you will have to write some helper method that takes the
URL
, streams it out to a file (for instance,File.createTempFile()
will probably do the trick.也许API很容易混淆,但是您可以像这样访问OSGI包内的文件:
getResource
将通过整个OSGI容器找到资源,就像OSGI类加载器理论一样。getEntry
将从本地包中查找资源。返回的 url 可以转换为文件,但可以转换为 inputStream。这是一个与此相同的问题: No access to Bundle Resource/File (OSGi)
希望这对您有帮助。
Maybe the API is confusable, but You can access a file inside an OSGI bundle like this:
getResource
will find the resource through the whole OSGI container just like OSGI classloader theory.getEntry
will find the resource from local bundle. and the return url could be convert to file but inputStream.Here is a question same with this: No access to Bundle Resource/File (OSGi)
Hope this helping you.
我使用的是 getClassLoader().getResourceAsStream():
这样文件将从您的资源目录加载。文件名应包含“src/main/resources”之后的路径。
完整示例在这里:
What I use is getClassLoader().getResourceAsStream():
This way the file will be loaded from your resource dir. FileName should contain the path after "src/main/resources".
Full example here: