0

What is the correct way to get a service, hopefully using Declarative Service if possible, when you don't know the attributes of the service to request until after runtime?

The use case is analogous to having 3 bundles providing services version 1.0, 2.0 and 3.0 but not knowing which one will be consumed until the user chooses one in the UI. If the user chooses 2.0 the consumer will consume the stuff from bundle 2.0

We are using BND annotations, so something with them would be ideal, but i have a feeling that we need to use the OSGi API directly instead of using annotations or declarative services injection.

Finally, if it is relevant, this is more to get different versions of a resource (XML schema) rather than about different behaviour/implementations. The idea was that the service would be providing its internal resource, which would be different in each version, even though the code in the service itself would be the same

Hilikus
  • 9,954
  • 14
  • 65
  • 118

2 Answers2

1

The declarative model of the Declarative Services specification is a build-time model, not a run-time one. To do run-time dependency management, you either need to do it yourself with a ServiceTracker, or use a different dependency management solution.

As one of its authors, I have a preference for the Apache Felix Dependency manager [1] which allows you to "declare" dependencies in Java code (at run-time, for example based on a choice made by a user in the UI like you say).It does not use Bnd annotations, but the code still allows you to use a declarative style and provides features like injection and/or callbacks.

Another solution that allows this is Apache Felix iPOJO [2].

[1] http://felix.apache.org/documentation/subprojects/apache-felix-dependency-manager.html

[2] http://felix.apache.org/documentation/subprojects/apache-felix-ipojo.html

Marcel Offermans
  • 3,313
  • 1
  • 15
  • 23
  • we are currently considering iPojo. what exactly of it could be used to do this runtime decision. I will also take a look at Dependency Manager – Hilikus Mar 17 '14 at 21:45
  • I'm not that familiar with it, but I understood that (a while ago) it added support for declaring dependencies through a Java API just like the Dependency Manager. – Marcel Offermans Mar 18 '14 at 07:25
1

I've worked in a similar system before and we had our own "routing" system. Basically when you register the services add the version number in the meta-data. Then in this routing mechanism pick the correct service. Your services will need to implement a common interface and in the router inject a List of them.

codesalsa
  • 882
  • 5
  • 18
  • so you mean a service that just gets notified of all other services registered and then this service, which you call routing mechanism, will provide a list of all the accumulated services via a List? if that is what you meant, the thing is i imagine this routing system is just a service registry that i imagine must already exist in the OSGi framework – Hilikus Mar 18 '14 at 02:53
  • 1
    Well the problem is that at runtime you want all versions available and pick based on the request from user so that is how we solved it. The router does not provide the list, it has the list. It gets the request and then gives back the correct versioned implementation to use. – codesalsa Mar 18 '14 at 05:42