3
votes

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 .

2
It depends a lot on what you're trying to achieve with this. If these are shared static resources without which the consumer bundles cannot function then including them as Maven dependencies and packaging them with the consumer as well is the way to go. If they're dynamic, non-mandatory resources which make sense to be consumed from a central service then go ahead and fetch them using whatever API. - pnt

2 Answers

3
votes

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.

2
votes

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