calling script functions or methods from Java

In JSR 223, there is an optional interface by the name javax.script.Invocable. If you are using a Script engine that supports this interface (for example, JavaScript reference engine in Mustang supports this interface), then you can call a specific script function from Java. For example, a specific script function may be called from an event handler in Java to implement handler in script.
Simple sample is as follows:

import javax.script.\*;

public class Main {
	public static void main(String[] args) {
		try {
			ScriptEngineManager manager = new ScriptEngineManager();
			ScriptEngine engine = manager.getEngineByName("js");
                        // JavaScript code in a String
			String script = "function hello(name) { print('Hello, ' + name); }";
                        // check whether this engine supports javax.script.Invocable interface
                        // Note that this is an optional interface -- script engines may choose
                        // to omit this!!
			if (engine instanceof Invocable) {
                                // There is also a overload eval method that accepts
                                // arbitrary java.io.Reader object for script input
                                // instead of String as used here...
				engine.eval(script);
				Invocable inv = (Invocable) engine;
                                // call a global script function called   
                                // "hello" passing an array of arguments   
                                // here, we pass one argument 'Sundar'                               
				inv.invoke("hello", new Object[]{"Sundar"});

                                // there is also another overload of 'invoke' method
                                // that can call a script method on a script object
                                // if the scripting language happens to be object based/oriented:

                                // inv.invoke(scriptObj, scriptMethod, arguments);

			}
		} catch (ScriptException exp) {
			exp.printStackTrace();
		} catch (NoSuchMethodException exp) {
                        // this exception will be thrown if there is no script 
                        // function of name specified.
			exp.printStackTrace();
		}
	}
}

Comments:

Is it possible to invoke anonymous javascript function in java code ? For example : JS : object.func1(function check(response) { ... }); Java : public void func1(Object aFunc) { // code to invoke javascript function }

Posted by Laurent Quérel on December 19, 2006 at 10:43 AM IST #

Yes, it is possible. Every JavaScript function is an instance of "Function" prototype and therefore has methods like "apply", "call". So, we can accept a function object and invoke "call" method on it. The following example invokes anonymous function passed to a Java method as an argument. Hope this helps.
import javax.script.\*;

public class t {
    private static ScriptEngine engine;

    public static void main(String[] args) throws Exception {
        ScriptEngineManager man = new ScriptEngineManager();
        engine = man.getEngineByName("js");
        engine.put("obj", new t());
        engine.eval("obj.func1(function () { println('hello'); })");
    }

    public static void func1(Object func) throws Exception {
        ((Invocable)engine).invokeMethod(func, "call", func, null);
    }
}

Posted by A. Sundararajan on December 19, 2006 at 11:56 AM IST #

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder