A command line client for the Configuration Admin Service

You can find some introductory articles on the OSGi Configuration Admin Service here and here

In our introductory articles on the OSGi Configuration Admin Service, we explained how to programmatically access to the OSGi ConfigurationAdmin interface to send configuration dictionaries to bundles installed in your OSGi framework.
Of course that’s just one possible usage scenario of the Configuration Admin Service. In many cases, however, you want to give your customer (or the deployment team) the opportunity to configure your system directly, and writing some Java code is not the most practical solution. In these cases, there are some tools that make it easy to use the Configuration Admin Service.

The one we are talking about today is an handy extension of the Apache Felix (or Equinox) console developed by Luminis. Let’s see how we can use it to configure the Apache Felix HTTP service.

First of all we need to start up Apache Felix. If you have downloaded the main Felix distribution, you need to type

java -jar bin/felix.jar

to start the OSGi container.

If you prefer using Pax-Runner, you’ll just type

pax-run.sh

In both cases, the usual Apache Felix console will be displayed

Welcome to Felix.
=================

->

Now you need to install two bundles

Installing them is straightforward, just type the following commands in the console:

-> install http://mirrors.dedipower.com/ftp.apache.org/felix/org.apache.felix.configadmin-1.0.10.jar
Bundle ID: 4
-> install https://opensource.luminis.net/wiki/download/attachments/2031635/net.luminis.cmc-0.2.3.jar?version=1&modificationDate=1240778068150
Bundle ID: 5
-> start 4 5
-> ps
START LEVEL 6
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (1.8.1)
[   1] [Active     ] [    1] osgi.compendium (4.1.0.build-200702212030)
[   2] [Active     ] [    1] Apache Felix Shell Service (1.2.0)
[   3] [Active     ] [    1] Apache Felix Shell TUI (1.2.0)
[   4] [Active     ] [    5] Apache Felix Configuration Admin Service (1.0.10)
[   5] [Active     ] [    5] luminis Configuration Admin command line client (0.2.3)

With the two additional bundles installed, we have added several new commands to the Apache Felix shell. Let’s see them typing cm help

-> cm help
Usage:
 cm help                  print this help message
 cm list                  list all known configurations
 cm get <pid>             show configuration for service <pid>
 cm getv <pid>            verbose get (shows value types also)
 cm put <pid> key value   set string value for service <pid>
 cm puts <pid> key value  set "simple" value for service <pid>: value is "true", "false",
                          a char in single quotes, an int, or a number, with appended:
                          i (Integer), l (Long), f (Float), d (Double), b (Byte), s (Short)
 cm del <pid>             deletes configuration for service <pid>

The help is pretty self-explaining, but let’s proceed with a simple example installing the Apache Felix HTTP service. This Felix subproject implements the OSGi HTTP Service specification, and embeds a Jetty servlet container. This internal Jetty server can be configured using the OSGi Configuration Admin Service.

Let’s start by installing the Apache Felix HTTP service.

-> install http://mirror.lividpenguin.com/pub/apache/felix/org.apache.felix.http.jetty-1.0.1.jar
Bundle ID: 6

The web page of this bundle lists all the configurations that you can set; for instance, you can use the org.osgi.service.http.port property to set the HTTP port Jetty is listening to.

To do that, first of all you need to create a configuration instance for the Apache Felix HTTP bundle, set the property and then start the Jetty bundle (in our case it’s bundle ID 6)

-> cm create org.apache.felix.http
-> cm put org.apache.felix.http org.osgi.service.http.port 8888
-> start 6

If everything is ok, you should see the Jetty bundle starting; now try to connect to http://localhost:8888 and see the nice 404 page! If you see that, you have successfully configured your bundle; the 404 error is normal, since you haven’t installed any web application yet.

Of course you can now set additional properties, or even change the one just set. New configurations will be read on the fly, and the Jetty instance updated accordingly.
In every moment, you can see the list of the existing configurations typing cm list

-> cm list
Configuration list:
org.apache.felix.http     http://mirror.lividpenguin.com/pub/apache/felix/org.apache.felix.http.jetty-1.0.1.jar

or display the list of properties for a given configuration typing cm get [service pid]

-> cm get org.apache.felix.http

Configuration for service (pid) "org.apache.felix.http"
(bundle location = http://mirror.lividpenguin.com/pub/apache/felix/org.apache.felix.http.jetty-1.0.1.jar)

key                          value
------                       ------
service.pid                  org.apache.felix.http
org.osgi.service.http.port   8888
osgi, tutorial

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

2 Responses to “A command line client for the Configuration Admin Service”

Leave Comment

(required)

(required)