Karaf 向现有配置文件添加附加属性 [英] Karaf add additional property to existing config file

查看:34
本文介绍了Karaf 向现有配置文件添加附加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a bundle which uses a configuration file org.jemz.karaf.tutorial.hello.service.config.cfg with one property:

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

我使用 ConfigAdmin 的蓝图是这样的:

My blueprint for using ConfigAdmin is like:

<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" />

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

This works fine as long as I can change the value of the property and the bundle automatically updates the property.

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

I am wondering if there's any way of adding a new property to my config file without having to change the blueprint (which involves compile/package again).Of course my bundle should be ready to handle new properties.

不确定这在 OSGi 中是否有意义.谁能给我一个关于如何将新属性动态添加到现有配置文件并使其在 ConfigAdmin 中可用的提示?

Not sure if this makes sense in OSGi. Can anyone give me a hint of how to dynamically add new properties to an existing configuration file and make them available in ConfigAdmin?

推荐答案

@yodamad 向我展示了我的 ConfigurationAdmin 服务中的属性正在更新,但不幸的是我的 Bundel 没有收到新的属性,因为在我刚刚使用的 bean 定义中固定属性.

@yodamad showed me that properties were being updated in my ConfigurationAdmin service, but unfortunately my bundel was not receiving the new properties because in the bean definition I was just using a fixed property.

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

Finally in order to get new properties from config files I have changed my blueprint definition as follows:

<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 服务的引用,所以现在我可以检查我的包的所有属性:

I have a reference to the ConfigurationAdmin service so, now I can check all properties for my bundle as:

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 服务时,我都可以获得更新.

As my 'update-strategy' is reload I can get updated whenever a new ConfigurationAdmin service is registered.

欢迎对我的方法提出任何意见!

Any comment about my approach is welcome!

这篇关于Karaf 向现有配置文件添加附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆