org.apache.camel.builder.script
Class ScriptBuilder

java.lang.Object
  extended by org.apache.camel.builder.script.ScriptBuilder
All Implemented Interfaces:
org.apache.camel.Expression, org.apache.camel.Predicate, org.apache.camel.Processor

public class ScriptBuilder
extends Object
implements org.apache.camel.Expression, org.apache.camel.Predicate, org.apache.camel.Processor

A builder class for creating Processor, Expression and Predicate objects using the JSR 223 scripting engine.

Version:

Field Summary
static String ARGUMENTS
          Additional arguments to ScriptEngine provided as a header on the IN Message using the key ARGUMENTS
 
Constructor Summary
ScriptBuilder(org.apache.camel.CamelContext camelContext, String scriptLanguage, String scriptText)
          Constructor.
ScriptBuilder(org.apache.camel.CamelContext camelContext, String scriptLanguage, String scriptText, ScriptEngineFactory scriptEngineFactory)
          Constructor.
ScriptBuilder(String scriptLanguage, String scriptText)
          Constructor.
 
Method Summary
protected  void addScriptEngineArguments(ScriptEngine engine, org.apache.camel.Exchange exchange)
           
 void assertMatches(String text, org.apache.camel.Exchange exchange)
           
 ScriptBuilder attribute(String name, Object value)
          Sets the attribute on the context so that it is available to the script as a variable in the ScriptContext.ENGINE_SCOPE
protected static ScriptEngine createScriptEngine(String language)
           
protected  ScriptEvaluationException createScriptEvaluationException(Throwable e)
           
protected static InputStreamReader createScriptReader(org.apache.camel.spi.ClassResolver classResolver, String resource)
           
 Object evaluate(org.apache.camel.Exchange exchange)
           
<T> T
evaluate(org.apache.camel.Exchange exchange, Class<T> type)
           
protected  Object evaluateScript(org.apache.camel.Exchange exchange)
           
 CompiledScript getCompiledScript()
           
 String getScriptDescription()
          Returns a description of the script
 String getScriptLanguage()
           
static ScriptBuilder groovy(String scriptText)
          Creates a script builder for the groovy script contents
static ScriptBuilder javaScript(String scriptText)
          Creates a script builder for the JavaScript/ECMAScript script contents
protected static ScriptEngineFactory lookupScriptEngineFactory(String language)
           
 boolean matches(org.apache.camel.Exchange exchange)
           
protected  boolean matches(org.apache.camel.Exchange exchange, Object scriptValue)
           
static ScriptBuilder php(String scriptText)
          Creates a script builder for the PHP script contents
protected  ScriptContext populateBindings(ScriptEngine engine, org.apache.camel.Exchange exchange, Map<String,Object> attributes)
           
 void process(org.apache.camel.Exchange exchange)
           
static ScriptBuilder python(String scriptText)
          Creates a script builder for the Python script contents
static ScriptBuilder ruby(String scriptText)
          Creates a script builder for the Ruby/JRuby script contents
protected  Object runScript(ScriptEngine engine, org.apache.camel.Exchange exchange, ScriptContext context)
           
static ScriptBuilder script(String language, String scriptText)
          Creates a script builder for the named language and script contents
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ARGUMENTS

public static final String ARGUMENTS
Additional arguments to ScriptEngine provided as a header on the IN Message using the key ARGUMENTS

See Also:
Constant Field Values
Constructor Detail

ScriptBuilder

public ScriptBuilder(String scriptLanguage,
                     String scriptText)
Constructor.

Parameters:
scriptLanguage - the name of the scripting language
scriptText - the script text to be evaluated, or a reference to a script resource

ScriptBuilder

public ScriptBuilder(org.apache.camel.CamelContext camelContext,
                     String scriptLanguage,
                     String scriptText)
Constructor.

Parameters:
scriptLanguage - the name of the scripting language
scriptText - the script text to be evaluated, or a reference to a script resource

ScriptBuilder

public ScriptBuilder(org.apache.camel.CamelContext camelContext,
                     String scriptLanguage,
                     String scriptText,
                     ScriptEngineFactory scriptEngineFactory)
Constructor.

Parameters:
scriptLanguage - the name of the scripting language
scriptText - the script text to be evaluated, or a reference to a script resource
scriptEngineFactory - the script engine factory
Method Detail

toString

public String toString()
Overrides:
toString in class Object

evaluate

public Object evaluate(org.apache.camel.Exchange exchange)

evaluate

public <T> T evaluate(org.apache.camel.Exchange exchange,
                      Class<T> type)
Specified by:
evaluate in interface org.apache.camel.Expression

matches

public boolean matches(org.apache.camel.Exchange exchange)
Specified by:
matches in interface org.apache.camel.Predicate

assertMatches

public void assertMatches(String text,
                          org.apache.camel.Exchange exchange)
                   throws AssertionError
Throws:
AssertionError

process

public void process(org.apache.camel.Exchange exchange)
Specified by:
process in interface org.apache.camel.Processor

attribute

public ScriptBuilder attribute(String name,
                               Object value)
Sets the attribute on the context so that it is available to the script as a variable in the ScriptContext.ENGINE_SCOPE

Parameters:
name - the name of the attribute
value - the attribute value
Returns:
this builder

script

public static ScriptBuilder script(String language,
                                   String scriptText)
Creates a script builder for the named language and script contents

Parameters:
language - the language to use for the script
scriptText - the script text to be evaluated, or a reference to a script resource
Returns:
the builder

groovy

public static ScriptBuilder groovy(String scriptText)
Creates a script builder for the groovy script contents

Parameters:
scriptText - the script text to be evaluated, or a reference to a script resource
Returns:
the builder

javaScript

public static ScriptBuilder javaScript(String scriptText)
Creates a script builder for the JavaScript/ECMAScript script contents

Parameters:
scriptText - the script text to be evaluated, or a reference to a script resource
Returns:
the builder

php

public static ScriptBuilder php(String scriptText)
Creates a script builder for the PHP script contents

Parameters:
scriptText - the script text to be evaluated, or a reference to a script resource
Returns:
the builder

python

public static ScriptBuilder python(String scriptText)
Creates a script builder for the Python script contents

Parameters:
scriptText - the script text to be evaluated, or a reference to a script resource
Returns:
the builder

ruby

public static ScriptBuilder ruby(String scriptText)
Creates a script builder for the Ruby/JRuby script contents

Parameters:
scriptText - the script text to be evaluated, or a reference to a script resource
Returns:
the builder

getCompiledScript

public CompiledScript getCompiledScript()

getScriptLanguage

public String getScriptLanguage()

getScriptDescription

public String getScriptDescription()
Returns a description of the script

Returns:
the script description

matches

protected boolean matches(org.apache.camel.Exchange exchange,
                          Object scriptValue)

lookupScriptEngineFactory

protected static ScriptEngineFactory lookupScriptEngineFactory(String language)

createScriptEngine

protected static ScriptEngine createScriptEngine(String language)

evaluateScript

protected Object evaluateScript(org.apache.camel.Exchange exchange)

runScript

protected Object runScript(ScriptEngine engine,
                           org.apache.camel.Exchange exchange,
                           ScriptContext context)
                    throws ScriptException,
                           IOException
Throws:
ScriptException
IOException

populateBindings

protected ScriptContext populateBindings(ScriptEngine engine,
                                         org.apache.camel.Exchange exchange,
                                         Map<String,Object> attributes)

addScriptEngineArguments

protected void addScriptEngineArguments(ScriptEngine engine,
                                        org.apache.camel.Exchange exchange)

createScriptReader

protected static InputStreamReader createScriptReader(org.apache.camel.spi.ClassResolver classResolver,
                                                      String resource)
                                               throws IOException
Throws:
IOException

createScriptEvaluationException

protected ScriptEvaluationException createScriptEvaluationException(Throwable e)


Apache Camel