McM
mcm@noway.es
Contact me: noway.es
Contact me

OSGI Configuration Admin + Declarative Services

Introduction

Application configuration is really important when developing it. OSGI provides a configuration admin specfication which describes how to use it.
The OSGi Configuration Admin Service is defined in Chapter 104 of the OSGi Compendium Specification.

Here I will try to explain an example of how to use the configuration admin and declarative services all together. You can find more information in other tutorials http://www.noway.es/tutorials.

Configuration admin specification

As commented in this tutorial , there are multiple implementation of the configuration admin OSGI service.

Here equinox implementation will be used with PAX ConfMan

Note- ManagedService interface (OSGI compendium ):

    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…

PAX CONFMAN

To use it, we will use to download the configuration admin implementation located in the OPS4j web: http://repository.ops4j.org/maven2/org/ops4j/pax/confman/pax-confman-pro...

When running the platform, introduce a JVM argument:
bundles.configuration.location=

In the configuration directory, create two subdirectories: services and factories.
Within these directories, create a properties file for each PID you have (PID_name.properties):
In these example:

$ tree configuration/
configuration/
|-- factories
`-- services
    |-- provider.application.properties

This file, provider.application.properties, will be a standard properties file.

$ more provider.application.properties
hola prueba
ip 127.0.0.1
ip2 182.3.3.6
server 192.168.3.6
itra
eb bakb

Using declarative services

In the component file, you will need to declare that you are going to implements ManagedService class of the configuration admin service. The component declaration will be something like this:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component name="com.as.configuration.provider"
        immediate="true"
        xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.osgi.org/xmlns/scr/v1.0.0 http://www.osgi.org/xmlns/scr/v1.0.0/scr.xsd ">
        <implementation class="com.as.configuration.provider.Implementation"/>
<property name="service.pid" value="provider.application"/>
        <service>
                <provide interface="com.as.configuration.provider.Service" />
                <provide interface="org.osgi.service.cm.ManagedService" />
        </service>
</scr:component>

Pay attention to the properties tag

                <provide interface="org.osgi.service.cm.ManagedService" />

where it is printed the pid name using the property name service.pid

and the service implementation:

       <implementation class="com.as.configuration.provider.Implementation"/>

                <provide interface="org.osgi.service.cm.ManagedService" />

where it is said that the configuration admin service will be implemented in the class Implementation.

Implementation class

I will like to remark that here, you must override the following methods:

  • protected void activate(ComponentContext context)
  • protected void deactivate(ComponentContext context)
  • public void updated(Dictionary arg0) throws ConfigurationException

Target Platform

Framework is launched.

id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.5.1.R35x_v20090827
1 ACTIVE      org.ops4j.pax.logging.pax-logging-service_1.4.0
2 ACTIVE      org.apache.commons.logging_1.0.4.v200904062259
3 ACTIVE      org.eclipse.equinox.cm_1.0.100.v20090520-1800
4 ACTIVE      org.apache.felix.scr_1.0.6
5 ACTIVE      org.ops4j.pax.logging.pax-logging-api_1.4.0
6 ACTIVE      com.as.configuration.provider_1.0.0.SNAPSHOT
7 ACTIVE      org.eclipse.osgi.services_3.2.0.v20090520-1800
8 ACTIVE      org.ops4j.pax.configmanager_0.2.2

Declarative sercie bundle: 4 ACTIVE org.apache.felix.scr_1.0.6
Config admin bundles:

  • 8 ACTIVE org.ops4j.pax.configmanager_0.2.2
  • 3 ACTIVE org.eclipse.equinox.cm_1.0.100.v20090520-1800

Binaries and source

You can download all the used bundles and the dummy source code of the configuration bundle here

There, you will find a script, start.sh that starts up the equinox platform.
First you will need to set up the path to the directory where you configuration will go. This path setup is found in the config.ini file.

be free to ask or report any problems!

Share it!

Twitter Gmail Delicious Google Bookmarks Hotmail Yahoo Mail Share/Bookmark