camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Scripting Languages Context
Date Wed, 26 Oct 2011 11:24:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/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/Scripting+Languages+Context">Scripting
Languages Context</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (2)</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" >| request | {{org.apache.camel.Message}}
| The IN message | <br>| response | {{org.apache.camel.Message}} | The OUT message |
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
properties | {{org.apache.camel.builder.script.PropertiesFunction}} | *Camel 2.9:* Function
with a {{resolve}} method to make it easier to use Camels [Properties] component from scripts.
See further below for example. | <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Attributes <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Using properties function <br>*Available as of Camel 2.9* <br> <br>If you
need to use the [Properties] component from a script to lookup property placeholders, then
its a bit cumbersome to do so. <br>For example to set a header name myHeader with a
value from a property placeholder, which key is provided in a header named &quot;foo&quot;.
<br>{code} <br>.setHeader(&quot;myHeader&quot;).groovy(&quot;context.resolvePropertyPlaceholders(&#39;{{&#39;
+ request.headers.get(&#39;foo&#39;) + &#39;}}&#39;)&quot;) <br>{code}
<br> <br>From Camel 2.9 onwards you can now use the properties function and the
same example is simpler: <br>{code} <br>.setHeader(&quot;myHeader&quot;).groovy(&quot;properties.resolve(request.headers.get(&#39;foo&#39;))&quot;)
<br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Dependencies <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h3><a name="ScriptingLanguagesContext-ScriptContext"></a>ScriptContext</h3>
<p>The JSR-223 scripting languages ScriptContext is pre configured with the following
attributes all set at <tt>ENGINE_SCOPE</tt>:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Attribute </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Value </th>
</tr>
<tr>
<td class='confluenceTd'> context </td>
<td class='confluenceTd'> <tt>org.apache.camel.CamelContext</tt> </td>
<td class='confluenceTd'> The Camel Context </td>
</tr>
<tr>
<td class='confluenceTd'> exchange </td>
<td class='confluenceTd'> <tt>org.apache.camel.Exchange</tt> </td>
<td class='confluenceTd'> The current Exchange </td>
</tr>
<tr>
<td class='confluenceTd'> request </td>
<td class='confluenceTd'> <tt>org.apache.camel.Message</tt> </td>
<td class='confluenceTd'> The IN message </td>
</tr>
<tr>
<td class='confluenceTd'> response </td>
<td class='confluenceTd'> <tt>org.apache.camel.Message</tt> </td>
<td class='confluenceTd'> The OUT message </td>
</tr>
<tr>
<td class='confluenceTd'> properties </td>
<td class='confluenceTd'> <tt>org.apache.camel.builder.script.PropertiesFunction</tt>
</td>
<td class='confluenceTd'> <b>Camel 2.9:</b> Function with a <tt>resolve</tt>
method to make it easier to use Camels <a href="/confluence/display/CAMEL/Properties" title="Properties">Properties</a>
component from scripts. See further below for example. </td>
</tr>
</tbody></table>
</div>


<h3><a name="ScriptingLanguagesContext-Attributes"></a>Attributes</h3>

<p>You can add your own attributes with the <tt>attribute(name, value)</tt>
DSL method, such as:</p>

<p>In the sample below we add an attribute <tt>user</tt> that is an object
we already have instantiated as myUser. This object has a getFirstName() method that we want
to set as header on the message. We use the groovy language to concat the first and last name
into a single string that is returned.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:in"</span>).setHeader(<span class="code-quote">"name"</span>).groovy(<span
class="code-quote">"'$user.firstName $user.lastName'"</span>).attribute(<span
class="code-quote">"user"</span>, myUser).to(<span class="code-quote">"seda:users"</span>);
</pre>
</div></div>

<h3><a name="ScriptingLanguagesContext-Anyscriptinglanguage"></a>Any scripting
language</h3>
<p>Camel can run any JSR-223 scripting languages using the <tt>script</tt>
DSL method such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:in"</span>).setHeader(<span class="code-quote">"firstName"</span>).script(<span
class="code-quote">"jaskel"</span>, <span class="code-quote">"user.firstName"</span>).attribute(<span
class="code-quote">"user"</span>, myUser).to(<span class="code-quote">"seda:users"</span>);
</pre>
</div></div>

<p>This is a bit different using the Spring DSL where you use the <b>expression</b>
element that doesn't support setting attributes (yet):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:in"</span>/&gt;</span>
    <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"firstName"</span>&gt;</span>
        <span class="code-tag">&lt;expression language=<span class="code-quote">"jaskel"</span>&gt;</span>user.firstName<span
class="code-tag">&lt;/expression&gt;</span>
    <span class="code-tag">&lt;/setHeader&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"seda:users"</span>/&gt;</span>
</pre>
</div></div>

<p>You can also use predicates e.g. in a Filter:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;filter&gt;</span>
        <span class="code-tag">&lt;language language=<span class="code-quote">"beanshell"</span>&gt;</span>request.getHeaders().get(<span
class="code-quote">"Foo"</span>).equals(<span class="code-quote">"Bar"</span>)<span
class="code-tag">&lt;/language&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:next"</span>
/&gt;</span>
    <span class="code-tag">&lt;/filter&gt;</span>
</pre>
</div></div>

<p>See <a href="/confluence/display/CAMEL/Scripting+Languages" title="Scripting Languages">Scripting
Languages</a> for the list of languages with explicit DSL support.</p>

<p>Some languages without specific DSL support but known to work with these generic
methods include:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Language</th>
<th class='confluenceTh'>Implementation</th>
<th class='confluenceTh'>language="..." value</th>
</tr>
<tr>
<td class='confluenceTd'>BeanShell</td>
<td class='confluenceTd'><a href="http://svn.apache.org/repos/asf/servicemix/m2-repo/org/beanshell/bsh/2.0b5/bsh-2.0b5.jar"
class="external-link" rel="nofollow">BeanShell 2.0b5</a></td>
<td class='confluenceTd'><tt>beanshell</tt> or <tt>bsh</tt></td>
</tr>
</tbody></table>
</div>


<h3><a name="ScriptingLanguagesContext-AdditionalargumentstoScriptingEngine"></a>Additional
arguments to ScriptingEngine</h3>
<p><b>Available as of Camel 2.8</b></p>

<p>You can provide additional arguments to the <tt>ScriptingEngine</tt>
using a header on the Camel message with the key <tt>CamelScriptArguments</tt>.<br/>
See this example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> void testArgumentsExample()
<span class="code-keyword">throws</span> Exception {
    <span class="code-keyword">if</span> (!ScriptTestHelper.canRunTestOnThisPlatform())
{
        <span class="code-keyword">return</span>;
    }

    getMockEndpoint(<span class="code-quote">"mock:result"</span>).expectedMessageCount(0);
    getMockEndpoint(<span class="code-quote">"mock:unmatched"</span>).expectedMessageCount(1);

    <span class="code-comment">// additional arguments to ScriptEngine
</span>    Map&lt;<span class="code-object">String</span>, <span
class="code-object">Object</span>&gt; arguments = <span class="code-keyword">new</span>
HashMap&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt;();
    arguments.put(<span class="code-quote">"foo"</span>, <span class="code-quote">"bar"</span>);
    arguments.put(<span class="code-quote">"baz"</span>, 7);

    <span class="code-comment">// those additional arguments is provided as a header
on the Camel Message
</span>    template.sendBodyAndHeader(<span class="code-quote">"direct:start"</span>,
<span class="code-quote">"hello"</span>, ScriptBuilder.ARGUMENTS, arguments);

    assertMockEndpointsSatisfied();
}
</pre>
</div></div>


<h3><a name="ScriptingLanguagesContext-Usingpropertiesfunction"></a>Using
properties function</h3>
<p><b>Available as of Camel 2.9</b></p>

<p>If you need to use the <a href="/confluence/display/CAMEL/Properties" title="Properties">Properties</a>
component from a script to lookup property placeholders, then its a bit cumbersome to do so.<br/>
For example to set a header name myHeader with a value from a property placeholder, which
key is provided in a header named "foo".</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.setHeader(<span class="code-quote">"myHeader"</span>).groovy(<span class="code-quote">"context.resolvePropertyPlaceholders('{{'
+ request.headers.get('foo') + '}}')"</span>)
</pre>
</div></div>

<p>From Camel 2.9 onwards you can now use the properties function and the same example
is simpler:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.setHeader(<span class="code-quote">"myHeader"</span>).groovy(<span class="code-quote">"properties.resolve(request.headers.get('foo'))"</span>)
</pre>
</div></div>

<h3><a name="ScriptingLanguagesContext-Dependencies"></a>Dependencies</h3>

<p>To use scripting languages in your camel routes you need to add the a dependency
on <b>camel-script</b> which integrates the JSR-223 scripting engine. </p>

<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest &amp; greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-script&lt;/artifactId&gt;
  &lt;version&gt;x.x.x&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>
    </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/Scripting+Languages+Context">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=97905&revisedVersion=12&originalVersion=11">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Scripting+Languages+Context?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message