Karaf为现有配置文件添加其他属性

时间:2015-10-09 16:30:05

标签: osgi apache-karaf karaf blueprint-osgi

我有一个捆绑包,它使用配置文件org.jemz.karaf.tutorial.hello.service.config.cfg和一个属性:

org.jemz.karaf.tutorial.hello.service.msg="I am a HelloServiceConfig!!"

我使用ConfigAdmin的蓝图如下:

<cm:property-placeholder persistent-id="org.jemz.karaf.tutorial.hello.service.config" update-strategy="reload" >
    <cm:default-properties>
        <cm:property name="org.jemz.karaf.tutorial.hello.service.msg" value="Hello World!"/>
</cm:default-properties>
</cm:property-placeholder>



<bean   id="hello-service-config"
        class="org.jemz.karaf.tutorial.hello.service.config.internal.HelloServiceConfig"
        init-method="startup"
        destroy-method="shutdown">

    <property name="helloServiceConfiguration">
        <props>
              <prop key="org.jemz.karaf.tutorial.hello.service.msg" value="${org.jemz.karaf.tutorial.hello.service.msg}"/>
        </props>
    </property>
</bean>

<service ref="hello-service-config" interface="org.jemz.karaf.tutorial.hello.service.IHelloService" />

只要我可以更改属性的值并且bundle自动更新属性,这样就可以正常工作。

我想知道是否有任何方法可以在我的配置文件中添加新属性而无需更改蓝图(这又涉及编译/包)。当然,我的包应该已准备好处理新属性。

不确定这在OSGi中是否有意义。任何人都可以给我一个如何动态地将新属性添加到现有配置文件并在ConfigAdmin中使用它们的提示吗?

2 个答案:

答案 0 :(得分:1)

您可以在karaf shell中添加属性:

1 / config:edit org.jemz.karaf.tutorial.hello.service.config

2 / config:propset <key> <value>

3 / finally config:update将更改保存到文件。

如果您想以编程方式执行此操作,请检查karaf源以查看实现

答案 1 :(得分:0)

@yodamad告诉我,我的ConfigurationAdmin服务正在更新属性,但不幸的是我的bundel没有收到新属性,因为在bean定义中我只是使用固定属性。

最后,为了从配置文件中获取新属性,我更改了我的蓝图定义,如下所示:

<cm:property-placeholder persistent-id="org.jemz.karaf.tutorial.hello.service.config" update-strategy="reload" >
</cm:property-placeholder>

<bean   id="hello-service-config"
        class="org.jemz.karaf.tutorial.hello.service.config.internal.HelloServiceConfig"
        init-method="startup"
        destroy-method="shutdown">
</bean>

<service ref="hello-service-config" interface="org.jemz.karaf.tutorial.hello.service.IHelloService" />


<reference id="hello-service-config-admin" interface="org.osgi.service.cm.ConfigurationAdmin"
           availability="optional">
    <reference-listener bind-method="setConfigAdmin"
                        unbind-method="unsetConfigAdmin">
        <ref component-id="hello-service-config"/>
    </reference-listener>
</reference>

我有一个对ConfigurationAdmin服务的引用,所以现在我可以检查我的bundle的所有属性:

public void setConfigAdmin(ConfigurationAdmin cfgAdmin) {
    this.cfgAdmin = cfgAdmin;
    try {
        Configuration cfg =   cfgAdmin.getConfiguration("org.jemz.karaf.tutorial.hello.service.config");
        Dictionary<String, Object> properties = cfg.getProperties();
        Enumeration<String> en = properties.keys();
        while(en.hasMoreElements()) {
            String key = en.nextElement();

            System.out.println("KEY: " + key + " VAL: " + properties.get(key));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

由于我的'更新策略'为reload,每当注册新的ConfigurationAdmin服务时,我都会得到更新。

欢迎任何关于我的方法的评论!

相关问题