Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Scripting Module Reference

The scripting module provides facilities for using scripting languages in Mule. Any scripting languages that support http://www.jcp.org/en/jsr/detail?id=223[JSR-223] can be used inside Mule. Scripts can be used as implementations of components or transformers. Also, scripts can be used for expression evaluations, meaning message routing can be controlled using script evaluations on the current message. You can even configure Mule instances from scripts.

Component

Defines a script component backed by a JSR-223 compliant script engine such as Groovy, JavaScript, or Ruby. Scripting allows you to either directly embed your script inside the XML config or reference a script using Spring’s dynamic language support: http://static.springframework.org/spring/docs/2.5.x/reference/dynamic-language.html.

Table 1. Attributes of <component…​>
Name Type Required Default Description

script-ref

string

no

A reference to a script object bean, that is, a <script:script …​> definition.

Table 2. Child Elements of <component…​>
Name Cardinality Description

script

0..1

A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.

java-interface-binding

0..*

A binding associates a Mule endpoint with an injected Java interface (this is like using Spring to inject a bean, but instead of calling a method on the bean a message is sent to an endpoint). Script bindings will only work with Java-based scripting languages. Right now there is no validation on when languages support Java bindings because there are so many scripting languages.

The following example demonstrates how to configure a Groovy script component with an in-line script:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:script="http://www.mulesoft.org/schema/mule/scripting"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xsi:schemaLocation="
       http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.4/mule.xsd
       http://www.mulesoft.org/schema/mule/scripting
       http://www.mulesoft.org/schema/mule/scripting/3.4/mule-scripting.xsd
       http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.4/mule-vm.xsd">
 
    <vm:connector name="vmConnector"/>
 
    <script:script name="myScript" engine="groovy">
        return "$payload Received"
    </script:script>           <flow name="inlineScript">             <vm:inbound-endpoint path="vm:in1"/>
            <script:component>
                <script:script engine="groovy">
                    return "$payload Received"
                </script:script>             </script:component>
            <vm:outbound-endpoint path="out1"/>
        </flow> ...

The following example demonstrates how to orchestrate message flows using bindings. The example calls out to two different flows and passes the results on to the outbound router.


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<flow name="scriptWithBindings">
            <inbound>
                <inbound-endpoint ref="client_request"/>
            </inbound>
            <script:component>
                <script:script engine="groovy">
                    msg = CalloutService.doSomething(payload)
                    return CalloutService.doSomethingElse(msg)
                </script:script>                 <script:java-interface-binding interface="org.mule.components.script.CalloutService" method="doSomething">                     <outbound-endpoint ref="callout_1" synchronous="true"/>
                </script:java-interface-binding>                 <script:java-interface-binding interface="org.mule.components.script.CalloutService" method="doSomethingElse">                     <outbound-endpoint ref="callout_2" synchronous="true"/>
                </script:java-interface-binding>             </script:component>
            <outbound>
                <pass-through-router>
                    <outbound-endpoint ref="client_response"/>
                </pass-through-router>
            </outbound>
        </flow>           <flow name="Callout1">             <inbound>                 <inbound-endpoint ref="callout_1"/>
            </inbound>             <test:component appendString=" Received by #[mule:context.serviceName]"/>
        </flow>           <flow name="Callout2">             <inbound>                 <inbound-endpoint ref="callout_2"/>
            </inbound>             <test:component appendString=" Received by #[mule:context.serviceName]"/>
        </flow>

Script

A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.

Table 3. Attributes of <script…​>
Name Type Required Default Description

name

string

no

The name used to identify this script object. This is used when you want to reference this script object from a component or transformer.

engine

string

no

The name of the script engine being used. All scripting languages that support JSR-223 have a script engine name such as groovy, ruby, python, etc. If this value is not set, but a script file is configured, Mule will attempt to load the correct script engine according to the script file’s extension.

file

string

no

The script file to load for this object. The file can be on the classpath or local file system.

Table 4. Child Elements of <script…​>
Name Cardinality Description

text

0..1

Used for embedding script code inside the XML. This is useful for simple scripts where you are just mocking up a quick application.

Script

Represents a script that can be used as a component for a service or a transformer. The script text can be pulled in from a script file or can be embedded inside this element. A script can be executed by any JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.

Table 5. Attributes of <script…​>
Name Type Required Default Description

name

string

no

The name used to identify this script object. This is used when you want to reference this script object from a component or transformer.

engine

string

no

The name of the script engine being used. All scripting languages that support JSR-223 have a script engine name such as groovy, ruby, python, etc. If this value is not set, but a script file is configured, Mule will attempt to load the correct script engine according to the script file’s extension.

file

string

no

The script file to load for this object. The file can be on the classpath or local file system.

Table 6. Child Elements of <script…​>
Name Cardinality Description

text

0..1

Used for embedding script code inside the XML. This is useful for simple scripts where you are just mocking up a quick application.

Script

A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.

Table 7. Attributes of <script…​>
Name Type Required Default Description

name

string

no

The name used to identify this script object. This is used when you want to reference this script object from a component or transformer.

engine

string

no

The name of the script engine being used. All scripting languages that support JSR-223 have a script engine name such as groovy, ruby, python, etc. If this value is not set, but a script file is configured, Mule will attempt to load the correct script engine according to the script file’s extension.

file

string

no

The script file to load for this object. The file can be on the classpath or local file system.

Table 8. Child Elements of <script…​>
Name Cardinality Description

text

0..1

Used for embedding script code inside the XML. This is useful for simple scripts where you are just mocking up a quick application.

Script

A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.

Table 9. Attributes of <script…​>
Name Type Required Default Description

name

string

no

The name used to identify this script object. This is used when you want to reference this script object from a component or transformer.

engine

string

no

The name of the script engine being used. All scripting languages that support JSR-223 have a script engine name such as groovy, ruby, python, etc. If this value is not set, but a script file is configured, Mule will attempt to load the correct script engine according to the script file’s extension.

file

string

no

The script file to load for this object. The file can be on the classpath or local file system.

Table 10. Child Elements of <script…​>
Name Cardinality Description

text

0..1

Used for embedding script code inside the XML. This is useful for simple scripts where you are just mocking up a quick application.

Script Context Bindings

When run inside Mule, scripts have a number of objects available to them in the script context:

Name Description

log

A logger that can be used to write to Mule’s log file.

muleContext

A reference to the MuleContext object.

registry

A convenience shortcut to Mule registry (otherwise available via muleContext.registry).

eventContext

A reference to the event context. This allows you to dispatch events progammatically from your script.

message

The current message.

originalPayload

The payload of the current message before any transforms.

payload

The transformed payload of the current message if a transformer is configured on the flow. Otherwise this is the same value as originalPayload.

src

Same as payload, kept for backward compatability.

flowConstruct

A reference to the current flow object.

id

The current message ID.

result

A placeholder object where the result of the script can be written. Usually it’s better to just return a value from the script unless the script method doesn’t have a return value.

If your script needs to return null, you must set result=null instead of simply returning null

message properties

Any message properties can be used as variables for the script.

Scripting Module

The scripting module provides facilities for using scripting languages in Mule. Any scripting languages that supports JSR-223 can be used inside Mule. Scripts can be used as implementations of service components or transformers. Also, scripts can be used for expression evaluations, meaning message routing can be controlled using script evaluations on the current message. You can even configure Mule instances from scripts.

Script Context Bindings

When run inside Mule, scripts have a number of objects available to them in the script context. These are:

Name Description

log

a logger that can be used to write to Mule’s log file.

muleContext

a reference to the MuleContext object.

eventContext

A reference to the eventcontext. This allows you to dispatch events progammatically from your script

message

the current message.

originalPayload

the payload of the current message before any transforms.

payload

the transformed payload of the current message if a transformer is configured on the service. Otherwise this is the same value as originalPayload.

src

same as payload, kept for backward compatability.

service

a reference to the current service object.

id

the current event id.

result

a placeholder object where the result of the script can be written to. Usually it’s better to just return a value from the script unless the script method doesn’t have a return value.

Transformers

These are transformers specific to this transport. Note that these are added automatically to the Mule registry at start up. When doing automatic transformations these will be included when searching for the correct transformers.

Name Description

transformer

Runs a script to perform transformation on the current message.

Filters

Filters can be used to control which data is allowed to continue in the flow.

Name Description

filter

Runs a script to perform filter on the current message.

Component

Defines a script component backed by a JSR-223 compliant script engine such as Groovy, JavaScript, or Ruby. Scripting allows you to either directly embed your script inside the XML config or reference a script using Spring’s dynamic language support: http://static.springframework.org/spring/docs/2.5.x/reference/dynamic-language.html.

Table 11. Attributes of <component…​>
Name Type Required Default Description

script-ref

string

no

A reference to a script object bean, that is, a <script:script …​> definition.

Table 12. Child Elements of <component…​>
Name Cardinality Description

script

0..1

A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.

java-interface-binding

0..*

A binding associates a Mule endpoint with an injected Java interface (this is like using Spring to inject a bean, but instead of calling a method on the bean a message is sent to an endpoint). Script bindings will only work with Java-based scripting languages. Right now there is no validation on when languages support Java bindinngs because there are so many scripting languages.

Script

Represents a script that can be used as a component for a service or a transformer. The script text can be pulled in from a script file or can be embedded inside this element. A script can be executed by any JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.

Table 13. Attributes of <script…​>
Name Type Required Default Description

name

string

no

The name used to identify this script object. This is used when you want to reference this script object from a component or transformer.

engine

string

no

The name of the script engine being used. All scripting languages that support JSR-223 have a script engine name such as groovy, ruby, python, etc. If this value is not set, but a script file is configured, Mule will attempt to load the correct script engine according to the script file’s extension.

file

string

no

The script file to load for this object. The file can be on the classpath or local file system.

Table 14. Child Elements of <script…​>
Name Cardinality Description

text

0..1

Used for embedding script code inside the XML. This is useful for simple scripts where you are just mocking up a quick application.

Groovy refreshable

A wrapper for a component object that allows the underlying object to be reloaded at runtime. This makes it possible to hot-deploy new component logic without restarting.

Table 15. Attributes of <groovy-refreshable…​>
Name Type Required Default Description

name

string

yes

The name for this refreshable groovy bean wrapper.

refreshableBean-ref

string

no

The reference to a groovy.lang.Groovy object to use for this component.

methodName

string

no

The entrypoint method to invoke when a message is received for the object.

Table 16. Child Elements of <groovy-refreshable…​>
Name Cardinality Description

Lang

This element allows the http://www.springframework.org/schema/lang namespace to be embedded. Within this element developers can include the Spring lang namespace.

Table 17. Attributes of <lang…​>
Name Type Required Default Description
Table 18. Child Elements of <lang…​>
Name Cardinality Description

Script Configuration Builder

The ScriptConfigurationBuilder allows developers to create a Mule instance from a JSR-223 compliant script. To load the manager from Groovy:

                ConfigurationBuilder builder = new ScriptConfigurationBuilder("groovy", "../conf/mule-config.groovy");                MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext(builder);

Or to start the server from the command line:

mule -M-Dorg.mule.script.engine=groovy
                -builder org.mule.module.scripting.builders.ScriptConfigurationBuilder
                -config ../conf/mule-config.groovy

For more information about configuring a Mule instance from code or script see [Configuration Overview].

To use Groovy as an example, the following transformer configuration converts a comma-separated string of values to a java.util.List:


         
      
1
2
3
4
5
6
7
8
9
<script:transformer name="stringReplaceWithParams">
        <script:script engine="groovy">
            <property key="oldStr" value="l"/>
            <property key="newStr" value="x"/>
            <script:text>
                return payload.toString().replaceAll("$oldStr", "$newStr")
            </script:text>         </script:script>
    </script:transformer>

Groovy refreshable

A wrapper for a component object that allows the underlying object to be reloaded at runtime. This makes it possible to hot-deploy new component logic without restarting.

Table 19. Attributes of <groovy-refreshable…​>
Name Type Required Default Description

name

string

yes

The name for this refreshable groovy bean wrapper.

refreshableBean-ref

string

no

The reference to a groovy.lang.Groovy object to use for this component.

methodName

string

no

The entry point method to invoke when a message is received for the object.

Table 20. Child Elements of <groovy-refreshable…​>
Name Cardinality Description

Lang

This element allows the http://www.springframework.org/schema/lang namespace to be embedded. Within this element developers can include the Spring lang namespace.

Table 21. Attributes of <lang…​>
Name Type Required Default Description
Table 22. Child Elements of <lang…​>
Name Cardinality Description