Package org.wso2.carbon.event.broker.utils

Source Code of org.wso2.carbon.event.broker.utils.RunJavascriptTest

package org.wso2.carbon.event.broker.utils;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public class RunJavascriptTest {
   
   
    public static void execuateJavaScript(String script, String methodName, Object[] args){
        Context cx = Context.enter();
        try {
            Scriptable scope = cx.initStandardObjects();
           
            Object wrappedOut = Context.javaToJS(System.out, scope);
          ScriptableObject.putProperty(scope, "out", wrappedOut);

            // Now evaluate the string we've collected. We'll ignore the result.
            cx.evaluateString(scope, script, "<cmd>", 1, null);

           
            // Call function "f('my arg')" and print its result.
            Object fObj = scope.get(methodName, scope);
            if (!(fObj instanceof Function)) {
                System.out.println("f is undefined or not a function.");
            } else {
                Function f = (Function)fObj;
                Object result = f.call(cx, scope, scope, args);
                String report = "f('my args') = " + Context.toString(result);
                System.out.println(report);
            }
        } finally {
            Context.exit();
        }
    }
   

    /**
     * @param args
     */
    public static void main(String[] args) {
        String script = "function onEvent(messageAsStr){ system.sendMessage(messageAsStr); out.println(42); return 50;}";
        //execuateJavaScript(script, "onEvent", new String[]{"hello"});
       
       
        JavaScriptExecuter executer = new JavaScriptExecuter(null);
        executer.execuateScript(script, "onEvent", new String[]{"hello"});
       
//        Context cx = Context.enter();
//        try {
//            // Initialize the standard objects (Object, Function, etc.)
//            // This must be done before scripts can be executed. Returns
//            // a scope object that we use in later calls.
//            Scriptable scope = cx.initStandardObjects();
//
//            Object wrappedOut = Context.javaToJS(System.out, scope);
//            ScriptableObject.putProperty(scope, "out", wrappedOut);
//           
//            // Collect the arguments into a single string.
//            String s = "out.println(42)";
//           
//
//            // Now evaluate the string we've colected.
//            Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
//
//            // Convert the result to a string and print it.
//            System.err.println(Context.toString(result));
//
//        } finally {
//            // Exit from the context.
//            Context.exit();
//        }


    }

}
TOP

Related Classes of org.wso2.carbon.event.broker.utils.RunJavascriptTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.