Hi everyone, good morning...
 
Thanks for the shoulder / snippets and references...  
 
I'll try the below probably later this afternoon, if I can get the chance... 
unfortunately, I sort of am multi-tasking and I can't get to working on these 
things dedicated, although I sure would like to...
 
Thanks again, Craig Phillips, Praxis Engineering

________________________________

From: Pierre De Rop [mailto:[EMAIL PROTECTED]
Sent: Fri 7/18/2008 5:24 PM
To: dev@felix.apache.org
Subject: Re: declarative services and configuration admin



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
http://www.aqute.biz/Code/FileInstall)

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");
    System.out.println("properties:");
    Enumeration e = context.getProperties().keys();
    while (e.hasMoreElements()) {
      String key = e.nextElement().toString();
     
System.out.println(key+"="+context.getProperties().get(key).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"/>
</component>

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


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);
        configuration.update(p);

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

    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"
    bind="bind"/>
</component>


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
http://www.mail-archive.com/[EMAIL PROTECTED]/msg01120.html

Kind Regards
/Pierre





Reply via email to