Programatically push configuration into OSGi ConfigurationAdmin


Yesterday I lost half an hour when I struggled to push a configuration into OSGi ConfigurationAdmin. The org.osgi.service.cm.ManagedService was in fact called, but the Dictionary of properties was always null. It turns out, I forgot that one has to lookup the org.osgi.service.cm.Configuration not just by service PID, but also by (bundle) location. And since the location was not matching, the configuration was not a valid config for the bundle that implements the ManagedService.
Below is how one correctly configures the ManagedService registered with PID “my.ManagedService.Service.Pid” (service.pid). Notice the “?” location wildcard!
String servicePID = "my.ManagedService.Service.Pid";
Configuration configuration = 
	cm.getConfiguration(servicePID, "?");
Dictionary<String, Object> properties = 
	configuration.getProperties();
if (properties == null) {
	properties = new Hashtable<String, Object>();
}
properties.put("someKey", "someValue");
configuration.update(properties);

One Response to Programatically push configuration into OSGi ConfigurationAdmin »»


Comments

  1. Comment by Rüdiger Herrmann | 2013/10/25 at 19:44:03

    Thanks for posting this! It reminded me of a feature in File Install that helps to configure Managed Services

    Cheers
    Rüdiger


Leave a Reply »»