Hi Malaka,

That didn't work either but your comment put me into the right direction. I
was able to get it working by using importClass(Packages....JSUtils); in
the javascript.

Thanks a bunch for responding!

Thought of documenting everything in case someone else faces the same issue.

Here is my ESB API

<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse";
     name="MySampleAPI2"
     context="/MySampleAPI2">
   <resource methods="GET">
      <inSequence>
         <send>
            <endpoint>
               <address uri="http://demo3177833.mockable.io/students"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <property name="messageType" value="application/json"
scope="axis2"/>

         <class name="org.example.JSONParseMediator">
            <property name="createJSONPayload" value="true" />
         </class>

         <script language="js" key="MyLocalJSEntry2"
function="executeMain"/>
         <send/>
      </outSequence>
   </resource>
</api>


And here is my JS

function executeMain(mc){

   importClass(Packages.org.example.JSUtils);

   var obj = {};
   var myJSUtilsTest = new JSUtils();
   obj.foo = myJSUtilsTest.getTestMessage("echo 123");
   obj.tee = myJSUtilsTest.getMessage();

}


Your class mediator can be anything but you need to have a plain simple
class like JSUtils with public methods which you want to expose to JS
engine. Make sure to package everything into the final jar file. Deploy
this jar file in <ESB_HOME>/repository/components/lib and restart ESB.

public class JSUtils {

    private static final Log logger =
LogFactory.getLog(JSUtils.class.getName());
    private ScriptEngineManager manager = new ScriptEngineManager();
    private ScriptEngine scriptEngine;

    public JSUtils() {
        manager.registerEngineExtension("jsEngine", new
RhinoScriptEngineFactory());
        scriptEngine = manager.getEngineByExtension("jsEngine");
    }
    public String getTestMessage(String test) {
        return "Testing JSUtils with " + test;
    }
    public Object getMessage() {
        return "Testing JSUtils with getMessage()";
    }

public Object parseJSON(String jsonStr) {
    try {
        return scriptEngine.eval("(" + jsonStr + ")");
    } catch (Exception e) {
        logger.error("Error when creating native object", e);
    }
    return null;
}

public Object stringifyJSON(Object jsonObject) {
    try {
        Object returnObj = ((Invocable)
scriptEngine).invokeMethod(this.scriptEngine.eval("JSON"), "stringify",
jsonObject);
        return returnObj;
    } catch (Exception e) {
        logger.error("Error when creating native object", e);
    }
    return null;
   }
}

This should work with ESB 4.8.0 and 4.8.1. Hope this will be useful.


Thanks.

On Tue, Mar 31, 2015 at 4:18 AM, Malaka Silva <mal...@wso2.com> wrote:

> Hi Akila,
>
> You can try following,
>
>         <script language="js"><![CDATA[
>              importPackage(Packages.org.......JSUtils);
>              ......................
>              var result = JSUtils.parseJSON(someJsonVar);
>  ]]></script>
>
>
> On Tue, Mar 31, 2015 at 3:31 PM, Akila Ravihansa Perera <
> raviha...@wso2.com> wrote:
>
>> Hi,
>>
>> Is it possible to do $subject? I've been playing around with wso2-synapse
>> code base but couldn't find a way around without overriding ScriptMediator
>> class which is something I'm trying to avoid.
>>
>> My requirement is that I've developed some Java Util methods which I
>> would like to expose as JS native functions. I tried writing a class
>> mediator and do the bindings but couldn't get it working.
>>
>> ScriptEngineManager manager = new ScriptEngineManager();
>> manager.registerEngineExtension("jsEngine", new
>> RhinoScriptEngineFactory());
>> ScriptEngine scriptEngine = manager.getEngineByExtension("jsEngine");
>>
>> Bindings bindings = scriptEngine.createBindings();
>> bindings.put("jsUtils", new JSUtils());
>>
>> JSUtils Java class has some string operations that should be accessible
>> via JS. Inside JS script mediator I should be able to something like this;
>>
>> var result = jsUtils.parseJSON(someJsonVar);
>>
>>
>> I know this can be done by purely using the class mediator to evaluate
>> the whole JS source. But I'd much rather use built-in script mediator and
>> only inject my custom Java classes onto it.
>>
>> I think this would be a cool feature to have, if it is not already
>> supported.
>>
>> Thanks.
>>
>> --
>> Akila Ravihansa Perera
>> Software Engineer, WSO2
>>
>> Blog: http://ravihansa3000.blogspot.com
>>
>
>
>
> --
>
> Best Regards,
>
> Malaka Silva
> Senior Tech Lead
> M: +94 777 219 791
> Tel : 94 11 214 5345
> Fax :94 11 2145300
> Skype : malaka.sampath.silva
> LinkedIn : http://www.linkedin.com/pub/malaka-silva/6/33/77
> Blog : http://mrmalakasilva.blogspot.com/
>
> WSO2, Inc.
> lean . enterprise . middleware
> http://www.wso2.com/
> http://www.wso2.com/about/team/malaka-silva/
> <http://wso2.com/about/team/malaka-silva/>
>
> Save a tree -Conserve nature & Save the world for your future. Print this
> email only if it is absolutely necessary.
>



-- 
Akila Ravihansa Perera
Software Engineer, WSO2

Blog: http://ravihansa3000.blogspot.com
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to