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

MEL Cheat Sheet

Server, Mule, Application and Message Variables

Overview of the global contexts and the variables to which they give access to.

server mule app message

fileSeparator

clusterId

encoding

id

host

home

name

rootId

ip

nodeId

standalone

correlationId

locale

version

workdir

correlationSequence

javaVersion

registry

correlationGroupSize

javaVendor

replyTo

osName

dataType

osArch

payload

osVersion

inboundProperties

systemProperties

inboundAttachments

timeZone

outboundProperties

tmpDir

outboundAttachments

userName

exception

userHome

userDir

Create a directory named target in the system’s temporary directory and set it as the current payload:


         
      
1
2
3
4
5
<expression-component>
    targetDir = new java.io.File(server.tmpDir, 'target');
    targetDir.mkdir();
    payload = targetDir
</expression-component>

Set the username and password for an HTTP endpoint at runtime based on inbound message properties:


         
      
1
<https:outbound-endpoint address="https://#[message.inboundProperties.username]:     #[message.inboundProperties.password]@api.acme.com/v1/users" />

Flow and Session Variables

Flow variables are available in the flowVars context or directly as top level variables (unless autoResolveVariables is false or their name violates the MVEL naming conventions). Session variables are available in the sessionVars context.

To create a session variable named sessionId whose value is the concatenation of the current message ID, "@" and the Mule instance node ID, use either set-session-variable or MEL code, both shown hereafter:


         
      
1
2
<set-session-variable variableName="sessionId" value="#[message.id+'@'+mule.nodeId]" />
<expression-component>sessionVars.sessionId = message.id+'@'+mule.nodeId;</expression-component>

The following shows how a bean can be dynamically retrieved from the registry, based on a name created by taking a value from a bean payload with a getTargetService() accessor:


         
      
1
2
<set-variable variableName="beanName" value="#[message.payload.targetService+'Processor']" />
<set-variable variableName="bean" value="#[app.registry[beanName]]" />

Payload and Attachments

Copy the current payload in a flow variable named originalPayload then restore it:


         
      
1
2
<set-variable variableName="originalPayload" value="#[message.payload]" />
<set-payload value="#[originalPayload]" />

To retrieve the message payload in a particular format, using Mule’s auto-transformation capability, use payloadAs:


         
      
1
<logger message="#[message.payloadAs(java.lang.String)]" />

To extract all *.txt and *.xml attachments, use a filtered projection:


         
      
1
<expression-transformer expression="#[($.value in  message.inboundAttachments.entrySet() if $.key ~= '(.*\\.txt|.*\\.xml)')]" />

To ask for a null payload:


         
      
1
#[payload is NullPayload]

Regex Support

Regular expression helper functions retrieve null, a single value or an array of values, depending on matches. The forms that take a melExpression argument apply the regex to the result of its evaluation instead of message.payload.


         
      
1
regex(regularExpression [, melExpression [, matchFlags]])

For example to select all the lines of the payload that begin with To:, From:, or Cc: use:


         
      
1
regex('^(To|From|Cc):')

XPath Support

XPath helper functions return DOM4J nodes. By default the XPath expression is evaluated on message.payload unless an xmlElement is specified:


         
      
1
xpath(xPathExpression [, xmlElement])

To get the text content of an element or an attribute:


         
      
1
2
#[xpath('//title').text]
#[xpath('//title/@id').value]

JSON Processing

MEL has no direct support for JSON. The json-to-object-transformer can turn a JSON payload into a hierarchy of simple data structures that are easily parsed with MEL. For example, the following uses a filtered projection to build the equivalent of the $..[? (@.title=='Moby Dick')].price JSON path expression:


         
      
1
2
3
<json:json-to-object-transformer returnClass="java.lang.Object" />
<expression-transformer
    expression='#[($.price in message.payload if $.title =='Moby Dick')[0]]" />

More MVEL Goodness

Quick access to the MVEL 2.0 Documentation: http://goo.gl/AjceB

Java interoperability, for example to create a random UUID and use it as an XSL-T parameter:


         
      
1
2
<mulexml:context-property key="transactionId"
                          value="#[java.util.UUID.randomUUID().toString()]" />

Safe bean property navigation, for example to retrieve fullName only if the name object is not null:


         
      
1
<set-variable variableName="fullName" value="#[message.payload.?name.fullName]" />

Local variable assignment, as in this splitter expression that splits a multi-line payload in rows and drops the first row:


         
      
1
2
splitter expression='#[rows=StringUtils.split(message.payload,'\n\r');
                       ArrayUtil.subarray(rows,1,rows.size())]" />

"Elvis" operator, to return the first non-null value of a list of values:


         
      
1
#[message.payload.userName or message.payload.userId]

Global Configuration

Define global imports, aliases and global functions in the global configuration element. Global functions can be loaded from the file system, a URL, or a classpath resource (see extraFunctions.mvel below). Flow variables auto-binding can be turned off via the autoResolveVariables attribute.


         
      
1
2
3
4
5
6
7
8
9
10
11
<configuration>
  <expression-language autoResolveVariables="false">
    <import class="org.mule.util.StringUtils" />
    <import name="rsu" class="org.apache.commons.lang.RandomStringUtils" />
    <alias name="appName" expression="app.name" />
    <global-functions file="extraFunctions.mvel">
      def reversePayload() { StringUtils.reverse(payload) }
      def randomString(size) { rsu.randomAlphanumeric(size) }
    </global-functions>
  </expression-language>
</configuration>