Config Bundle

Config Bundle contains the implementation of the OSGi Configuration Admin Service. The Config Bundle adds more functionality to OSGi basics by supporting initial load of configurations before bundles are started, giving remote access to its services, and providing utilities for administration of applications wishing to modify configurations.

Contents:


Bundle Information

The Config Bundle uses packages from DB Bundle, ProSyst Util Bundle, OSGi Library Bundle and ProSyst Metatype Bundle. These bundles should be started in order to use the Config Bundle. The Config Bundle as well as the other four it depends on, are a part of the ProSyst base bundle configuration and they are active by default.

Bundle JAR

The JAR file of the Config Bundle is config.jar from the bundles directory.

Import

All packages imported by the Config Bundle are exported by bundles in the Framework Package:

Package Exporter Description
com.prosyst.mbs.framework.access System Bundle/
ProSyst Util Full Bundle
Contains the Framework Access service for faster search of bundles with specific manifest headers.
com.prosyst.mbs.framework.event Contains event utility for asynchronous notification of listeners.
com.prosyst.mbs.framework.resman Contains the resource manager, which the Config Bundle uses to switch to the resource context of the bundle, whose Managed Service's updated method is called, as well as to those of registered Configuration Plugins.
com.prosyst.mbs.services.db DB Bundle Provides a persistent database for storing bundle configurations.
com.prosyst.util.hash ProSyst Util Bundle/
ProSyst Util Full Bundle
Hosts hash table utilities.
com.prosyst.util.io System Bundle/
ProSyst Util Full Bundle
Provides abilities for input/output management.
com.prosyst.util.pcommands ProSyst Util Bundle /
ProSyst Util Full Bundle
Contains the API for plugging console commands.
com.prosyst.util.ref Holds the log reference utility for forwarding log messages to the system log.
com.prosyst.util.threadpool Contains the Thread Pool Manager for executing jobs by using reusable threads.
com.prosyst.util.timer Hosts the components of the Timer service.
org.mbs.services.metatype ProSyst Metatype Bundle Contains the ProSyst Metatype API used for definition of initial configuration data.
org.osgi.service.cm OSGi Library Bundle Contains the OSGi Configuration Admin interfaces for administering, processing and receiving configuration data.
org.osgi.service.event Contains the OSGi Event Admin service for posting and receiving events by using the OSGi whiteboard pattern.
org.osgi.service.metatype Shares interfaces through which an attribute can be described. They can also provide access to metatypes of miscellaneous resources.
org.osgi.util.tracker Contains the OSGi Service Tracker utility for enhanced tracking of services.

Export

The Config Bundle exports the com.prosyst.mbs.services.config.cm package, which contains a service for remote configuration management.


Principles of Work

The Configuration Admin, defined by the OSGi Configuration Admin Specification and implemented in the Config Bundle, delivers two main functions:

Configuration Management

Configuration is the process of assigning and providing configuration data to bundles, running in the OSGi framework. An application that wants to become configurable through the Configuration Admin, should register a Managed Service (org.osgi.service.cm.ManagedService) or Managed Service Factory (org.osgi.service.cm.ManagedServiceFactory) according to its needs.

The Configuration Admin provides management applications with access to exported configurations in the form of org.osgi.service.cm.Configuration objects.

The singleton configurations and factory configurations are stored persistently in the DB Bundle in a custom database called "CONFIG".

Exporting Configuration Properties

A service implementing ManagedService will receive a single set of properties when registered, or when a configuration object is created for it in the Configuration Admin, whichever happens later. It is also informed when its configuration object is changed in the Configuration Admin by a management application.

A service implementing ManagedServiceFactory will receive from 0 to n configuration sets when it registers in the OSGi framework. The factory is informed of updates and deletion of generated configuration objects in the Configuration Admin storage.

The following source is an example of a ManagedService implementation:

                   . . .
    
public class ConfigTest implements ManagedService, BundleActivator {
  ServiceRegistration reg;
  Hashtable configuration;
  
  public void start(BundleContext bc) {
    reg = bc.registerService(ManagedService.class.getName(), 
      this, getDefaults());
  }
  
  private Hashtable getDefaults() {
    Hashtable defaults = new Hashtable();
    defaults.put("port", "12345");
    defaults.put("product", "device");
    defaults.put("baud", "9600");
    defaults.put(Constants.SERVICE_PID, "com.acme.serialport.device");
    return defaults;
  }
  
  public synchronized void updated(Dictionary configuration) {
    if (configuration == null){
      reg.setProperties(getDefaults());
    } else {
      reg.setProperties(configuration);
    }
  }

  public void stop(BundleContext bc) {
  }
                    . . .
}
Listing 1: Using ManagedService interface

Remote Configuration Management

For the needs of remote management, a configuration holding real data is represented as a CMDictionary object, which extends com.prosyst.util.io.Externalizable and hence can be transported over PMP and stored in the DB Bundle. A CMDictionary stores properties with case sensitive keys.

To execute configuration management remotely over PMP, an application should use the com.prosyst.mbs.services.config.cm.CMRemoteManager service.

Metadata Management

Overview

The Config Bundle is responsible for loading a bundle's initial configuration data on the basis of metadata exported by the bundle. Management of configuration metadata uses the ProSyst Metatype API, which extends the OSGi Metatype API by allowing definition of object class instances for initial generation, of attribute and object class modifiers and properties, etc.

Configuration metatypes are handled by the ProSyst Metatype Bundle, which implements the OSGi Metatype Specification and supports the ProSyst Metatype API. The bundle's Metadata Manager processes available configuration metatypes and delivers the information to the Config Bundle.

Exporting a Configuration Metatype

Configuration metadata should be put in a Metatype Provider with ID equal to Managed Service's or Managed Service Factory's PID and with a "template" ObjectClassDefinition again with ID equal to the PID.

For a configuration factory, the Metatype Provider can also provide a configuration object in its metatype definition, which will be automatically generated and loaded in the Configuration Admin storage at bundle installation in the case of XML metadata and at bundle start in the case of Metatype Provider service.

A bundle developer has two options for providing configuration metadata - implement a service or write an XML.

Implement a Metatype Provider Service. In this case, a configuration metatype should be registered as an org.mbs.services.metatype.MetaTypeProviderExtern service.

To indicate that the Metatype Provider contains configuration metadata, it should be registered with the following String properties:

The ObjectClassDefinition, representing a configuration, should be an instance of the ObjectClassDefinitionEx interface and its attributes - AttributeDefinitionEx instances. To indicate if a configuration and its attributes should be loaded in the Configuration Admin storage, the ObjectClassDefinition should have a modifier (returned by the getModifier method) with key load and value true. Otherwise, the configuration should be explicitly created with the config.create console command or with the Defaults mConsole option for the corresponding metatype. Optional attributes can be loaded by default if their load modifier is true.

If the configuration is factory, the Metatype Provider can implement the getConfigurationObjects method so as to return ObjectClassDefinitionEx instances representing configuration objects to be automatically produced by the Config Bundle.

Write a metadata XML resource. XML metadata should be created in accordance with the ProSyst proprietary DTD or according to the metatype XML Schema from the OSGi Service Platform, Service Compendium.

A bundle, which provides its metadata in the ProSyst proprietary XML format, must use the Config (for ManagedService) or FactoryConfig (for ManagerServiceFactory) manifest header, or both. Their syntax is:

header := "Config"|"FactoryConfig"":"1*(, metatype_provider)
metatype_provider := (xml=xmlresource_path; pid=service_pid; name=display_name[; version=major.minior.micro])

For example:
Config: xml=myconfigs/myhttpconfig.xml; pid=my.http.pid; name=My HTTP Configuration

Following is an example of metadata XML for an HTTP server in the format introduced by ProSyst:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE metatype-provider SYSTEM "conf.dtd">
<metatype-provider>
<objectclass load="true">
<locale>en</locale>
<name>HTTP Server Configuration</name>
<id>my.http.fpid</id>
<description>HTTP Server Socket Configuration</description>
<attribute modifier="req">
<name>Port</name>
<id>port</id>
<description>Primary port on which client requests are accepted</description>
<type>&int;</type>
<cardinality>0</cardinality>
<key name="minValue" value="1"/>
<key name="maxValue" value="65535"/>
<value>
<scalar>80</scalar>
</value>
</attribute>
<attribute modifier="opt">
<name>Secondary Port</name>
<id>secondaryPort</id>
<description>Secondary port is opened if primary port is busy</description>
<type>&int;</type>
<cardinality>0</cardinality>
<key name="minValue" value="1"/>
<key name="maxValue" value="65535"/>
<value>
<scalar>8080</scalar>
</value>
</attribute>
<attribute modifier="req">
<name>Max Users</name>
<id>maxUsers</id>
<description>Maximum number of users simultaneously connected with http server</description>
<type>&int;</type>
<cardinality>0</cardinality>
<key name="minValue" value="0"/>
<key name="maxValue" value="50"/>
<value>
<scalar>50</scalar>
</value>
</attribute>
<attribute modifier="req">
<name>Request Timeout</name>
<id>requestTimeout</id>
<description>Timeout in seconds.
An inactive connection is closed after this timeout expiration.</description>
<type>&int;</type>
<cardinality>0</cardinality>
<key name="minValue" value="0"/>
<key name="maxValue" value="300"/>
<value>
<scalar>30</scalar>
</value>
</attribute>
<attribute modifier="req">
<name>Connection Type</name>
<id>connectionType</id>
<description>Type of connection: 1 for plain connection and 2 for secure connection.</Description>
<type>&int;</type>
<cardinality>0</cardinality>
<key name="minValue" value="1"/>
<key name="maxValue" value="2"/>
<value>
<scalar>1</scalar>
</value>
</attribute>
<attribute modifier="req">
<name>PersistentConnection</name>
<id>persistentConnection</id>
<description>Specifies if persistent connections should be managed</description>
<type>&boolean;</type>
<cardinality>0</cardinality>
<value>
<scalar>true</scalar>
</value>
</attribute>
</objectclass>
<configobject>
<id>mbs.http.fpid</id>
<attribute modifier="req">
<id>port</id>
<value>
<scalar>443</scalar>
</value>
</attribute>
<attribute modifier="req">
<id>connectionType</id>
<value>
<scalar>2</scalar>
</value>
</attribute>
</configobject>
</metatype-provider>
Listing 2.1: A configuration metatype written in the ProSyst XML format.

If the configuration metatype will be developed in compliant with the OSGi metatype XML schema, it should be available in the bundle JAR file in the OSGI-INF/metatype directory.

Listing 2.2 contains the same configuration metatype as 2.1 but this time defined according to the OSGi metatype XML schema.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <OCD name="HTTP Server Configuration" id="httpConfig"
       description="HTTP Server Socket Configuration">
<
AD name="Port" id="port" type="Integer" default="80" descrption="Primary port on which client requests are accepted" min="1" max="65535"/>
<
AD name="Secondary Port" id="secondaryPort" type="Integer" default="8080" description="Secondary port is opened if primary port is busy" min="1" max="65535" required="false"/>
<
AD name="Max Users" id="maxUsers" type="Integer" default="50" descrption="Maximum number of users simultaneously connected with http server" min="0" max="50"/>
<
AD name="Request Timeout" id="requestTimeout" type="Integer" default="30" descrption="Timeout in seconds. An inactive
connection is closed after this timeout expiration" min="0" max="300"/>
<
AD name="Connection Type" id="connectionType" type="Integer" default="1" descrption="Type of connection: 1 for plain connection and 2 for secure connection." min="1" max="2"/>
<
AD name="PersistentConnection" id="PersistentConnection" type="Boolean" default="true" descrption="Specifies if persistent connections
should be managed"/>
</
OCD> <Designate pid="my.http.pid_1" bundle="D:/mybundles/myconfig.jar" factory="my.http.fpid"> <Object ocdref="httpConfig"> <Attribute adref="port" content="446"/> <Attribute adref="connectionType" content="2"/> </Object> </Designate> </metatype:MetaData>
Listing 2.2: A configuration metatype following the OSGi metatype XML schema.

Loading Data in the Configuration Admin

When a bundle is installed in the OSGi framework, the ProSyst Metatype Bundle inspects the bundle for availability of configuration metadata. This is determined by looking for Config and FactoryConfig "service.pid" properties or headers in the bundle manifest. If there is a match, the ProSyst Metatype Bundle sends extracted metadata to the Config Bundle, which converts it into a new configuration instance in the following way:

Finally, the Configuration Admin passes the configuration data to the target bundle.

If the Configuration Admin already holds configurations with the same PID or factory PID, then the default configurations will not be loaded. This allows management entities to predefine configuration data that differs from the default one.

Note: The Config Bundle does not load configurations with duplicate PIDs and factory PIDs.

If a bundle is uninstalled, its configurations are deleted from the Configuration Admin. This is done in compliance with OSGi Configuration Admin Service Specification requirements for deletion of only those configurations which are tightly bound to a bundle.

When a bundle is updated, its configuration data is preserved in the configuration database in binary format. If the update includes changes in a configuration metatype, the Config Bundle will update the information in its database as well.


Services

Configuration Admin

The interface of the Configuration Admin service is org.osgi.service.cm.ConfigurationAdmin. This is a service for administering configuration data. The purpose of the Configuration Admin service is to provide management bundles with access to available configurations, represented as Configuration objects.

CM Remote Manager

The interface of the CM Remote Manager (com.prosyst.mbs.services.config.cm.CMRemoteManager) is designed for remote usage over PMP. It has functionality similar to org.osgi.service.cm.ConfigurationAdmin.

Pluggable Commands

The Config Bundle registers a com.prosyst.util.pcommands.PluggableCommands service that provides the console commands of the config group. Refer to the "Console Commands" section for description of the config command.


Console Commands

The following commands are available in the config group of the framework console:

Command Shortcut Description
list [options] [<bid>[ <bid>]]
ls
Returns all PIDs or/and FPIDs for the bundles in framework. When a bundle ID is given, only PIDs for that bundle are listed.
-p, -P Shows the pids and the properties of the matching configurations.
-f, -F Shows the PIDs of the configuration matching an LDAP filter.
getvalue [options] <pid> [prop_name[ prop_name]]
gv
Returns property value for the given PID and name.
-t, -T Lists all properties for the given PID with their type and current value.
-d, -D Lists all properties for the given PID with their full description.
-s, -S Lists the properties for the given PID sorted alphabetically by property key.
setvalue [options] [filter] <pid> [key val [key val]]
sv

Sets the property value for the given PID and property name with the specified value.

To set a property by using this pluggable command, the corresponding configuration containing this property must be in a Metatype Provider.

When setting value to an array, enclose the value in {} and separate the array elements with commas. When setting value to a Vector, enclose the value in [] and separate the Vector elements with commas.

-f, -F Use an LDAP filter to specify the range of configurations, whose property will be modified.
create [options] <pid>|<fpid> [location] [key val[ key vale]]
cr
Creates a new configuration for the given PID or FPID for the bundle with the given location. It is populated with default data unless new values are explicitly specified.
-f, -F Creates a new factory configuration.
-l, -L Indicates that a location parameter is passed.
-e, -E Creates a new empty configuration.
delete [options] [filter] [<pid>[ <pid>]
del
Deletes a configuration(s) with the specified PID(s).
-f, -F Deletes configurations according to an LDAP filter.
-z, -Z Deletes configurations, which have not been bound to a bundle since their last bundle-association was uninstalled. When using this option other options and parameters are ignored.
prinitial <pid>[ <pid>]
pr
Processes the initial data from the bundle configuration XML. The data previously associated with this PID is overwritten.
setlocation <pid> <location>[ <pid> <location] sl Sets the bundle location of one or more configurations with the given PIDs.
getlocation [<pid>[ <pid>]] gl Gets the locations of one or more configurations with given PIDs. When used without parameters, lists the locations of all available configurations.



System Properties

System Property Default Value Description
mbs.config.updateTimeout 10 Refers to the period in seconds the Config Bundle will wait for a Managed Service (Factory) to process its configuration. Configuration Manager update events can hang while calling a Managed Service or Managed Service Factory updated method. Specifies a time period in seconds during which CM Event Thread must return from an updated method. After this period passes, the Config Bundle stops the running thread and creates a new one to process the events. The minimum allowed value is 1 second.
mbs.config.debug false Turns on/off debugging to the Log Service or the system output.
mbs.config.console false Specifies that logged entries should be printed to the framework runtime console.
mbs.measurements.full false Turns on/off logging the startup time of Config Bundle's functional parts.
mbs.measurements.bundles


References


OSGi Bundles