Programmatically updating OSGi configurations in AEM and Sling

Adobe Experience Manager | AEM/CQ | Apache Sling

Programmatically updating OSGi configurations in AEM and Sling

OSGi components can be configured via the concept of Managed Services and Managed Service Factories. Each component has a Service PID. Once a configuration has been updated, the changes are persisted to the file system. In Adobe Experience Manager you can find the files under the crx-quickstart/launchpad/config folder. If you're using the Sling jar, you can find the files under the sling/config folder.

Hopefully you're following best practices and creating sling:OsgiConfig files so that configurations can be stored in your version control system and configured for specific server instances assigned by Sling run modes. When you want to manually update an OSGi configuration you do so through the Apache Felix console's Configuration screen located at http://localhost:4502/system/console/configMgr.

You can programmatically read and update OSGi configurations through the Apache Felix provided ConfigurationAdmin service. In fact, this is the same service used by the Apache Felix console. When you look at the Configuration console it tells you the status of the ConfigurationAdmin service. If you disable the Apache Felix Configuration Admin Service (org.apache.felix.configadmin) you will notice that you are no longer able to update configurations from the Felix console.

Using the service is simple and straightforward. However, the one thing to watch out for is that updating the configuration though ConfigurationAdmin is an asynchronous call and the component associated with the configuration will become unavailable for a brief moment on update. That means trying to make rapid consecutive updates or trying to read from the configuration directly after an update is unreliable unless properly handled.

The following example demonstrates how to get a Configuration from a Managed Service as well as a Managed Service Factory, how to get properties from the configuration and how to update the configuration.

The files below are a service I created for the Publick Sling + Sightly Blog Engine to manage configurations easily and consistently throughout the application as well as a sample consumer of the service.

Further reading:

Apache Felix Configuration Admin Service