Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: FELIX-964 Add section on ManagedServiceFactory (thanks for the patch)

...

After the call to update the Configuration Admin service persists the new configuration data and sends an update to the ManagedService registered with the service PID org.sample.PrettyPrinterConfigurator, which happens to be our PrettyPrinterConfigurator class as expected.

ManagedServiceFactory example

Registering a service as ManagedServiceFactory means that it will be able to receive several different configuration dictionaries; that's particularly useful when we want to create a Service Factory, that is, a service responsible for creating multiple instances of a specific service.

A ManagedServiceFactory service needs to implement the ManagedServiceFactory interface, as showed in the example.

Code Block

public class SmsSenderFactory implements ManagedServiceFactory
{	
	Map existingServices = new HashMap();
	
	public void updated(String pid, Dictionary dictionary) throws ConfigurationException 
	{
		// invoked when a new configuration dictionary is assigned
		// to service 'pid'. 
		if (existingServices.containsKey("pid"))  //the service already exists
		{
			MyService service = (MyService) existingServices.get(pid);
			service.configure(dictionary);
		}
		else //configuration dictionary for a new service
		{
			MyService service = createNewServiceInstance();
			service.configure(dictionary);
			existingServices.put(pid, service);
		}
	}
	
	public void deleted(String pid) 
	{
		// invoked when the service 'pid' is deleted
		existingServices.remove(pid);
	}

	public String getName() 
	{
		return "test.smssenderfactory";
	}
}

The example above shows that, differently from the ManagedService, the ManagedServiceFactory is designed to manage multiple instances of a service. In fact, the update method accept a pid and a Dictionary as arguments, thus allowing to associate a certain configuration dictionary to a particular service instance (identified by the pid).

Note also that the ManagedServiceFactory interface requires to implement (besides the getName method) a delete method: this method is invoked when the Configuration Admin Service asks the ManagedServiceFactory to delete a specific service instance.

The registration of a ManagedServiceFactory follows the same steps of the ManagedService example:

Code Block

private ServiceRegistration factoryService;
public void start(BundleContext context) {
    Dictionary props = new Hashtable();
    props.put("service.pid", "test.smssenderfactory");
    factoryService = context.registerService(ManagedServiceFactory.class.getName(),
        new SmsSenderFactory(), props);
}
public void stop(BundleContext context) {
    if (factoryService != null) {
        factoryService.unregister();
        factoryService = null;
    }
}
...

Finally, using the ConfigurationAdmin interface, it is possible to send new or updated configuration dictionaries to the newly created ManagedServiceFactory:

Code Block

public class Activator implements BundleActivator 
{   
    private List configurationList = new ArrayList();  
	 
    public void start(BundleContext bundleContext) throws Exception 
    {  
        ServiceReference configurationAdminReference = 
            bundleContext.getServiceReference(ConfigurationAdmin.class.getName());  
              
        if (configurationAdminReference != null) 
        {  
            ConfigurationAdmin confAdmin = (ConfigurationAdmin) bundleContext.getService(configurationAdminReference);  
              
            Configuration configuration = confAdmin.createFactoryConfiguration("test.smssenderfactory", null);  
            Dictionary properties = createServiceProperties();
            configuration.update(properties);  
              
            //Store in the configurationList the configuration object, the dictionary object
            //or configuration.getPid()  for future use  
            configurationList.add(configuration);  
        }  
    }   
}  

Apache Felix Implementation Details

...