This page last changed on Oct 28, 2006 by ross.

Due to licensing issue, Mule cannot bundle JSR-223 libraries in the distribution. Until this page is updated with detailed instructions, one can check MULE-1168 for steps involved.

 

Mule Components, transports and configuration can be scripted using any JSR-223 compliant scripting language such as PHP (stateless), Groovy or Rhino (javascript).

Script Components

Scripts can be invoked as Mule managed components by using org.mule.components.script.jsr223.ScriptComponent.

To use Groovy as an example -

<mule-descriptor name="GroovyUMO"
    inboundEndpoint="jms://groovy.queue"
    implementation="org.mule.components.script.jsr223.ScriptComponent">
    <properties>
        <property name="scriptEngineName" value="groovy"/>
        <text-property name="scriptText">
             return "Received message: " + message.getPayload()
        </text-property>
    </properties>
</mule-descriptor>

The following properties can be set on the ScriptComponent

Property Description
scriptText The script text that will be executed
scriptFile The location of a script file on the class path of file system
scriptEngineName The type of script being parsed, i.e. php, groovy or javascript

Interacting with Mule from your scripts

When a script is invoked, the JSR-223 api has a notion of a namespace, a collection of objects that are referenceable by name from your script. Mule makes the following objects available.

Name Description
eventContext The current UMOEventContext
managementContext The Mule Manager instance
message the current UMOMessage. This is also accessible using 'context.getMessage()'
descriptor The UMODescriptor object that represents this scripting component
componentNamespace A reference to the namespace collection for this script
log The commons Logger instance used by the script component
result A placeholder 'result' object that can be used to pass back a result to the script component.

Script Configuration Builder

The ScriptConfigurationBuilder allows developers to create a mule instance from a JSR-223 compliant script. To load the manager from javascript -

ScriptConfigurationBuilder builder = new ScriptConfigurationBuilder("javascript");
UMOManager manager = builder.configure("../conf/mule-config.js");

Or to start the server from the command line -

java -Dorg.mule.script.engine=javascript
-cp ... org.mule.MuleServer
-builder org.mule.config.builders.ScriptConfigurationBuilder
-config ../conf/mule-config.js

For more information about configuring a Mule instance from code or script see Configuring Mule Programmatically.

Script Transformers

Script transforms can be used to perform transformations on objects using scripting. The transformers are configuring in the same way as the ScriptComponent.

To use Groovy as an example, the configuration for a transformer that converts a comma-separated string of values to a java.util.List would look like -

<transformer name="StringToList" returnClass="java.util.List"
        className="org.mule.transformers.script.ScriptTransformer">
    <properties>
        <property name="scriptEngineName" value="groovy"/>
        <property name="sourceType" value="java.lang.String"/>
        <text-property name="scriptText">
           return src.toString().tokenize(",")
        </text-property>
    </properties>
</transformer>

The sourceType property tells the transformer only to accept source objects of type string. This property can be set multiple times to register different source types.

The scriptText property specifies the groovy script to invoke.

The returnClass attribute on the transformer ensures that we will always get a java.util.List back from this transformer.

Document generated by Confluence on Nov 27, 2006 10:27