2

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.

3 Answers 3

2

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.

2
  • thank Neil, I realized that now. But I need to know how you do this "multiple functionality in one bundle" in Declarative Service. Any documents that explains the idea, with code? As usual, I try to learn osgi from scratch. Searching for the APIs, and getting lost there. Jan 28, 2014 at 5:25
  • I think the links that Balazs provided in the comments to his answer are pretty good. Jan 28, 2014 at 14:08
1

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.

5
  • Let's say I have two users. Both selected the same feature, but later, one decided to stop that feature. (The feature here is a bundle). Isn't that a use case? Jan 27, 2014 at 11:33
  • Depends on the meaning of the feature. I think a bundle contains an implementation of a feature. By selecting that feature multiple times, it means that the classes in the bundle are instantiated multiple times. E.g. You can create a DS component that is based on configuration factory. If the user creates a new configuration, the component will be instantiated with that configuration. Jan 27, 2014 at 12:49
  • Are you talking about ComponentFactory? Can you please provide an example, hopefully a small code? Jan 27, 2014 at 19:02
  • This is the first thing that google returns when searching for declarative services. I was talking about code. Jan 28, 2014 at 5:29
  • What about this one: github.com/everit-org/osgi-liquibase-component/blob/master/… This is DataSource component that is instantiated as many times as many configuration is available. The purpose of this one is similar but with different tools: github.com/osgi/bundles/blob/master/osgi.jdbc/src/osgi/jdbc/… Jan 28, 2014 at 8:25
1

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.

1
  • Would you share a completed example for this implementation?
    – Zach Pham
    Mar 23, 2020 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.