Osgi Karaf将附加属性添加到现有配置文件

Osgi Karaf将附加属性添加到现有配置文件,osgi,apache-karaf,karaf,blueprint-osgi,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.co

我有一个包,它使用一个配置文件
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" />

只要我可以更改属性的值,并且捆绑包自动更新属性,这就可以正常工作

我想知道是否有任何方法可以在不必更改蓝图的情况下将新属性添加到配置文件中(需要再次编译/打包)。当然,我的包应该可以处理新属性了


不确定这在OSGi中是否有意义。有谁能给我一个提示,告诉我如何在现有配置文件中动态添加新属性,并使它们在ConfigAdmin中可用吗?

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

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

2/
config:propset

3/最后
config:update
将更改保存到文件


如果您想以编程方式执行,请检查karaf source以查看实现情况

@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服务的引用,因此,现在我可以检查捆绑包的所有属性,如下所示:

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();
    }
}
public void setConfigAdmin(ConfigurationAdmin cfgAdmin){
this.cfgAdmin=cfgAdmin;
试一试{
Configuration cfg=cfgAdmin.getConfiguration(“org.jemz.karaf.tutorial.hello.service.config”);
Dictionary properties=cfg.getProperties();
枚举en=properties.keys();
while(en.hasMoreElements()){
String key=en.nextElement();
System.out.println(“KEY:+KEY+”VAL:+properties.get(KEY));
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
由于我的“更新策略”是“重新加载”,因此每当注册新的ConfigurationAdmin服务时,我都可以得到更新


欢迎对我的方法发表任何评论

谢谢你的回答。您的解决方案有效,属性存储在属性占位符中,我可以列出它们。问题是我的bundle无法获得这些新属性。我认为这与bean定义有关,我只设置了一个要注入的属性“org.jemz,karaf.tutorial.service.msg”,因此在我的
setHelloServiceConfiguration
方法中无法获得更多的属性。我已经找到了从配置文件中获取新属性的另一种方法,我将在回答中发布它。无论如何,我会给你一个ideaHi Jorge的投票,如果你使用SpringDM,我可以给你一个配置示例,让你能够为一个bundle管理多个配置文件你也可以使用一个类来实现ConfigurationListener,BundleContextAware接口。然后,您将收到有关配置更改的所有事件,因此您可以处理应用程序中需要的任何内容