views:

103

answers:

4

I have created two OSGI bundles A and B using the eclipse Plug-in project wizard (using eclipse Helios).

In the manifest file of bundle B I have added bundle A as a dependency. Further I have exported the packages in A so they are visible for B. I also have a .properties file in bundle A that I would like to make visible for bundle B. In the build.properties pane in bundle A I have specified:

source.. = src/
bin.includes = META-INF/,\
               .,\
               bundle_A.properties

Now in bundle B I try to load the .properties file using:

  private Properties loadProperties() {
    Properties properties = new Properties();
    InputStream istream = this.getClass().getClassLoader().getResourceAsStream(
        "bundle_A.properties");
    try {
      properties.load(istream);
    } catch (IOException e) {
      logger.error("Properties file not found!", e);
    }
    return properties;
  }

But that gives a nullpointer exception (the file is not found on the classpath).

Is it possible to export resources from bundle A (just like when you export packages) or somehow access the file in A from B in another way (accessing the classloader for bundle A from bundle B)?

+1  A: 

Have you considered adding a method to bundle A's API that loads and returns the resource?

Many might consider this a better design as it allows the name or means of storage of the resource to change without breaking clients of bundle A.

ChrisH
That approach works when I run the plugin-test. But when I run the bundle through the launch configuration the .properties file cannot be found. What is the difference between setting up a pluign-test and OSGI run configuration?
tul
+1  A: 

Have you tried using the BundleContext of bundle A to load resources?

Tassos Bassoukos
Jep and that works fine, I am just puzzled why its not possible to load a resource from another bundle using eg:this.getClass().getClassLoader().getResourceAsStream( "bundle_A.properties");when the bundle is specified as a dependency and the .properties file are located in a package that is exported.
tul
Because 'this' is in bundle A. Use a class from Bundle B and it works.
Tassos Bassoukos
+1  A: 

If you're writing an Eclipse plug-in you could try something like:

Bundle bundle = Platform.getBundle("your.plugin.id");

Path path = new Path("path/to/a/file.type");

URL fileURL = Platform.find(bundle, path);

InputStream in = fileURL.openStream();
Johannes Wachter
+3  A: 

The getEntry(String) method on Bundle is intended for this purpose. You can use it to load any resource from any bundle. Also see the methods findEntries() and getEntryPaths() if you don't know the exact path to the resource inside the bundle.

There is no need to get hold of the bundle's classloader to do this.

Neil Bartlett
Also note there is no need to export the package containing the resources from bundle A.
Neil Bartlett