2
votes

I have one java application which uses OSGI model :

I have two preexisting bundles :

  1. com.mos
  2. com.login

com.login has a implementation and registration of Authenticator service (own service for authentication).com.login as well as com.mos uses this authentication service.

Now I am writing one new bundle (com.new) and I have to add / modify Authenticator Service so I have written my own implementation of it.

Once I start my program / application, if my new bundle com.new runs after com.login then all bundles uses new Authenticator Service.But If com.new bundles runs before com.login then old Authenticator Service is available.

Is there any mechanism in OSGI where we give some priority something like which bundle should start first.

2
So do you want to ensure that always the new authenticator service is used? Then you could introduce dependencies as @Thomas suggests (I would also refrain from tweaking startlevels). Or do you want to dynamically find out at runtime which is the best authenticator to use (i.e. use the new one once it is available)? Then you could use a ServiceListener or ServiceTracker along with suitable filters to always know which authenticators are available.Tobold

2 Answers

1
votes

The OSGi bundle startlevels allow you to influence the start order for each bundle. See the according javadocs http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/startlevel/package-summary.html But I would not recommend to do that. Start levels should usually not be used as a way to control service startup. In OSGi service start orders are not guaranteed and services may come and go at will.

Making your new bundle (com.new) depending on the specific implementation of your Authenticator service would do the trick and guarante the correct order.

1
votes

In general, when you have multiple OSGi services available, you have two options to pick one:

  1. Service Filter
  2. Service Ranking

Service Filter can be used to filter out services based on service properties as described here or here.

Service Ranking published by the service makes them eligible to be picked up based on the service ranking. The one with highest service ranking will be picked up as described here or here.

According to the documentation of the BundleContext.getServiceReference() method:

If multiple such services exist, the service with the highest priority is selected. This priority is defined as the service reference with the highest ranking (as specified in its Constants.SERVICE_RANKING property) is returned.

If there is a tie in ranking, the service with the lowest service ID (as specified in its Constants.SERVICE_ID property); that is, the service that was registered first is returned.