+

Search Tips   |   Advanced Search

Coding the service to receive configuration properties

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

Complete the task described in Associate a service with a persisted identity.

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 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, Associate 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 we get values from the configuration properties, use the following mechanisms to allow some flexibility:

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


Parent topic: Enable a service to receive configuration data

Concepts:

  • Server configuration

    Tasks:

  • Associate a service with a persisted identity

    Reference:
    OSGi Service Platform Release 4 Version 4.2 Enterprise Specification