【问题标题】:Osgi Config ManagedService Dictionary keys case insensitiveOsgi Config ManagedService 字典键不区分大小写
【发布时间】:2018-01-26 10:07:36
【问题描述】:

我遇到了一个问题。我有一个用于 osgi 配置的 org.osgi.service.cm.ManagedService impl 客户端。配置是键值对的集合。 这些属性中的键,当作为java.util.Dictionary 对象传递给ManagedService 的更新方法(ManagedService.updated) 时似乎不区分大小写,即props.get("HellO") 有效,即使配置中的键是“Hello”。

当我通过迭代其条目将该字典转换为Hashmap 时,映射中的键会按预期区分大小写。这是否期望Dictionary 中的键不区分大小写?

这是在 AEM 6.2 实例上测试的。

这是我的ManagedService impl 课程。

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;
}

ManagedService impl 使用以下代码注册为服务。

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);

【问题讨论】:

    标签: osgi aem apache-felix


    【解决方案1】:

    这符合配置管理服务规范。

    属性的名称或键必须始终是 String 对象,而不是 查找时区分大小写,但必须保留原始大小写。

    Apache felix 源代码供参考。 https://github.com/apache/felix/blob/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/CaseInsensitiveDictionary.java

    /** * CaseInsensitiveDictionary 是一个 * java.util.Dictionary 符合规定的要求 * 由需要该属性的配置管理服务规范提出 * 名称保持大小写,但在访问属性时忽略大小写。 */

    【讨论】:

    • 密钥不区分大小写的原因是在当时(2000 年),X.500 规范看起来很重要。 X.509 证书、SNMP 和 LDAP 是我猜剩下的几样东西。区分大小写成为标准,我们陷入了错误的决定。不遗余力地向后兼容的标准的缺点之一。
    猜你喜欢
    • 2012-12-09
    • 2020-02-18
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多