Platform Engineering

Edward McLoughlin

Upgrade your Groovy Scripts in Mule with DSLDs

Posted by Edward McLoughlin on 29 September 2015

tech, mule, groovy

The groovy script transformer component is a very powerful piece of Mule. I use it for almost every message transformation that I need to make. Its XML and JSON libraries reduce message format conversion down to little more than building a map, while closures give you some powerful processing tools for handling complex data structures.

I’ve discovered a way to give these scripts a little extra integration into Eclipse (and therefore Anypoint Studio). Groovy scripts come with standard script properties like ‘flowVars’, ‘sessionVars’ and ‘message’. However, Anypoint Studio doesn’t have the info required to know what type these properties are, so code completion doesn’t work with them. The good news is that DSLDs (a little talked about feature that comes as a part of the Groovy plugin in Eclipse) are designed for just this particular problem.

To make these files work for you, you’ll need to install the Groovy Eclipse plugin in Anypoint Studio, and enable the Groovy nature on your project (Right click -> Configure -> Convert to Groovy Project).

Once that’s done, simply include the below file in your project somewhere (it doesn’t have to be on the build path) and Anypoint Studio should start using it as a part of your code completion in groovy scripts.

So here is a DSLD that will give you code completion for the standard Groovy script properties. You might find it useful to customise it for your own scripts as well. Further details about these standard properties can be found in the Mule documentation.

  contribute(currentType(subType('groovy.lang.Script'))) {
  property name : 'log', type : 'org.apache.commons.logging.Log', provider : 'Mule', doc : 'A logger that can be used to write to Mule\'s log file.'
  property name : 'muleContext', type : 'org.mule.api.MuleContext', provider : 'Mule', doc : 'A reference to the MuleContext object.'
  property name : 'message', type : 'org.mule.api.MuleMessage', provider : 'Mule', doc : 'The current message.'
  property name : 'originalPayload', type : 'java.lang.Object', provider : 'Mule', doc : 'The original payload object.'
  property name : 'payload', type : 'java.lang.Object', provider : 'Mule', doc : 'The transformed payload of the current message.'
  property name : 'src', type : 'java.lang.Object', provider : 'Mule', doc : 'The transformed payload of the current message.'
  property name : 'result', type : 'java.lang.Object', provider : 'Mule', doc : '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.'
  property name : 'flowVars', type : 'java.util.Map<String, Object>', provider : 'Mule', doc : 'Flow variable map, defined by earlier components in the flow.'
  property name : 'sessionVars', type : 'java.util.Map<String, Object>', provider : 'Mule', doc : 'Session variable map, defined by earlier components in the flow.'
  property name : 'exception', type : 'java.lang.Throwable', provider : 'Mule', doc : 'If an exception payload was generated before this script, this will contain the exception, otherwise it will be null.'
  property name : 'eventContext', type : 'org.mule.api.MuleEventContext', provider : 'Mule', doc : 'An event context for the current mule event.'
  property name : 'id', type : 'java.lang.String', provider : 'Mule', doc : 'The mule event\'s id.'
  property name : 'flowConstruct', type : 'org.mule.api.construct.FlowConstruct', provider : 'Mule', doc : 'The mule event\'s flow construct.'
  }
view rawmule-script.dsld hosted with ❤ by GitHub