Saving state of Virtual BUG

From BUG Wiki

Jump to: navigation, search

Upon unregistering a bundle from the OSGi Framework all configuration data that might have been set to define its state is gone. To relieve user of pains of reconfiguring the bundle once its active, Configuration Admin can be used to persist configuration data so that upon the bundle's being active again it's states can be set to those when they were before bundle was unregistered.


Contents

Get Configuration Admin Service:

ServiceReference serviceReference = context.getServiceReference(ConfigurationAdmin.class.getName());
ConfigurationAdmin configAdmin = (ConfigurationAdmin) context.getService(serviceReference);

or use a ServiceTracker in order to know when service will be available.


Get the Configuration:

Each Configuration contains a Dictionary that holds properties for service of interest, to initiate a new store or retrieve an existing one:

Configuration config = configAdmin.getConfiguration("com.buglabs.someService");


Retrieve properties for the service:

Dictionary properties = config.getProperties();


Update service with properties:

Dictionary prop = new Hashtable();
prop.put("enabled", new Boolean(true));

Refer to section 10.4.2 of OSGi R3 specifications for valid types that can be used in Dictionary.

config.update(properties);

Configuration's properties can also be cleared by executing

config.update(null);

This will clear all properties for this Configuration but leave Constants.SERVICE_PID intact since it identified id for this Configuration.


Complete specifications for OSGi including CM can be downloaded from http://www2.osgi.org/Specifications/HomePage.