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 Fri, 12 Nov 2010 19:24: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/Scripting+Languages+Context">Scripting
Languages Context</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~ammulder">Aaron
Mulder</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;">You
can also use predicates e.g. in a Filter: <br>{code:xml} <br>    &lt;filter&gt;
<br>        &lt;language language=&quot;beanshell&quot;&gt;request.getHeaders().get(&quot;Foo&quot;).equals(&quot;Bar&quot;)&lt;/language&gt;
<br>        &lt;to uri=&quot;direct:next&quot; /&gt; <br>    &lt;/filter&gt;
<br>{code} <br> <br>Some languages with DSL support include: <br>||Language||DSL
keyword|| <br>|EL|{{el}}| <br>|Groovy|{{groovy}}| <br>|JavaScript|{{javaScript}}|
<br>|JoSQL|{{sql}}| <br>|JXPath|{{jxpath}}| <br>|MVEL|{{mvel}}| <br>|OGNL|{{ognl}}|
<br>|PHP|{{php}}| <br>|Python|{{python}}| <br>|Ruby|{{ruby}}| <br>|XPath|{{xpath}}|
<br>|XQuery|{{xquery}}| <br> <br>Some languages without specific DSL support
but known to work with these generic methods include: <br>||Language||Implementation||language=&quot;...&quot;
value|| <br>|BeanShell|[BeanShell 2.0b5|http://svn.apache.org/repos/asf/servicemix/m2-repo/org/beanshell/bsh/2.0b5/bsh-2.0b5.jar]|{{beanshell}}
or {{bsh}}| <br> <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>
</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>Some languages with DSL support include:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Language</th>
<th class='confluenceTh'>DSL keyword</th>
</tr>
<tr>
<td class='confluenceTd'>EL</td>
<td class='confluenceTd'><tt>el</tt></td>
</tr>
<tr>
<td class='confluenceTd'>Groovy</td>
<td class='confluenceTd'><tt>groovy</tt></td>
</tr>
<tr>
<td class='confluenceTd'>JavaScript</td>
<td class='confluenceTd'><tt>javaScript</tt></td>
</tr>
<tr>
<td class='confluenceTd'>JoSQL</td>
<td class='confluenceTd'><tt>sql</tt></td>
</tr>
<tr>
<td class='confluenceTd'>JXPath</td>
<td class='confluenceTd'><tt>jxpath</tt></td>
</tr>
<tr>
<td class='confluenceTd'>MVEL</td>
<td class='confluenceTd'><tt>mvel</tt></td>
</tr>
<tr>
<td class='confluenceTd'>OGNL</td>
<td class='confluenceTd'><tt>ognl</tt></td>
</tr>
<tr>
<td class='confluenceTd'>PHP</td>
<td class='confluenceTd'><tt>php</tt></td>
</tr>
<tr>
<td class='confluenceTd'>Python</td>
<td class='confluenceTd'><tt>python</tt></td>
</tr>
<tr>
<td class='confluenceTd'>Ruby</td>
<td class='confluenceTd'><tt>ruby</tt></td>
</tr>
<tr>
<td class='confluenceTd'>XPath</td>
<td class='confluenceTd'><tt>xpath</tt></td>
</tr>
<tr>
<td class='confluenceTd'>XQuery</td>
<td class='confluenceTd'><tt>xquery</tt></td>
</tr>
</tbody></table>
</div>


<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-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;1.4.0&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=9&originalVersion=8">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