felix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pierre De Rop <pierre.de_...@alcatel-lucent.fr>
Subject Re: declarative services and configuration admin
Date Fri, 18 Jul 2008 21:24:11 GMT
Hi Craig;

Here is a sample code showing how to configure a declarative service 
component, using the ConfigurationAdmin service.
Hope this sample code will help you.

(But alternatively, you could have a look to the fileinstaller (made by 
Peter Kriens) which is able to activate bundles and map associated 
property files to the config admin service: see 

1/ First, here is a simple component which displays its configuration, 
once activated:

public class MyComponent {
  protected void activate(ComponentContext context) {
    System.out.println("MyComponent activated");
    Enumeration e = context.getProperties().keys();
    while (e.hasMoreElements()) {
      String key = e.nextElement().toString();

-> the properties can be configured in SCR.xml, but also using the 
ConfigurationAdmin service.
Here here the corresponding SCR.xml:

<component name="MyComponent">
  <implementation class="configurable.MyComponent"/>
  <property name="vendor" value="Alcatel-Lucent"/>

SCR will actually register a ManagedService on behald of the 
"MyComponent" with a PID equals to the component name (that is: 

2/ Now, here is a sample code which relies on the ConfigurationAdmin 
service in order to configure the MyComponent service:

public class Configurator {
  private ConfigurationAdmin _cm;

  protected void bind(ConfigurationAdmin cm) { // we need that service 
in order to provide config ...
    _cm = cm;

  protected void activate(ComponentContext compctx) {
    Runnable r = new Runnable() {
    public void run() {
      try {
        Properties p = new Properties();
        p.load(new FileInputStream("/tmp/MyComponent.properties")); // 
you could load all properties found from a specific directory ...

        Configuration configuration = 
_cm.getConfiguration("MyComponent", null);

        p.setProperty("foo", "bar2");
        configuration.update(p); // This update will actually restart 
(REACTIVATE) the "MyComponent" service
      } catch (Throwable t) {

    Thread t = new Thread(r);
    t.start(); // we must not block the SCR thread ...

and here is the corresponding SCR.xml:

<component name="Configurator">
  <implementation class="configurator.Configurator"/>
  <reference name="cm" interface="org.osgi.service.cm.ConfigurationAdmin"

As you will see, when the Configurator updates the conf (after the short 
2 seconds delay), the SCR reactivates the "MyComponent" !
If you would like more information about this, you can refer to 

Kind Regards

View raw message