The OSGi specification defines two
mechanisms that are related to service dependency management. These are
the Service Tracker and the WireAdmin service.
The ServiceTracker
is a utility class defined in the OSGi specification. This class
simplifies tracking of registration, modification, and unregistration
of services. Essentially, the Service Tracker embodies a list of
service references that correspond to a particular service. The Service
Tracker is responsible for keeping the list up to date with respect to
changes in the framework.
To use the Service Tracker, a client
creates an instance of it and passes parameters associated to the
service to be tracked (a ServiceReference, a class or a filter). Once
created, the Service Tracker object must be activated by the client
(through the open()
method). Once activated, the client can obtain service objects from the
tracker by calling the getService()
method. When a client requests a service, it is possible that the
Service Tracker returns null if no services are available. The Service
Tracker also provides a method called waitForService
which receives an interval in milliseconds that represents the duration
before the method returns. Finally, the Service Tracker can be
customized by implementing some callback methods that are called when
the list of services change.
Although the Service Tracker does
simplify the act maintaining a list of ServiceReferences associated to
particular services, it does not address the problem of service dependency management.
The WireAdmin service is an
administrative service that is used to control a wiring topology in the
OSGi Service Platform. The WireAdmin service creates Wires, which are objects that that
connect two particular types of services: Producer and Consumer services. Moreover,
multiple Wire objects can exist between the same Producer and Consumer
pair. These services communicate using a sequence of data objects, the
Producer can either push this data into the Consumer or the Consumer
can poll the Producer to obtain (pull) the data objects. Producers and
consumers are identified with a Persistend IDentity (PID). A PID is a
unique identifier (set as the service.pid
property upon service registration) for a service that persists over
multiple invocations of the Framework.
The Wire Admin service maintains a set
of persistent Wire objects. A Wire object contains a PID for a Consumer
service and a PID for a Producer service. Wire objects can be created
when the Producer or Consumer service is not registered. If at some
point, both Producer and Consumer services are registered with the
Framework, they are connected by the Wire Admin service. To connect
them, the Wire Admin service calls a method on both service objects and
provides the list of Wire objects to which they are connected.
The WireAdmin Service addresses service dependency management
problem but only for a particular type of services; moreover, this
mechanism is data-type oriented.
If you want an open source implementation of the WireAdmin service,
you can find it here.