0
votes

I am using two OSGi frameworks within a plain Java application. Both frameworks load bundles from a shared directory.

In one bundle, I load a file from the resources. I tried different ways e.g.

this.getClass().getClassLoader().getResourceAsStream(...)
FrameworkUtil.getBundle(XXX.class).getEntry(...)
FrameworkUtil.getBundle(XXX.class).getResource(...)

But it doesn't matter which of the commands I use, it all works fine at the beginning. However, after several install and uninstall steps in both frameworks. The returned InputStream is null.

I also works fine if use just one OSGi framework.

After debugging a little, I found that the Bundle a got with

FrameworkUtil.getBundle(XXX.class)

is pointing to the correct jar-file BUT when I look for the referenced bundlefile in the BundleData of the Bundle, it references the bundlefile of another bundle. The bundlefiles are temporary files of the OSGi framework (Equinox in my case), that can be found for example in the local Maven repository:

.m2\repository\org\eclipse\osgi\org.eclipse.osgi\3.6.0.v20100517\configuration\org.eclipse.osgi\bundles\29\1

Anyone has an idea what could be wrong here?

2

2 Answers

0
votes

Is the code doing the resource loading running from a bundle in the framework? Or from code outside the framework?

Each time a bundle is resolved, it gets a new class loader. When the bundle becomes unresolved (like when it is uninstalled), its class loader is destroyed and disconnected from the backing store (e.g. bundle jar file). So the Class object you use may no longer be useful since it was loaded from a now destroyed class loader.

Remember, at runtime a Class object is unique by the class file, class loader pair.

0
votes

Both frameworks use the same directory to save the configurations of the bundles. It seems by accident one framework overwrites the bundlefile/configuration file of the other framework.

When the bundle tries to access its resources it looks up the configuration file. If this file has been overwriten, the entry for the resource file is not available anymore, leading to an InputStream with value null.

To avoid these kind of problems, it is possible to set the configuration directoy for each framework differently, e.g by

Map<String, String> frameworkPropertiesMap = new HashMap<String, String>();
frameworkPropertiesMap.put("osgi.configuration.area", "@user.home/osgi-framework-configuration-" + numberOfFramework);
framework = getFrameworkFactory().newFramework(frameworkPropertiesMap);
framework.start();