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, <strong><span style="color: #ff0000;">"?"</span></strong>);
Dictionary<String, Object> properties = 
    configuration.getProperties();
if (properties == null) {
    properties = new Hashtable<String, Object>();
}
properties.put("someKey", "someValue");
configuration.update(properties);

Want to comment? Send me an email to blog-comments-2018 at lemmster d.t de and I'll paste it here (I won't publish your address). Why don't you use an external comment service like disqus, you ask? Well, I like to keep this site under my control, comments included. You can use markdown to format your comment.