Java – Sharing a configuration file between OSGi bundles in Apache Servicemix 4

apache-felixapache-servicemixjavaosgi

Has anyone been able to successfully share configuration between two or more bundles in SMX4? What I'm looking for is this:

  1. Have a single file in $SMX_HOME/etc/myconfiguration.cfg
  2. Have this configuration be "available" to be injected into my bundles via the OSGi Configuration Admin using Spring dm, i.e.
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:osgi="http://www.springframework.org/schema/osgi"
        xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
        xmlns:ctx="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
                            http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium-1.2.xsd">

        <osgix:cm-properties id="cfg"
            persistent-id="myconfiguration">
            <prop key="db.driverClassName">org.postgresql.Driver</prop>
            <prop key="db.url">jdbc:postgresql://localhost/db</prop>
            <prop key="db.username">someuser</prop>
            <prop key="db.password">somepassword</prop>
            <prop key="amq.brokerURL">vm://default</prop>
        </osgix:cm-properties>

        <ctx:property-placeholder properties-ref="cfg" />

Then, I can inject things like this into my bean(s):

    
    .
    .
    .
        <bean id="activeMqConnectionFactory"
            class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="${amq.brokerURL}" />
        </bean>
    .
    .
    .

All of that is just peachy, for a single bundle. What I am looking for is a way to define this once and then be able to reuse the same configuration file as properties for a set of bundles. Right now, I have multiple bundles, each with their own configuration instance (persistent id) and thus, each bundle that needs a database connection, Java JMS, etc. has to have the configuration repeated in every file.

Currently, I'm using Apache Servicemix 4, which is using Apache Felix as the OSGi container.

Best Answer

I share my configuration located in

$SMX_HOME/etc/my.config.cfg

using this declaration

<!-- get properties as bean from OSGi Configuration Admin Service -->
<osgix:cm-properties id="myConfig" persistent-id="my.config" />

<!-- activate ${...} placeholder -->
<ctx:property-placeholder properties-ref="myConfig" />

in each of my bundles. Looks quite similar to your solution and works perfect! You can share your configuration (do not need to provide a configuration file for each bundle) but you have to declare the reference to this configuration in each spring-application-context file.

Im using FUSE 4.3 which is based on apache-servicemix-4.3.0-fuse-01-00.

Related Topic