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
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 must be avoided unless specifically required. Using the modified attribute is the preferred way to receive configuration updates.
Example
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);
}
}
- 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.