shashantrika
shashantrika

Reputation: 1089

How to share resources between multiple osgi bundles

Im having two osgi bundles .

Bundle A (Consumer Bundle)

Consumer.java

myService = new MyImpl((InputStream)  Client.class.getClassLoader().getResourceAsStream(DEF_FILE));

DEF_FILE is in src/main/resources

Bundle B (Service Bundle)

Contains the service and the implementation classes .

MyImpl.java

public MyImpl(InputStream inputStream)
{
     try
     {
         readFunction(inputStream);
         LOG.info(" ReadFunction in " + inputStream);
     }
     catch (Exception e)
     {
         LOG.error("Could not define Readfunction in " + inputStream, e);
     }
}

The main intention is to read the resources file declared in one bundle in the other bundle. I could use the maven-resource-plugin or assembly plugin but in that also I need to add the dependent bundles in the pom , which I don't prefer because of cyclic dependency problems . Is there any other way to read the files from one bundle to the other in a efficient way ?

NOTE : I may have alot of consumer bundles for the Service .

Upvotes: 3

Views: 838

Answers (2)

Peter Kriens
Peter Kriens

Reputation: 15372

Sharing static resources sounds very low level for an OSGi application. In my experience you want to only share services between bundles. These services could then represent domain objects that are stored in these static resources. With the extender pattern you can turn resources in a bundle into services.You might want to take a look at the extender pattern to see how this works in general.

In a web environment those resources could be web resources, in that case you could serve from a special servlet like for example the OSGi enRoute web server servlet does. This makes it a lot easier to add functionality that is shared.

OSGi is really good in creating these abstractions that can save a lot of time when your product evolves. Obviously, you could also have a perfectly valid use case for using streams.

Upvotes: 3

Neil Bartlett
Neil Bartlett

Reputation: 23948

The Bundle interface has getResource and getResources methods. Refer to the linked JavaDocs for details.

Upvotes: 2

Related Questions