Question

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

Était-ce utile?

La solution

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.

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top