A Managed Service is a service that needs configuration data. Such an object should be registered with the Framework registry with the {@code service.pid}property set to some unique identifier called a PID.
If the Configuration Admin service has a {@code Configuration} objectcorresponding to this PID, it will callback the {@code updated()} method ofthe {@code ManagedService} object, passing the properties of that{@code Configuration} object.
If it has no such {@code Configuration} object, then it calls back with a{@code null} properties argument. Registering a Managed Service will alwaysresult in a callback to the {@code updated()} method provided theConfiguration Admin service is, or becomes active. This callback must always be done asynchronously.
Else, every time that either of the {@code updated()} methods is called onthat {@code Configuration} object, the {@code ManagedService.updated()}method with the new properties is called. If the {@code delete()} method iscalled on that {@code Configuration} object, {@code ManagedService.updated()}is called with a {@code null} for the properties parameter. All thesecallbacks must be done asynchronously.
The following example shows the code of a serial port that will create a port depending on configuration information.
class SerialPort implements ManagedService { ServiceRegistration registration; Hashtable configuration; CommPortIdentifier id; synchronized void open(CommPortIdentifier id, BundleContext context) { this.id = id; registration = context.registerService( ManagedService.class.getName(), this, getDefaults() ); } Hashtable getDefaults() { Hashtable defaults = new Hashtable(); defaults.put( "port", id.getName() ); defaults.put( "product", "unknown" ); defaults.put( "baud", "9600" ); defaults.put( Constants.SERVICE_PID, "com.acme.serialport." + id.getName() ); return defaults; } public synchronized void updated( Dictionary configuration ) { if ( configuration == null ) registration.setProperties( getDefaults() ); else { setSpeed( configuration.get("baud") ); registration.setProperties( configuration ); } } ... }
As a convention, it is recommended that when a Managed Service is updated, it should copy all the properties it does not recognize into the service registration properties. This will allow the Configuration Admin service to set properties on services which can then be used by other applications.
Normally, a single Managed Service for a given PID is given the configuration dictionary, this is the configuration that is bound to the location of the registering bundle. However, when security is on, a Managed Service can have Configuration Permission to also be updated for other locations. @author $Id: 396a171acbab5e3842ae9d927ab8b3dbd4126f30 $
|
|
|
|
|
|