Search code examples
osgiaemapache-felix

Osgi Config ManagedService Dictionary keys case insensitive


I am running into an issue. I have a org.osgi.service.cm.ManagedService impl client for an osgi configuration. The configuration is a collection of key-value pairs. The keys in these properties, when passed to the updated method(ManagedService.updated) of the ManagedService as a java.util.Dictionary object seem to be case insensitive i.e. props.get("HellO") works even though the key in the config is "Hello".

When I convert that dictionary to a Hashmap by iterating over its entries, the keys in the map become case sensitive as expected. Is this expected that the keys in Dictionary are case insensitive?

This was tested on an AEM 6.2 instance.

Here is my ManagedService impl class.

public class ConfigService implements ManagedService {

public void updated(final Dictionary props) throws ConfigurationException {
    // props.get("HellO") returns value
    if (props != null) {
        String pid = (String) props.get(Constants.SERVICE_PID);
        // convert to map
        Map map = map(props);
        // map.get("HellO") returns null
        // map.get("Hello") returns value
    }
}

private static Map map(Dictionary dict) {
    Map map = new ConcurrentHashMap();
    for (Enumeration keys = dict.keys(); keys.hasMoreElements();) {
        Object key = keys.nextElement();
        map.put(key, dict.get(key));
    }
    return map;
}

The ManagedService impl is registered as a service using the below code.

final Dictionary props = new Hashtable();
props.put(Constants.SERVICE_PID, "pid.of.the.osgi.configuration" );
ServiceRegistration configSvc = context.registerService(ManagedService.class.getName(),
                new ConfigService(), props);

Solution

  • This is as per the Configuration Admin Service specification.

    The name or key of a property must always be a String object, and is not case-sensitive during look up, but must preserve the original case.

    Apache felix source code for reference. https://github.com/apache/felix/blob/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/CaseInsensitiveDictionary.java

    /** * The CaseInsensitiveDictionary is a * java.util.Dictionary which conforms to the requirements laid * out by the Configuration Admin Service Specification requiring the property * names to keep case but to ignore case when accessing the properties. */