如何在OSGI环境中从磁盘加载资源

时间:2022-05-08 23:52:50

Part 1: I've been trying to load a (XML) file as a resource from disk using bundle class loader. I can't package the file in a bundle beforehand as it'll be generated at runtime. I tried searching too, and almost always, people talk about loading resources from within a bundle (either same or different). So is it even possible to load a resource from disk in an OSGi environment using bundle classloader? If yes, how can it be done?

第1部分:我一直在尝试使用bundle class loader从磁盘加载(XML)文件作为资源。我无法预先将文件打包在一个包中,因为它将在运行时生成。我也试过搜索,几乎总是,人们谈论从一个包(相同或不同)中加载资源。那么甚至可以使用bundle classloader从OSGi环境中的磁盘加载资源吗?如果是,怎么办?

Part 2: Now I need to add a constraint to the above. In my complete scenario, while I'd be generating the file, it would be loaded by a third-party bundle. In this case, what could be done (generate in a certain location, any changes to classpath etc.) so that the third-party bundle's class loader could find the generated file?

第2部分:现在我需要为上面添加一个约束。在我的完整场景中,当我生成文件时,它将由第三方包加载。在这种情况下,可以做什么(在某个位置生成,类路径的任何更改等),以便第三方包的类加载器可以找到生成的文件?

Using: apache karaf 3.0.2, ubuntu 12.

使用:apache karaf 3.0.2,ubuntu 12。

1 个解决方案

#1


Part 1:

So is it even possible to load a resource from disk in an OSGi environment using bundle classloader?

那么甚至可以使用bundle classloader从OSGi环境中的磁盘加载资源吗?

Resources (read-only files on the classpath) can be loaded with classloaders, not ordinary files from any folder of the disk. When you want to process the content of files from the ClassPath, you should use the classloader.

资源(类路径上的只读文件)可以使用类加载器加载,而不是来自磁盘任何文件夹的普通文件。如果要处理ClassPath中的文件内容,则应使用类加载器。

You want to generate a temporary file (generated and processed at runtime) so you should use the standard Java API for that:

您希望生成一个临时文件(在运行时生成和处理),因此您应该使用标准Java API:

File myTmpFile = File.createTempFile(...);

For more info, see the javadoc of this function: https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String)

有关更多信息,请参阅此函数的javadoc:https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang。串)

Part 2:

The third bundle should have an API that either accepts a File, URL, Path or other type instance that can point to a file in the file system.

第三个bundle应该有一个API,可以接受File,URL,Path或其他可以指向文件系统中文件的类型实例。

#1


Part 1:

So is it even possible to load a resource from disk in an OSGi environment using bundle classloader?

那么甚至可以使用bundle classloader从OSGi环境中的磁盘加载资源吗?

Resources (read-only files on the classpath) can be loaded with classloaders, not ordinary files from any folder of the disk. When you want to process the content of files from the ClassPath, you should use the classloader.

资源(类路径上的只读文件)可以使用类加载器加载,而不是来自磁盘任何文件夹的普通文件。如果要处理ClassPath中的文件内容,则应使用类加载器。

You want to generate a temporary file (generated and processed at runtime) so you should use the standard Java API for that:

您希望生成一个临时文件(在运行时生成和处理),因此您应该使用标准Java API:

File myTmpFile = File.createTempFile(...);

For more info, see the javadoc of this function: https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String)

有关更多信息,请参阅此函数的javadoc:https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang。串)

Part 2:

The third bundle should have an API that either accepts a File, URL, Path or other type instance that can point to a file in the file system.

第三个bundle应该有一个API,可以接受File,URL,Path或其他可以指向文件系统中文件的类型实例。