How to define dynamic list of constant values in Blueprint

Solution Verified - Updated -

Environment

  • JBoss Fuse 6.1

Issue

I would like to define a list of the constant tokens - let's say ["foo", "bar", "baz"]. I would like to access the latter list from all the bundles deployed to the given Fuse instance. I would like to change the contents of the list from time to time.

What is the best way to achieve this in Blueprint and OSGi environment?

Resolution

In the OSGi environment, the best way to keep the singleton list of the constant elements is to define it as the OSGi service, just as demonstrated on the Blueprint snippet below. With this approach, we can update the list definition by replacing the OSGi bundle in which the list has been defined.

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd

       http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd">

    <!-- Constant list service -->

    <bean id="tokens" class="java.util.Arrays" factory-method="asList">
        <argument>
            <array>
                <value>${foo}</value>
                <value>${bar}</value>
            </array>
        </argument>
    </bean>

    <service interface="java.util.List" ref="tokens"/>

    <!-- Properties configuration -->

    <cm:property-placeholder id="myblueprint.placeholder" persistent-id="my.persistent.id">
        <cm:default-properties>
            <cm:property name="foo" value="foo"/>
            <cm:property name="bar" value="bar"/>
        </cm:default-properties>
    </cm:property-placeholder>

</blueprint>

In the example above we additionally use the OSGi Compendium Configuration Admin Service to optionally override the particular values of the list. Thanks to Admin Service we can change the value of the particular tokens even without redeploying the Blueprint bundle containing the list definition (we can override the token values by editing the KARAF_HOME/etc/my.persistent.id.cfg file).

The zip file attached to the hereby article contains runnable test example demonstrating the described concept.

Attachments

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.