Single versus multiple configuration instances
You can also configure multiple configuration instances by using the OSGi metatype services.
As described in the Describing configuration by using the OSGi Metatype service, you can use OSGi metatype service to support a single set of configuration properties for a given service (as identified by a configuration PID). It is also common to support multiple instances of the same configuration type, for example in the way that Liberty supports multiple entries for applications and data sources. This can be done by providing a metatype definition that tells the Liberty configuration parser, and the Configuration Admin service, that it is dealing with a factory configuration. Also, the class that receives the configuration needs to implement the org.osgi.service.cm.ManagedServiceFactory interface.
<server>
<teenager name="joy" age="15" />
<teenager name="angela" age="18" />
</server>
<?xml version="1.0" encoding="UTF-8"?>
<metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.1.0"
xmlns:ibm="http://www.ibm.com/xmlns/appservers/osgi/metatype/v1.0.0">
<OCD id="teenager-ocd" name="teenager" >
<AD id="name" name="name" type="String" />
<AD id="age" name="age" type="Integer" />
</OCD>
<Designate factoryPid="teenager" pid="teenager">
<Object ocdref="teenager-ocd" />
</Designate>
</metatype:MetaData>
bundleContext.registerService(ManagedServiceFactory.class, new MgdSvcFactoryImpl(), new Hashtable();
defaults.put(org.osgi.framework.Constants.SERVICE_PID, "teenager"));
public void updated(String pid, Dictionary<String, ?> properties)
throws ConfigurationException {
String name = (String) properties.get("name");
Integer age = (Integer) properties.get("age");
}
If a particular configuration instance is deleted, for example because one of the teenager elements is deleted from the server.xml file, the ManagedServiceFactory implementation is notified through the deleted() method, and the pid of the deleted instance is provided. This allows the ManagedServiceFactory implementation to keep track of the instances that are valid at any given time.