Travelling Salesman
Travelling Salesman

Reputation: 2281

Installing two bundles with the same file

I am using OSGI declarative Services.

In my java application, I launch Apache Felix, and install and start some bundles.

There are two bundles installed using the same file path. Once the bundle starts, it is supposed to display a 'Hello' message.

When I install and start the first bundle:

Bundle bundle1 = context.installBundle("file:C://Users//bundles//myBundle.jar");
bundle1.start();

This shows 'Hello' in the console.

However, when I install the second bundle (with the same file path as the first bundle)

Bundle bundle2= context.installBundle("file:C://Users//bundles//myBundle.jar");
bundle2.start();

I don't see any output. It means that the installation and/or the starting of the second bundle was ignored.

I need a way to install and start two different bundles with the same file path, and when I stop one of these bundles, the other should stay ACTIVE. How can I possibly achieve that? Thanks.

The use case I am thinking of: Let's say I have two users, and both would like to use the same feature (bundle). What if one of them decided to stop the bundle feature, and the other one would like to keep it?

Is there a better way to achieve that? Thanks.

Upvotes: 2

Views: 806

Answers (3)

Malek Ben Salem
Malek Ben Salem

Reputation: 11

There are two things you need to consider: The bundle location must be unique and the framework must be set to accept multiple bundles with the same bundle symbolic name. You should use bundleContext.install(location, inputStream) to install the bundle. Maybe like this:

byte[] byteArray byteArray = IOUtils.toByteArray(new FileInputStream(new File(filePath);
Bundle bundle = bc.installBundle(jobID, new ByteArrayInputStream(byteArray));

And to enable the framework to accept multiple bundles with the same bundle symbolic name, you need to start the framework with the following option:

-Dorg.osgi.framework.bsnversion=multiple

Notice that the update command will still try to update the bundle from the given location, which in my case (jobID) was not a real file path. This was irrelevant for my use case, so I never bothered to solve it.

Upvotes: 1

Balazs Zsoldos
Balazs Zsoldos

Reputation: 6046

You can call the function

context.installBundle(location, inputStream)

and pass an InputStream to that function. In that case, you can specify two different locations (e.g. one that has some meaning but not a real location).

Your next problem will be if two bundles with the same SymbolicName and same version can cannot exist twice.

I cannot imagine a use-case where the same bundle should be installed twice. It might be the result of a bad concept.

Upvotes: 1

Neil Bartlett
Neil Bartlett

Reputation: 23958

Does the file content change in between installing two times? If not, then you cannot do this. Essentially you're trying to instantiate the bundle twice, and OSGi only allows each bundle to be installed once.

Actually you can have multiple versions of a bundle at the same time, but the pair of Bundle-SymbolicName and Bundle-Version must be unique within the framework.

Mapping user functionality to installed bundles is really not a good idea. Why should users care about the modules installed in your application?? Instead you want to make the functionality in the bundle support multiple users.

Upvotes: 2

Related Questions