Karaf将其他属性添加到现有配置文件 [英] Karaf add additional property to existing config file

查看:190
本文介绍了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天全站免登陆