Fix Pack 8550

Coding the service to receive configuration properties

Configuration properties are available through the org.osgi.service.component.ComponentContext object that is provided on the activation method.

Before you begin

You must complete the task described in Associating a service with a persisted identity.

About this task

If properties are updated after activation has occurred, the method used for injection depends on the context that the service provides in its OSGi Declarative Services (DS) declaration.

Generally, it is best to declare a method that is to be used specifically for injection of updated properties by using the modified attribute on the service declaration. If a modified method is not available, DS deactivates and then reactivates the service with the new properties.

Deactivating and then activating a service can also cause dependent services to be recycled, and should be avoided unless specifically required. Using the modified attribute is the preferred way to receive configuration updates.

Example

In the previous task, Associating a service with a persisted identity, you defined a service to DS. The following are examples of activate and modified methods from the DS declaration described in that task.
private static Dictionary<String, Object> _props = null;

    protected void activate(ComponentContext cc) {
        _props = cc.getProperties();
    }

    protected void modified(Map<?, ?> newProperties) {
        if (newProperties instanceof Dictionary) {
            _props = (Dictionary<String, Object>) newProperties;
        } else {
            _props = new Hashtable(newProperties);
        }
    }
When you get values from the configuration properties, use the following mechanisms to allow some flexibility:
  • Code the methods to expect at least the default properties that are included in the same bundle, but to make allowances for user overrides, so that migration of user configuration is not necessary.
  • Ignore redundant or unrecognized properties.
The service must be able to operate on the default configuration alone. To provide a reasonable level of function, user overrides must not be mandatory.

What to do next

Provide descriptions and default values for configuration metadata

Icon that indicates the type of topic Task topic

Terms and conditions for information centers | Feedback


Timestamp icon Last updated: Wednesday, 22 May 2013
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-nd-mp&topic=twlp_setup_service_getprops
File name: twlp_setup_service_getprops.html