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 Fri, 09 Jul 2010 15:45:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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 (3)</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" > <br>h3. PropertyResolver <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">As
usually Camel provides a pluggable mechanism which allows 3rd part to provide their own resolver
to lookup properties. Camel provides a default implementation {{org.apache.camel.component.properties.DefaultPropertiesResolver}}
which is capable of loading properties from the file system or classpath. You can prefix the
locations with either {{file:}} or {{classpath:}} where {{classpath:}} is the default if no
prefix is provided. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">As
usually Camel provides a pluggable mechanism which allows 3rd part to provide their own resolver
to lookup properties. Camel provides a default implementation {{org.apache.camel.component.properties.DefaultPropertiesResolver}}
which is capable of loading properties from the file system, classpath or [Registry]. You
can prefix the locations with either: <br>- {{ref:}} *Camel 2.4:* to lookup in the [Registry]
<br>- {{file:}} to load the from file system <br>- {{classpath:}} to load from
classpath (this is also the default if no prefix is provided) <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Defining location <br></td></tr>
            <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;">h3.
Using a Properties from the [Registry] <br>*Available as of Camel 2.4* <br>For
example in OSGi you may want to expose a service which returns the properties as a {{java.util.Properties}}
object. <br> <br>Then you could setup the [Properties] component as follows: <br>{code:xml}
<br>   &lt;propertyPlaceholder id=&quot;properties&quot; location=&quot;ref:myProperties&quot;/&gt;
<br>{code} <br>Where {{myProperties}} is the id to use for lookup in the OSGi
registry. Notice we use the {{ref:}} prefix to tell Camel that it should lookup the properties
for the [Registry]. <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Examples using properties component
<br>When using property placeholders in the endpoint URIs you can either use the {{properties:}}
component or define the placeholders directly in the URI. We will show example of both cases,
starting with the former. <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>
</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>
</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>

<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 id=<span
class="code-quote">"camel"</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/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>
              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&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-Unittests"></a>Unit tests</h3>
<p>See the unit tests in camel-core and camel-spring</p>
<ul class="alternate" type="square">
	<li>PropertiesComponentTest.java</li>
	<li>PropertiesComponentEndpointTest.java</li>
	<li>PropertiesComponentSimpleLanguageTest.java</li>
	<li>SpringPropertiesComponentTest.xml</li>
	<li>SpringPropertiesComponent2Test.xml</li>
	<li>SpringPropertiesComponent3Test.xml</li>
</ul>

    </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=7&originalVersion=6">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