Search code examples
groovywso2esbapache-synapsewso2-esb

WSO2 ESB Can't run Groovy script stored in registry


I'm trying to execute a Groovy script that I'm storing in the WSO2 ESB Local registry. When I do that I'm getting the following error:

ERROR {org.apache.synapse.mediators.bsf.ScriptMediator} -  The script engine returned a NoSuchMethodException executing the external groovy script : Value {name ='null', keyValue ='file:Scripts/Groovy/test.groovy'} function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
java.lang.NoSuchMethodException: No signature of method: com.sun.script.groovy.GroovyScriptEngine.mediate() is applicable for argument types: (org.apache.synapse.mediators.bsf.ScriptMessageContext) values: [org.apache.synapse.mediators.bsf.ScriptMessageContext@716f8a10]
Possible solutions: wait()

If I put the code in-line in the script mediator, everything is running ok. I've tried to wrap the script code like this <x><![CDATA[...code...]]></x>, as shown in the Using Ruby Scripts for Mediation example: Sample 353: Using Ruby Scripts for Mediation. I add the groovy-all-2.4.7.jar to ESB_HOME\repository\components\lib too.

How can I run the groovy scripts stored in the registry? What am I doing wrong?

Here is the Groovy Script and the proxy, wiht which I'm testing:

Groovy Script

class Example {
   static def DisplayName() {
      println("This is how methods work in groovy");
      println("This is an example of a simple method");
   } 

   static void main(String[] args) {
      DisplayName();
   } 
}

Proxy service

<proxy name="TestScriptProxy" startOnLoad="true" trace="disable"
  transports="http https" xmlns="http://ws.apache.org/ns/synapse">
  <target>
    <inSequence>
      <script language="groovy"><![CDATA[println "This is an in-line script";]]></script>
      <script function="DisplayName"
        key="file:Scripts/Groovy/test.groovy" language="groovy"/>
    </inSequence>
    <outSequence/>
    <faultSequence/>
  </target>
</proxy>

ESB version is 5.0.0 running on Windows 10.


Solution

  • If you have the Groovy script in the registry, the source should be the registry path. For example : gov:scripts/Groovy/test.groovy.

    If you are referring a file give path relative to ESB_HOME. For example if scripts folder is in ESB_HOME directory, file:scripts/Groovy/test.groovy

    First load the script file as below :

    <localEntry key="DisplayNameScript" src="file:scripts/Groovy/test.groovy"/>
    

    Now call the function

    <script language="groovy" key="DisplayNameScript" function="DisplayName"/>