Search code examples
javajdbcapache-camelblueprint-osgi

Apache Camel - Read JDBC dataSource properties from file


i'm using Apache Camel and i try to load datasource properties from this file

config.properties:

url = my_url
user = user_name
password = user_pass

this is dataSource (blueprint.xml):

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
      <property name="URL" value="my_url"/>
      <property name="user" value="user_name"/>
      <property name="password" value="user_pass"/>
  </bean> 

How can i read values from config.properties and insert them into dataSource properties ?


Solution

  • You talk about blueprint.xml, and camel, so I assume you are in an osgi container like Karaf/ServiceMix, and you are using Aries Blueprint.

    Then you can use the cm namespace and a property-placeholder. If you use camel and want your properties to be dynamically reloaded, then you can use too an update strategy reload, which start/stop the blueprint container when the configuration change. This will load the configuration with pid "datasource" (ie, in karaf, the file etc/datasource.cfg) :

    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
               xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0"
               xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0">
    
      <cm:cm-properties id="myProps" persistent-id="datasource" update-strategy="reload"/>
    
      <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
          <property name="URL" value="${url}"/>
          <property name="user" value="${user}"/>
          <property name="password" value="${password}"/>
      </bean> 
    </blueprint>
    

    If you want to use your configuration file without using ConfigurationAdmin or dynamically reload your bundle, then you can use the ext namespace :

    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
               xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0">
    
        <ext:property-placeholder>
            <ext:location>file:config.properties</ext:location>
        </ext:property-placeholder>
    
        <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
              <property name="URL" value="${url}"/>
              <property name="user" value="${user}"/>
              <property name="password" value="${password}"/>
         </bean> 
    </blueprint>