camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Properties
Date Mon, 08 Feb 2010 10:43:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Properties">Properties</a></h2>
    <h4>Page  <b>added</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="Properties-PropertiesComponent"></a>Properties Component</h2>
<p><b>Available as of Camel 2.3</b></p>

<h2><a name="Properties-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>

<h3><a name="Properties-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="Properties-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 or classpath. You can prefix the
locations with either <tt>file:</tt> or <tt>classpath:</tt> where
<tt>classpath:</tt> is the default if no prefix is provided.</p>

<h3><a name="Properties-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="Properties-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="Properties-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="Properties-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="Properties-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>

<h3><a name="Properties-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="Properties-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="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Properties">View Online</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Properties?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message