camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Using PropertyPlaceholder
Date Sun, 27 Feb 2011 11:00:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2036/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Using+PropertyPlaceholder">Using
PropertyPlaceholder</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
Using system and environment variables in locations <br>*Available as of Camel 2.7*
<br> <br>The location now supports using placeholders for JVM system properties
and OS environments variables. <br> <br>For example: <br>{code} <br>location=file:${karaf.home}/etc/foo.properties
<br>{code} <br> <br>In the location above we defined a location using the
file scheme using the JVM system property with key {{karaf.home}}. <br> <br>To
use an OS environment variable instead you would have to prefix with env: <br>{code}
<br>location=file:${env:APP_HOME}/etc/foo.properties <br>{code} <br>Where
{{APP_HOME}} is an OS environment. <br> <br>You can have multiple placeholders
in the same location, such as: <br>{code} <br>location=file:${env:APP_HOME}/etc/${prop.name}.properties
<br>{code} <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Configuring in Java DSL <br>You
have to create and register the {{PropertiesComponent}} under the name {{properties}} such
as: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="UsingPropertyPlaceholder-UsingPropertyPlaceholder"></a>Using
PropertyPlaceholder</h2>
<p><b>Available as of Camel 2.3</b></p>

<p>Camel now provides a new <tt>PropertiesComponent</tt> in <b>camel-core</b>
which allows you to use property placeholders when defining Camel <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">Endpoint</a> URIs. <br/>
This works much like you would do if using Spring's <tt>&lt;property-placeholder&gt;</tt>
tag. However Spring have a limitation which prevents 3rd party frameworks to leverage Spring
property placeholders to the fullest. See more at <a href="/confluence/display/CAMEL/How+do+I+use+Spring+Property+Placeholder+with+Camel+XML"
title="How do I use Spring Property Placeholder with Camel XML">How do I use Spring Property
Placeholder with Camel XML</a>.</p>

<p>The property placeholder is generally in use when doing:</p>
<ul class="alternate" type="square">
	<li>lookup or creating endpoints</li>
	<li>lookup of beans in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a></li>
	<li>additional supported in Spring XML (see below in examples)</li>
	<li>using Blueprint PropertyPlaceholder with Camel <a href="/confluence/display/CAMEL/Properties"
title="Properties">Properties</a> component</li>
</ul>


<h3><a name="UsingPropertyPlaceholder-Syntax"></a>Syntax</h3>
<p>The syntax to use Camel's property placeholder is to use {{<tt>key</tt>}}
for example {{<tt>file.uri</tt>}} where <tt>file.uri</tt> is the property
key.<br/>
You can use property placeholders in parts of the endpoint URI's which for example you can
use placeholders for parameters in the URIs.</p>

<h3><a name="UsingPropertyPlaceholder-PropertyResolver"></a>PropertyResolver</h3>
<p>As usually Camel provides a pluggable mechanism which allows 3rd part to provide
their own resolver to lookup properties. Camel provides a default implementation <tt>org.apache.camel.component.properties.DefaultPropertiesResolver</tt>
which is capable of loading properties from the file system, classpath or <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a>. You can prefix the locations with either:</p>
<ul class="alternate" type="square">
	<li><tt>ref:</tt> <b>Camel 2.4:</b> to lookup in the <a
href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a></li>
	<li><tt>file:</tt> to load the from file system</li>
	<li><tt>classpath:</tt> to load from classpath (this is also the default
if no prefix is provided)</li>
	<li><tt>blueprint:</tt> <b>Camel 2.7:</b> to use a specific
OSGi blueprint placeholder service</li>
</ul>


<h3><a name="UsingPropertyPlaceholder-Defininglocation"></a>Defining location</h3>
<p>The <tt>PropertiesResolver</tt> need to know a location(s) where to resolve
the properties. You can define 1 to many locations. If you define the location in a single
String property you can separate multiple locations with comma such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
pc.setLocation(<span class="code-quote">"com/mycompany/myprop.properties,com/mycompany/other.properties"</span>);
</pre>
</div></div>

<h4><a name="UsingPropertyPlaceholder-Usingsystemandenvironmentvariablesinlocations"></a>Using
system and environment variables in locations</h4>
<p><b>Available as of Camel 2.7</b></p>

<p>The location now supports using placeholders for JVM system properties and OS environments
variables.</p>

<p>For example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
location=file:${karaf.home}/etc/foo.properties
</pre>
</div></div>

<p>In the location above we defined a location using the file scheme using the JVM system
property with key <tt>karaf.home</tt>.</p>

<p>To use an OS environment variable instead you would have to prefix with env:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
location=file:${env:APP_HOME}/etc/foo.properties
</pre>
</div></div>
<p>Where <tt>APP_HOME</tt> is an OS environment.</p>

<p>You can have multiple placeholders in the same location, such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
location=file:${env:APP_HOME}/etc/${prop.name}.properties
</pre>
</div></div>


<h3><a name="UsingPropertyPlaceholder-ConfiguringinJavaDSL"></a>Configuring
in Java DSL</h3>
<p>You have to create and register the <tt>PropertiesComponent</tt> under
the name <tt>properties</tt> such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
PropertiesComponent pc = <span class="code-keyword">new</span> PropertiesComponent();
pc.setLocation(<span class="code-quote">"classpath:com/mycompany/myprop.properties"</span>);
context.addComponent(<span class="code-quote">"properties"</span>, pc);
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-ConfiguringinSpringXML"></a>Configuring
in Spring XML</h3>
<p>Spring XML offers two variations to configure. You can define a spring bean as a
<tt>PropertiesComponent</tt> which resembles the way done in Java DSL. Or you
can use the <tt>&lt;propertyPlaceholder&gt;</tt> tag.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"properties"</span>
class=<span class="code-quote">"org.apache.camel.component.properties.PropertiesComponent"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"location"</span>
value=<span class="code-quote">"classpath:com/mycompany/myprop.properties"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>Using the <tt>&lt;propertyPlaceholder&gt;</tt> tag makes the
configuration a bit more fresh such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext ...&gt;</span>
   <span class="code-tag">&lt;propertyPlaceholder id=<span class="code-quote">"properties"</span>
location=<span class="code-quote">"com/mycompany/myprop.properties"</span>/&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-UsingaPropertiesfromtheRegistry"></a>Using
a Properties from the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a></h3>
<p><b>Available as of Camel 2.4</b><br/>
For example in OSGi you may want to expose a service which returns the properties as a <tt>java.util.Properties</tt>
object.</p>

<p>Then you could setup the <a href="/confluence/display/CAMEL/Properties" title="Properties">Properties</a>
component as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
   <span class="code-tag">&lt;propertyPlaceholder id=<span class="code-quote">"properties"</span>
location=<span class="code-quote">"ref:myProperties"</span>/&gt;</span>
</pre>
</div></div>
<p>Where <tt>myProperties</tt> is the id to use for lookup in the OSGi registry.
Notice we use the <tt>ref:</tt> prefix to tell Camel that it should lookup the
properties for the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.</p>

<h3><a name="UsingPropertyPlaceholder-Examplesusingpropertiescomponent"></a>Examples
using properties component</h3>
<p>When using property placeholders in the endpoint URIs you can either use the <tt>properties:</tt>
component or define the placeholders directly in the URI. We will show example of both cases,
starting with the former.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.end=mock:result

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span
class="code-quote">"properties:{{cool.end}}"</span>);
</pre>
</div></div>

<p>You can also use placeholders as a part of the endpoint uri:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.foo=result

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span
class="code-quote">"properties:mock:{{cool.foo}}"</span>);
</pre>
</div></div>
<p>In the example above the to endpoint will be resolved to <tt>mock:result</tt>.</p>

<p>You can also have properties with refer to each other such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.foo=result
cool.concat=mock:{{cool.foo}}

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span
class="code-quote">"properties:mock:{{cool.concat}}"</span>);
</pre>
</div></div>
<p>Notice how <tt>cool.concat</tt> refer to another property.</p>

<p>The <tt>properties:</tt> component also offers you to override and provide
a location in the given uri using the <tt>locations</tt> option:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"properties:bar.end?locations=com/mycompany/bar.properties"</span>);
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-Examples"></a>Examples</h3>
<p>You can also use property placeholders directly in the endpoint uris without having
to use <tt>properties:</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.foo=result

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span
class="code-quote">"mock:{{cool.foo}}"</span>);
</pre>
</div></div>

<p>And you can use them in multiple wherever you want them:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.start=direct:start
cool.showid=<span class="code-keyword">true</span>
cool.result=result

<span class="code-comment">// route
</span>from(<span class="code-quote">"{{cool.start}}"</span>)
    .to(<span class="code-quote">"log:{{cool.start}}?showBodyType=<span class="code-keyword">false</span>&amp;showExchangeId={{cool.showid}}"</span>)
    .to(<span class="code-quote">"mock:{{cool.result}}"</span>);
</pre>
</div></div>

<p>You can also your property placeholders when using <a href="/confluence/display/CAMEL/ProducerTemplate"
title="ProducerTemplate">ProducerTemplate</a> for example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
template.sendBody(<span class="code-quote">"{{cool.start}}"</span>, <span class="code-quote">"Hello
World"</span>);
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-ExamplewithSimplelanguage"></a>Example
with <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> language</h3>
<p>The <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a>
language now also support using property placeholders, for example in the route below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cheese.quote=Camel rocks

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>)
    .transform().simple(<span class="code-quote">"Hi ${body} <span class="code-keyword">do</span>
you think ${properties:cheese.quote}?"</span>);
</pre>
</div></div>

<p>You can also specify the location in the <a href="/confluence/display/CAMEL/Simple"
title="Simple">Simple</a> language for example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// bar.properties
</span>bar.quote=Beer tastes good

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>)
    .transform().simple(<span class="code-quote">"Hi ${body}. ${properties:com/mycompany/bar.properties:bar.quote}."</span>);
</pre>
</div></div>


<h3><a name="UsingPropertyPlaceholder-AdditionalpropertyplaceholdersupportedinSpringXML"></a>Additional
property placeholder supported in Spring XML</h3>
<p>The property placeholders is also supported in many of the Camel Spring XML tags
such as <tt>&lt;package&gt;, &lt;packageScan&gt;, &lt;contextScan&gt;,
&lt;jmxAgent&gt;, &lt;endpoint&gt;, &lt;routeBuilder&gt;, &lt;proxy&gt;</tt>
and the others.</p>

<p>The example below has property placeholder in the &lt;jmxAgent&gt; tag:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext xmlns=<span
class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;propertyPlaceholder id=<span class="code-quote">"properties"</span>
location=<span class="code-quote">"org/apache/camel/spring/jmx.properties"</span>/&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- we can use propery
placeholders when we define the JMX agent --&gt;</span></span>
    &lt;jmxAgent id=<span class="code-quote">"agent"</span> registryPort=<span
class="code-quote">"{{myjmx.port}}"</span> disabled=<span class="code-quote">"{{myjmx.disabled}}"</span>
              usePlatformMBeanServer=<span class="code-quote">"{{myjmx.usePlatform}}"</span>
              createConnector=<span class="code-quote">"true"</span>
              statisticsLevel=<span class="code-quote">"RoutesOnly"</span>/&gt;

    <span class="code-tag">&lt;route id=<span class="code-quote">"foo"</span>
autoStartup=<span class="code-quote">"false"</span>&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:start"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>You can also define property placeholders in the various attributes on the &lt;camelContext&gt;
tag such as <tt>trace</tt> as shown here:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext trace=<span
class="code-quote">"{{foo.trace}}"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;propertyPlaceholder id=<span class="code-quote">"properties"</span>
location=<span class="code-quote">"org/apache/camel/spring/processor/myprop.properties"</span>/&gt;</span>

    <span class="code-tag">&lt;template id=<span class="code-quote">"camelTemplate"</span>
defaultEndpoint=<span class="code-quote">"{{foo.cool}}"</span>/&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"{{foo.header}}"</span>&gt;</span>
            <span class="code-tag">&lt;simple&gt;</span>${in.body} World!<span
class="code-tag">&lt;/simple&gt;</span>
        <span class="code-tag">&lt;/setHeader&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-OverridingapropertysettingusingaJVMSystemProperty"></a>Overriding
a property setting using a JVM System Property</h3>
<p><b>Available as of Camel 2.5</b><br/>
It is possible to override a property value at runtime using a JVM System property without
the need to restart the application to pick up the change. This may also be accomplished from
the command line by creating a JVM System property of the same name as the property it replaces
with a new value. An example of this is given below</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
PropertiesComponent pc = context.getComponent(<span class="code-quote">"properties"</span>,
PropertiesComponent.class);
pc.setCache(<span class="code-keyword">false</span>);
        
<span class="code-object">System</span>.setProperty(<span class="code-quote">"cool.end"</span>,
<span class="code-quote">"mock:override"</span>);
<span class="code-object">System</span>.setProperty(<span class="code-quote">"cool.result"</span>,
<span class="code-quote">"override"</span>);

context.addRoutes(<span class="code-keyword">new</span> RouteBuilder() {
    @Override
    <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span>
Exception {
        from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"properties:cool.end"</span>);
        from(<span class="code-quote">"direct:foo"</span>).to(<span class="code-quote">"properties:mock:{{cool.result}}"</span>);
    }
});
context.start();

getMockEndpoint(<span class="code-quote">"mock:override"</span>).expectedMessageCount(2);

template.sendBody(<span class="code-quote">"direct:start"</span>, <span class="code-quote">"Hello
World"</span>);
template.sendBody(<span class="code-quote">"direct:foo"</span>, <span class="code-quote">"Hello
Foo"</span>);

<span class="code-object">System</span>.clearProperty(<span class="code-quote">"cool.end"</span>);
<span class="code-object">System</span>.clearProperty(<span class="code-quote">"cool.result"</span>);
        
assertMockEndpointsSatisfied();
</pre>
</div></div>  

<h3><a name="UsingPropertyPlaceholder-UsingpropertyplaceholdersforanykindofattributeintheXMLDSL"></a>Using
property placeholders for any kind of attribute in the XML DSL</h3>
<p><b>Available as of Camel 2.7</b></p>

<p>Previously it was only the <tt>xs:string</tt> type attributes in the
XML DSL that support placeholders. For example often a timeout attribute would be a <tt>xs:int</tt>
type and thus you cannot set a string value as the placeholder key. This is now possible from
Camel 2.7 onwards using a special placeholder namespace.</p>

<p>In the example below we use the <tt>prop</tt> prefix for the namespace
<tt><a href="http://camel.apache.org/schema/placeholder" class="external-link" rel="nofollow">http://camel.apache.org/schema/placeholder</a></tt>
by which we can use the <tt>prop</tt> prefix in the attributes in the XML DSLs.
Notice how we use that in the <a href="/confluence/display/CAMEL/Multicast" title="Multicast">Multicast</a>
to indicate that the option <tt>stopOnException</tt> should be the value of the
placeholder with the key "stop".</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="code-keyword">xmlns:prop</span>=<span class="code-quote">"http://camel.apache.org/schema/placeholder"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    "&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- Notice in the
declaration above, we have defined the prop prefix as the Camel placeholder namespace --&gt;</span></span>

    <span class="code-tag">&lt;bean id=<span class="code-quote">"damn"</span>
class=<span class="code-quote">"java.lang.IllegalArgumentException"</span>&gt;</span>
        <span class="code-tag">&lt;constructor-arg index=<span class="code-quote">"0"</span>
value=<span class="code-quote">"Damn"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>

    <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>

        &lt;propertyPlaceholder id=<span class="code-quote">"properties"</span>
                             location=<span class="code-quote">"classpath:org/apache/camel/component/properties/myprop.properties"</span>
                             xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>/&gt;

        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
            &lt;!-- use prop namespace, to define a property placeholder, which maps to
                 option stopOnException={{stop}} --&gt;
            <span class="code-tag">&lt;multicast prop:stopOnException=<span class="code-quote">"stop"</span>&gt;</span>
                <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:a"</span>/&gt;</span>
                <span class="code-tag">&lt;throwException ref=<span class="code-quote">"damn"</span>/&gt;</span>
                <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:b"</span>/&gt;</span>
            <span class="code-tag">&lt;/multicast&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>In our properties file we have the value defined as</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
stop=<span class="code-keyword">true</span>
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-UsingpropertyplaceholderintheJavaDSL"></a>Using
property placeholder in the Java DSL</h3>
<p><b>Available as of Camel 2.7</b></p>

<p>Likewise we have added support for defining placeholders in the Java DSL using the
new <tt>placeholder</tt> DSL as shown in the following equivalent example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// use a property placeholder <span class="code-keyword">for</span>
the option stopOnException on the Multicast EIP
</span>    <span class="code-comment">// which should have the value of {{stop}}
key being looked up in the properties file
</span>    .multicast().placeholder(<span class="code-quote">"stopOnException"</span>,
<span class="code-quote">"stop"</span>)
        .to(<span class="code-quote">"mock:a"</span>).throwException(<span
class="code-keyword">new</span> IllegalAccessException(<span class="code-quote">"Damn"</span>)).to(<span
class="code-quote">"mock:b"</span>);
</pre>
</div></div>


<h3><a name="UsingPropertyPlaceholder-UsingBlueprintpropertyplaceholderwithCamelroutes"></a>Using
Blueprint property placeholder with Camel routes</h3>
<p><b>Available as of Camel 2.7</b></p>

<p>Camel supports <a href="/confluence/display/CAMEL/Using+OSGi+blueprint+with+Camel"
title="Using OSGi blueprint with Camel">Blueprint</a> which also offers a property
placeholder service. Camel supports convention over configuration, so all you have to do is
to define the OSGi Blueprint property placeholder in the XML file as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Using OSGi blueprint property placeholders in
Camel routes</b></div><div class="codeContent panelContent">
<pre class="code-xml">&lt;blueprint xmlns=<span class="code-quote">"http://www.osgi.org/xmlns/blueprint/v1.0.0"</span>
           <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
           <span class="code-keyword">xmlns:cm</span>=<span class="code-quote">"http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"</span>
           xsi:schemaLocation="
           http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- OSGI blueprint
property placeholder --&gt;</span></span>
    <span class="code-tag">&lt;cm:property-placeholder id=<span class="code-quote">"myblueprint.placeholder"</span>
persistent-id=<span class="code-quote">"camel.blueprint"</span>&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- list some
properties for this test --&gt;</span></span>
        <span class="code-tag">&lt;cm:default-properties&gt;</span>
            <span class="code-tag">&lt;cm:property name=<span class="code-quote">"result"</span>
value=<span class="code-quote">"mock:result"</span>/&gt;</span>
        <span class="code-tag">&lt;/cm:default-properties&gt;</span>
    <span class="code-tag">&lt;/cm:property-placeholder&gt;</span>

    <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/blueprint"</span>&gt;</span>

        &lt;!-- in the route we can use {{ }} placeholders which will lookup in blueprint
             as Camel will auto detect the OSGi blueprint property placeholder and use it
--&gt;
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:foo"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"{{result}}"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/blueprint&gt;</span>
</pre>
</div></div>

<p>By default Camel detects and uses OSGi blueprint property placeholder service. You
can disable this by setting the attribute <tt>useBlueprintPropertyResolver</tt>
to false on the <tt>&lt;camelContext&gt;</tt> definition.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>About placeholder
syntaxes</b><br />Notice how we can use the Camel syntax for placeholders {{ }}
in the Camel route, which will lookup the value from OSGi blueprint.<br/>
The blueprint syntax for placeholders is ${ }. So outside the &lt;camelContext&gt;
you must use the ${ } syntax. Where as inside &lt;camelContext&gt; you must use {{
}} syntax.<br/>
OSGi blueprint allows you to configure the syntax, so you can actually align those if you
want.</td></tr></table></div>

<p>You can also explicit refer to a specific OSGi blueprint property placeholder by
its id. For that you need to use the Camel's &lt;propertyPlaceholder&gt; as shown
in the example below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Explicit referring to a OSGi blueprint placeholder
in Camel</b></div><div class="codeContent panelContent">
<pre class="code-xml">&lt;blueprint xmlns=<span class="code-quote">"http://www.osgi.org/xmlns/blueprint/v1.0.0"</span>
           <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
           <span class="code-keyword">xmlns:cm</span>=<span class="code-quote">"http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"</span>
           xsi:schemaLocation="
           http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- OSGI blueprint
property placeholder --&gt;</span></span>
    <span class="code-tag">&lt;cm:property-placeholder id=<span class="code-quote">"myblueprint.placeholder"</span>
persistent-id=<span class="code-quote">"camel.blueprint"</span>&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- list some
properties for this test --&gt;</span></span>
        <span class="code-tag">&lt;cm:default-properties&gt;</span>
            <span class="code-tag">&lt;cm:property name=<span class="code-quote">"result"</span>
value=<span class="code-quote">"mock:result"</span>/&gt;</span>
        <span class="code-tag">&lt;/cm:default-properties&gt;</span>
    <span class="code-tag">&lt;/cm:property-placeholder&gt;</span>

    <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/blueprint"</span>&gt;</span>

        <span class="code-tag"><span class="code-comment">&lt;!-- using Camel
properties component and refer to the blueprint property placeholder by its id --&gt;</span></span>
        <span class="code-tag">&lt;propertyPlaceholder id=<span class="code-quote">"properties"</span>
location=<span class="code-quote">"blueprint:myblueprint.placeholder"</span>/&gt;</span>

        <span class="code-tag"><span class="code-comment">&lt;!-- in the route
we can use {{ }} placeholders which will lookup in blueprint --&gt;</span></span>
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:foo"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"{{result}}"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/blueprint&gt;</span>
</pre>
</div></div>

<p>Notice how we use the <tt>blueprint</tt> scheme to refer to the OSGi
blueprint placeholder by its id. This allows you to mix and match, for example you can also
have additional schemes in the location. For example to load a file from the classpath you
can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
location=<span class="code-quote">"blueprint:myblueprint.placeholder,classpath:myproperties.properties"</span>
</pre>
</div></div>

<p>Each location is separated by comma.</p>




    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Using+PropertyPlaceholder">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=13271723&revisedVersion=14&originalVersion=13">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Using+PropertyPlaceholder?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message