Manage multiple service instances. Bundles registering this interface are giving the Configuration Admin service the ability to create and configure a number of instances of a service that the implementing bundle can provide. For example, a bundle implementing a DHCP server could be instantiated multiple times for different interfaces using a factory.
Each of these service instances is represented, in the persistent storage of the Configuration Admin service, by a factory {@code Configuration} object that has a PID. When such a{@code Configuration} is updated, the Configuration Admin service calls the{@code ManagedServiceFactory} updated method with the new properties. When{@code updated} is called with a new PID, the Managed Service Factory shouldcreate a new factory instance based on these configuration properties. When called with a PID that it has seen before, it should update that existing service instance with the new configuration information.
In general it is expected that the implementation of this interface will maintain a data structure that maps PIDs to the factory instances that it has created. The semantics of a factory instance are defined by the Managed Service Factory. However, if the factory instance is registered as a service object with the service registry, its PID should match the PID of the corresponding {@code Configuration} object (but it should not beregistered as a Managed Service!).
An example that demonstrates the use of a factory. It will create serial ports under command of the Configuration Admin service.
class SerialPortFactory implements ManagedServiceFactory { ServiceRegistration registration; Hashtable ports; void start(BundleContext context) { Hashtable properties = new Hashtable(); properties.put( Constants.SERVICE_PID, "com.acme.serialportfactory" ); registration = context.registerService( ManagedServiceFactory.class.getName(), this, properties ); } public void updated( String pid, Dictionary properties ) { String portName = (String) properties.get("port"); SerialPortService port = (SerialPort) ports.get( pid ); if ( port == null ) { port = new SerialPortService(); ports.put( pid, port ); port.open(); } if ( port.getPortName().equals(portName) ) return; port.setPortName( portName ); } public void deleted( String pid ) { SerialPortService port = (SerialPort) ports.get( pid ); port.close(); ports.remove( pid ); } ... }
@author $Id: d91f9771b95e91e371aaccf4811fc1e463cbdade $