The OSGi Look

There’s life beyond JavaEE

Configuration Admin Service explained: the ManagedService interface

leave a comment »

As chapter 104.1 of the OSGi compendium specification states:

The Configuration Admin service is an important aspect of the deployment
of an OSGi Service Platform. It allows an Operator to set the configuration
information of deployed bundles.
Configuration is the process of defining the configuration data of bundles
and assuring that those bundles receive that data when they are active in the
OSGi Service Platform.

In simple words, the Configuration Admin service provides a simple way to

  • Write services that can be dynamically configured
  • Programmatically configure services

Write a dynamically configurable service

The simplest way to write a service able to receive new configurations at runtime is to implement the ManagedService interface, like in the example:

public class SmsService implements ManagedService
{
        //... sms service implementation here

	/**
	 * This method receives new Configuration Dictionaries
	 */
	public void updated(Dictionary properties) throws ConfigurationException 
	{
		//configure here your service
	}
}

The ManagedService is notified every time the Configuration Admin Service push a new configuration dictionary. In this case, the method update is called, giving the service the opportunity to re-configure itself.

To make the OSGi container aware of our SmsService, it is important to register it as a ManagedService like in the example:

public class SmsServiceActivator implements BundleActivator
{
	ServiceRegistration registration;
	
	public void start(BundleContext context) throws Exception 
	{
		Dictionary properties = new Hashtable();
		properties.put("service.pid", "mySmsService");
		registration = context.registerService(ManagedService.class.getName(), 
                         new SmsService(), properties);
	}
	
	public void stop(BundleContext context) throws Exception 
	{
		if (registration!=null)
		{
			registration.unregister();
			registration = null;
		}
	}
}

Notice that the ManagedService is registered with a service.pid parameter; that’s the reference name we will need to use to configure that particular name.

Configure a service using the Configuration Admin Service

Once the SmsService is implemented, registered and deployed, it can be configured at runtime using a few lines of code:

ServiceReference caRef = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());  

if (caRef != null) 
{  
    ConfigurationAdmin configAdmin = (ConfigurationAdmin)  bundleContext.getService(caRef);  
    Configuration config = configAdmin.getConfiguration("mySmsService");

    Dictionary props = config.getProperties();
    if (props == null) 
    {
       props = new Hashtable();
    }
   // configure the Dictionary
   props.put("key", "value");

   //push the configuration dictionary to the SmsService
   config.update(props);
}

The code displayed above retrieves the ConfigurationAdmin service available in the OSGi platform, and invoke its getConfiguration method passing the service.pid of the service to be configured. The result of this invocation is a Configuration object, that expose the configuration Dictionary via the getProperties method.
After applying all the needed modifications to the configuration Dictionary, the update method pushes the configuration Dictionary to the ManagedService (Sms Service in our example)

Advertisements

Written by fdiotalevi

March 22, 2009 at 8:16 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: