Themis Pyrgiotis
Themis Pyrgiotis

Reputation: 896

Load environment variables outside Camel Context

I am using Camel and blueprint xml. And i want to access environment variables outside Camel Context, when i am setting up my beans. Initially i created an environment variable (i am using Windows)

set TEST=test_value

My blueprint xml is like this

<?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"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 
    https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"
    xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
    xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf">

    <cm:property-placeholder persistent-id="configuration.file"/>
    ...
    ...
    <bean id="_test" class="com.xxx.Test">
        <argument value="${my.property}"/>
    </bean>
    ...
    <camelContext id="_camelContext"
        xmlns="http://camel.apache.org/schema/blueprint"/>
    ...
    <!-- I can access environment variable with the following 2 ways -->
    <log message="TEST = {{env:TEST}}"/>     
    <log message="TEST via cfg file = {{my.property}}"/>
    ...
    </camelContext> 

and me configuration file (under etc folder) configuration.file.cfg is

my.property=${env.TEST}

With the previous approach i can access the environment variable in bean (outside context). How can i do the same directly? Without using the property file?

I tried the following

<bean id="_test" class="com.xxx.Test">
   <argument value="${env:TEST}"/>
</bean>

but is is not working.

Thanks!

Upvotes: 0

Views: 2331

Answers (2)

Themis Pyrgiotis
Themis Pyrgiotis

Reputation: 896

Finally I found a way. I am using fabric8-karaf-blueprint which leverages Aries PropertyEvaluator and property placeholders resolvers from fabric8-karaf-core to let you resolve placeholders in your Blueprint XML file.

First add that feature in your pom.xml.

<startupFeatures>
  ...
  <feature>fabric8-karaf-blueprint</feature>
  ...
</startupFeatures>

Next, add the namespace blueprint-ext in our blueprint.xml. Define different prefix and sufix (eg. $[]), so not to be confused with the existing blueprint-cm. Finally access environment variables with syntax $[env:TEST]. Next follows the blueprint sample.

<?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"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 
    https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"
    xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
    xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0"
    xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf">

    <cm:property-placeholder persistent-id="configuration.file"/>
    <ext:property-placeholder evaluator="fabric8" placeholder-prefix="$[" placeholder-suffix="]"/>
    ...
    ...
    <bean id="_test" class="com.xxx.Test">
        <argument value="$[env:TEST]"/>
    </bean>

Upvotes: 0

Related Questions