Most applications require configuration settings. Applications are often deployed in multiple environments (dev, test, prod, etc) and these environments often require different runtime configurations. Also, it’s useful to be able to update an application configuration without a restart. If you find yourself with requirements like these for your Eclipse RCP application, you should really check out the OSGi Configuration Admin service.
Specifically, the Configuration Admin service allows you to:
- Configure the OSGi services that you provide as part of your application.
- Configure OSGi services that others provide. An example of this is the ability to configure the Pax Logging service, which I’ll explore in a second post.
- Configure Spring beans when using Spring DM. Here is a good reference for using Config Admin with Spring DM.
If you’d like to learn more about the Configuration Admin service, check out this post or the OSGi Compendium specification. In this post I’m going to describe how to set up the Configuration Admin service in an RCP application using Pax ConfMan.
What is Pax ConfMan?
The Configuration Admin service doesn’t specify any particular way of storing and accessing properties. Each implementation can do what it likes, and Pax ConfMan has opted for the simplest approach – properties files.
Setting up the target platform
The first step in getting Pax ConfMan working in your RCP application is to add the required bundles to your target platform. Here is the complete list of bundles which need to be added:
- org.eclipse.equinox.cm (download)
- org.eclipse.osgi.services (download)
- org.ops4j.pax.confman.pax-confman-propsloader (download)
- org.ops4j.pax.logging.pax-logging-api (download)
- org.ops4j.pax.logging.pax-logging-service (download)
Also make sure to add these bundles to your product configuration and run configuration so they will be included in builds and at runtime.
Forcing Pax ConfMan to start
If you run your RCP application at this point and examine the running bundles in an OSGi console, you’ll notice that the bundles added above are not started. To make the service available at runtime, we need to force them to start using the Configuration page (new in Galileo) of the Product Configuration Editor.
This page allows us to specify that specific bundles should be automatically started when the application is launched. Note that adding bundles to this page currently wipes out necessary RCP bundle settings. After adding these back in, your config page should look like this:
Now if you run your RCP application, you should see Pax ConfMan logging output to your console. These logs will tell you that the configuration folder cannot be found. So on to the next step!
Adding a configuration folder
Pax ConfMan allows you to configure your OSGi application using standard Java properties files, so we need to create a directory to hold these files. To get started, create a folder called confadmin
in one of plug-ins that is part of your RCP application. You can call this folder anything you like, and the ConfMan default is configurations
. I think confadmin
is a better name in that it distinguishes it from the configuration
folder used by the OSGi framework.
Inside the confadmin
folder, create two sub-folders called services
and factories
. These folder names are required.
Finally, you need to tell Pax ConfMan where your folder is located, and this is done using a Java system property called bundles.configuration.location
.
When running inside the Eclipse IDE, the simplest way to do this is to add an argument to your run configuration. Mine looks like this:
Now when you run the application you should see ConfMan messages indicating that your configuration folder has been located. Of course we haven’t added any properties files yet, but the wiring is all in place.
How to handle deployment
There are a variety of ways to handle configuration files in a deployed RCP application:
- Place your configuration files in a separate bundle and deploy that bundle as a folder instead of a jar file.
- Add your configuration folder to the install directory by utilizing the
root
property in the build.properties file for a feature. - Use an installer script to copy the configuration files into another directory on a users machine.
No matter which option you choose, you’ll need to provide a bundles.configuration.location
VM argument that points to the configuration directory.
What’s next?
Well you can now start to configure your own OSGi services using Pax ConfMan. In my next post I’ll show how to use ConfMan to configure Pax Logging in an RCP application.