Snippets / GetConfiguration aQute - Software Consultancy
Search
*

Get Configuration Data with a Managed Service

Purpose

Show how a bundle can receive configuration data with a Managed Service.

Prerequisites

  • Configuration Management - org.osgi.service.cm (The Knopflerfish implementation unfortunately requires some Knopflerfish specific packages)
  • osgi interfaces, provides all service interfaces for the OSGi specifications.

Instructions

Configuration data is received by registering a Managed Service. This service receives configuration data when detected by the Configuration Admin service. The configuration data is derived from the service.pid property; this string property must have a unique name.

In this example, we have a POJO that runs a motto of the day server. The default port of 6123 can be changed as well as the motto. The Configuration Admin always calls the update() method, we therefore wait with creating a the server until we get the first configuration.

The source code looks as follows:

  Source: aQute.getconfiguration.GetConfiguration

  package aQute.getconfiguration;
  import java.util.*;
  import org.osgi.service.cm.*;

  public class GetConfiguration implements ManagedService {
    boolean       active;
    int           port;
    String        message;
    MottoOfTheDay motd;

The MottoOfTheDay class runs in a thread and it is therefore necessary to kill this thread when this component is deactivated.

    protected synchronized void deactivate() {
      if (motd != null) motd.kill();
    }

The following method is called by the Configuration Admin service. We parse the given dictionary with configuration data and update the motto of the day server accordingly.

    public synchronized void updated(Dictionary properties)
        throws ConfigurationException {
      if (properties != null) {
        try {
          message = (String) properties.get("motto");
          port = Integer.parseInt((String) properties
              .get("port"));
        } catch (Exception e) {
          // Ignore, use defaults
        }
      }
      if (motd != null) {
        motd.kill();
      }
      motd = new MottoOfTheDay();
      motd.setPort(port == 0 ? port = 6123 : port);
      motd.setMotto(message == null ? "No motto" : message);
      motd.start();
    }
  }

The next step is the Motto Of the Day class. This class implements a typical socket server.

  Source: aQute.getconfiguration.MottoOfTheDay

  package aQute.getconfiguration;
  import java.io.*;
  import java.net.*;

  public class MottoOfTheDay extends Thread {
    String       motto;
    int          port   = 0;
    ServerSocket server;
    boolean      active = true;

    public void setPort(int port) {
      this.port = port;
      interrupt();
    }

    public void setMotto(String message) {
      motto = message;
    }

    public void run() {
      while (active)
        try {
          server = new ServerSocket(port);
          while (active && !interrupted())
            try {
              Socket socket = server.accept();
              socket.getOutputStream()
                  .write(motto.getBytes());
              socket.close();
            } catch (IOException e) {
              // Ignore, not much we can do
            }
        } catch (IOException e) {
          try {
            Thread.sleep(2000);
            server.close();
          } catch (Exception ex) {
            // Ignore
          }
        }
    }

    public void kill() {
      active = false;
      try {
        server.close();
      } catch (IOException e) {
        // Ignore
      }
    }
  }

And the bnd file to build the bundle. Notice the properties that set the service.pid service property.

  Bnd file: aQute.getconfiguration.bnd

  Export-Package: aQute.getconfiguration
  Service-Component: aQute.getconfiguration.GetConfiguration; \ 
provide:=org.osgi.service.cm.ManagedService; \
properties:="service.pid=aQute.getconfiguration"

Links

Copyright 2006 aQute SARL, All Rights Reserved