3

I am using cm:property-placeholder in blueprint to load config file:

<!-- External configuration -->
<cm:property-placeholder persistent-id="mubyndle" update-strategy="reload">
    <cm:default-properties>
        <cm:property name="schemaValidation" value="false"/> 
    </cm:default-properties>
</cm:property-placeholder>

<bean id="myBean" class="com.mybean">
    <property name="abc" value="${abc}" />
</bean>

I can set values into beans, like ${abc}.
How can I access any other property of the config from java?

I am planning to add list of properties: prop1=11 prop2=22 ... propn=nn

I cannot add all in blueprint as the number varies.

Thank you, Viktor

Viktor D
  • 136
  • 1
  • 7

3 Answers3

3

Add reference to ConfigurationAdmin service in blueprint:
<reference id="configAdmin" interface="org.osgi.service.cm.ConfigurationAdmin" />

Set it to bean:
<bean id="myBean" class="com.mybean"> <property name="abc" value="${abc}" /> <property name="configAdmin" ref="configAdmin" /> </bean>

In bean:
private ConfigurationAdmin configAdmin;

public void setConfigAdmin(ConfigurationAdmin configAdmin) throws IOException {
    this.configAdmin = configAdmin;

    System.out.println(configAdmin);
    System.out.println(configAdmin.getConfiguration("any-persistent-id").getProperties());
}
Viktor D
  • 136
  • 1
  • 7
  • You can have a look also to this thread: http://stackoverflow.com/questions/33043163/karaf-add-additional-property-to-existing-config-file/33085204#comment54127110_33085204 – Jorge Martinez Dec 14 '15 at 15:11
1

You can set org.apache.aries.blueprint.compendium.cm.CmPropertyPlaceholder
into myBean as <property name="cmpp" ref="cmpp" />
after giving it id <cm:property-placeholder id="cmpp" ....

Then add setter in myBean for cmpp:
public void setProperties(CmPropertyPlaceholder properties) { Configuration config = CmUtils.getConfiguration(properties.getConfigAdmin(), properties.getPersistentId()); if (config != null) { Dictionary props = config.getProperties(); System.out.println(props); }

The only problem is bundle org.apache.aries.blueprint.cm does not export package org.apache.aries.blueprint.compendium.cm, so I can't access CmPropertyPlaceholder in java. Is there any command in karaf that would additionally export a package of a bundle?

It is possible to access configuration through context, will look into that and post.

Viktor D
  • 136
  • 1
  • 7
0

You better do this:

a) in blueprint.xml

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0"
    xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"

...

<cm:property-placeholder persistent-id="<name of cfg file>"
    update-strategy="reload">
    <cm:default-properties>
        <cm:property name="<property name in file>" value="<value>" />
    </cm:default-properties>
</cm:property-placeholder>


<bean id="<name of Bean>" class="<full name of class>" >
    <property name="<property name in file>" value="${<value>}"></property>
</bean>

b) inside "bean"

  • define a public constructor (definition required by Spring)
  • define a variable and its Setter:
private <Type of> <name of variable>;
public <Constructor> (){} // no parameters
public void  set<property name in file first upper letter>(<Type of> <property name in file>) {
  <name of variable> = <property name A>;
}

c) add into '/etc' folder the configuration file:

<name of cfg file>.cfg

Obviously, within it, the property:

<Property name> = <value>

  • The question was how to set list of values, that are not comma separated but defined one per line in config file. – Viktor D Jan 12 '17 at 08:48