Josh
Josh

Reputation: 277

Share a Property between two blueprints

I am trying to define a property in blueprint 1 and use it as the default property in blueprint 2.

Both blueprints will be loaded into the same Karaf OSGI container but exist in different bundles. The manifests are already set up with necessary dependency info.

Here is my Blueprint1 that defines the property i want to use again in another blueprint:

<blueprint
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
    xsi:schemaLocation="
      http://www.osgi.org/xmlns/blueprint/v1.0.0
      https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

<!-- Properties for this blueprint -->
<osgix:cm-properties id="sharedProperties" persistent-id="com.foo.project" update-strategy="reload"
                      xmlns="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0">
    <default-properties>
        <property name="shared-property" value="value"/>
    </default-properties>
</osgix:cm-properties>

<bean id="myBean"
      class="com.foo.MyClass">
    <property name="setting" value="{{shared-property}}"/>
</bean>

And my other Blueprint which is trying to use "my-property" defined above:

<blueprint
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
    xmlns:ctx="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
      http://www.osgi.org/xmlns/blueprint/v1.0.0
      https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

<!-- Load the properties from Blueprint1. Intent is to use property defined over there, here. -->
<osgix:cm-properties id="sharedProperties" persistent-id="com.foo.project"/>
<!-- Make properties defined in sharedProperties available using ${} syntax-->
<ctx:property-placeholder properties-ref="sharedProperties" />

<!-- Properties for this blueprint -->
<property-placeholder persistent-id="com.foo.project.blueprint2" update-strategy="reload"
                      xmlns="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
                      placeholder-prefix="{{{"
                      placeholder-suffix="}}}">
    <default-properties>
        <property name="my-property" value="${shared-property}"/>
    </default-properties>
</property-placeholder>

<bean id="myBean"
      class="com.foo.MyClass">
    <property name="setting" value="{{{my-property}}}"/>
</bean>

Ive got a feeling(maybe just hope) this is close to correct but off by a little. If it is correct my issue may be with a missing namespace handler.

Upvotes: 0

Views: 174

Answers (1)

Matt Pavlovich
Matt Pavlovich

Reputation: 4306

By using the "default-properties" tag you are redefining the value. Remove the from the second project. Also, I recommend removing default-properties from both and utilize an externalized cfg file so you do not have a race condition at start-up.

Remove this:

<default-properties>
    <property name="my-property" value="${shared-property}"/>
</default-properties>

Upvotes: 1

Related Questions