Configuration Admin Service
THe OSGi Componendium Configuration Admin Service specifies a service, which allows for easy management of configuration data for configurable components. Basicaly configuration is a list of name-value pairs. Configuration is managed by management applications by asking the Configuration Admin Service for such configuration. After updating the configuration, it is sent back to the Configuration Admin Service. The Configuration Admin Service is like a central hub, which cares for persisting this configuration and also for distributing the configuration to interested parties. One class of such parties are the components to be configured. These are registered as ManagedService
services. There is also a notion of ManagedServiceFactory
, which allows for multiple configurations of the same kind to be applied.
For more information, I suggest you read Chapter 104, Configuration Admin Service Specification, of the OSGi Compendium Services Specification book. IMHO this is worth reading.
For a starter this page sets out to describe how you can create a component, which is interested in some configuration. As such this page is at its very beginning just highlighting the simplest of all cases: A single component being interested in its configuration.
ManagedService
Example
Consider you have requirement for some configuration, say the line length of a pretty printer. You want to have this configurable through configuration admin.
You need the following parts:
- A service PID identifying the configuration
- A
ManagedService
to receive the configuration - Name(s) for the configuration property/ies
The PID is just a string, which must be globally unique. Assuming a simple case where your PrettyPrinter configurator receives the configuration has a unique class name, you may well use that name. So lets assume, our ManagedService
is called org.sample.PrettyPrinterConfigurator
and that name is also used as the PID. For more information on the Service PID, refer to Section 104.3, The Persistent Identity of the OSGi Compendium Services Specification.
The class would be:
package org.sample; class PrettyPrinterConfigurator implements ManagedService { public void update(Dictionary props) throws ConfigurationException { if (props == null) { // no configuration from configuration admin // or old configuration has been deleted } else { // apply configuration from config admin } } }
Now, in your bundle activator's start() method you register the PrettyPrinterConfigurator
as a ManagedService
:
... private ServiceRegistration ppcService; public void start(BundleContext context) { Dictionary props = new Hashtable(); props.put("service.pid", "org.sample.PrettyPrinterConfigurator"); ppcService = context.registerService(ManagedService.class.getName(), new PrettyPrinterConfigurator(), props); } public void stop(BundleContext context) { if (ppcService != null) { ppcService.unregister(); ppcService = null; } } ...
That's more or less it. You may now go on to use your favourite tool to create and edit the configuration for the PrettyPrinterConfigurator
, for example something like this:
Configuration config = configurationAdmin.getConfiguration( "org.sample.PrettyPrinterConfigurator"); Dictionary props = config.getProperties(); // if null, the configuration is new if (props == null) { props = new Hashtable(); } // set some properties props.put(..., ...); // update the configuration config.update(props);
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.