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.
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:
Now, in your bundle activator's start() method you register the PrettyPrinterConfigurator as a ManagedService:
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:
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.
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.
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:
Finally, using the ConfigurationAdmin interface, it is possible to send new or updated configuration dictionaries to the newly created ManagedServiceFactory:
The Apache Felix implementation of the Configuration Admin Service specification has a few specialities, which may be of interest when deploying it. These are described here.
The Apache Felix implementation is configurable with Framework properties. Here is a short table listing the properties. Please refer to the later sections for a description of these properties.
|felix.cm.loglevel||int||2||Logging level to use in the absence of an OSGi LogService. See the Logging section below.|
|felix.cm.dir||String||BundleContext.getDataFile("config")||Location of the Configuration Admin configuration files. See the Configuration Files section below.|
Logging goes to the OSGi LogService if such a service is registered int the OSGi framework. If no OSGi LogService is registered, the log output is directed to the Java platform standard error output (System.err).
To limit the output in the absence of an OSGi LogService, the felix.cm.loglevel framework property may be set to an integer value limiting the level of the log messages actually written: Only messages whose level is lower than or equal to the limit is actually written. All other messages are discarded.
The log levels correspond to the predefined log levels of the OSGi Log Service Specification as listed in the following table:
|Level Number||LogService Constant||Remark|
|1||LOG_ERROR||Used for error messages|
|2||LOG_WARNING||Used for warning messages. This is the default value of the felix.cm.loglevel property if it is not set or if the value cannot be converted to an integer.|
|3||LOG_INFO||Used for informational messages|
|4||LOG_DEBUG||Used for debug messages|
Note: The felix.cm.loglevel property is ignored if an OSGi LogService is actually used for logging because it is then the responsibility of the LogService to limit the actual log output.
By default the Apache Felix Configuration Admin Implementation stores the configuration data in the platform filesystem. The location of the configuration data can be configured with the felix.cm.dir framework property.
The resolution of the location using the felix.cm.dir and the BundleContext is implemented according to the following algorithm.
- If the felix.cm.dir property is not set, a directory named config is used inside the persistent storage area of the Apache Felix Configuration Admin Service bundle is used. This is the default behaviour.
- If the felix.cm.dir property is not set and the framework does not support persistent storage area for bundles in the filesystem, the config directory is used in the current working directory as specified in the user.dir system property is assumed.
- Otherwise the felix.cm.dir property value is used as the directory name to take the configuration data.
The result of these steps may be a relative file. In this case and if the framework provides access to persistent storage area, the directory name is resolved as being inside the persistent storage area. Otherwise the directory name is resolved to an absolute path calling the File.getAbsoluteFile() method.
If a non-directory file exists as the location found in the previous step or the named directory (including any parent directories) cannot be created, the configuration data cannot be stored in the filesystem. Generally this will result in failure to store configuration data at all, except if there is a org.apache.felix.cm.PersistenceManager service registered, which is then used.