alex_jelimalai
alex_jelimalai

Reputation: 11

How to share resources of a OSGI bundle to other bundles

How can resource of a bundle be shared with other bundle(but not just one)? For example I don't want to duplicate the images or properties file to all bundles, instead I want to access them from a single place.

I tried to use Fragment-Host, but there is not possible to specify multiple bundles(at least I do not know how) eg:

   <!-rest of the pom--> 
   <instructions>
       <Fragment-Host>
          com.bundlehost
       </Fragment-Host>
    </instructions>

Upvotes: 1

Views: 943

Answers (3)

Holly Cummins
Holly Cummins

Reputation: 11482

You can also use normal package imports and exports to ensure the resources are on the class path of the consuming bundle. See, for example, access common property file inside bundle with osgi.

Upvotes: 1

Neil Bartlett
Neil Bartlett

Reputation: 23948

As Dmytro notes, you can use OSGi API methods to access the resources in any bundle. However the harder question is this: how do you know from which bundle to access these resources, and how do you know where they are located within the bundle?

If you just make assumptions or hard-code the answer, then you end up with a very brittle system and a hidden coupling between the bundles. Then when somebody deploys your bundles into an application but doesn't include the resource bundle, everything breaks. This defeats the point of modularity.

Upvotes: 2

Dmytro Pishchukhin
Dmytro Pishchukhin

Reputation: 2369

You can use Bundle.getEntryPaths() and Bundle.getEntry() to get bundle resources. To read content use URL.openStream()

Upvotes: 1

Related Questions