Scripting Support in Mustang - An Example You Can Try



Well, it's 1:30 a.m. but feels like late afternoon. Time shift is a funny thing. I thought that if I can't sleep, I could try to play with the new scripting support in Mustang (JSR-223). Well, it's as simple as this:

Preparing JavaScript code (notice I am using java.lang.StringBuffer inside JavaScript - isn't that cool?):

        input.setText("var out = new java.lang.StringBuffer();\\nfor (i=0; i<3; i++)\\n   
                                    out.append('hello, world! ('+i+')\\\\n');");


Executing the code:

	ScriptEngineManager manager = new ScriptEngineManager();
	ScriptEngine jsengine = manager.getEngineByName("js");
        try {
            jsengine.eval(input.getText());
            String out = new String((StringBuffer)jsengine.get("out"));
            output.setText(out);
        } catch (javax.script.ScriptException e) {
            output.setText("ScriptException" + e);
        }

A screenshot:

Full source code is here. To execute, you need to install latest mustang build.

Well, if it's simple as this it may mean future Java code getting "polluted" by scripting code. Programming in scripting languages can be very productive for certain types of problems. Currently, Rhino JavaScript support is available by Sun as reference implementation and PHP support is being written, too. Any other languages may follow - the scripting framework enables any other languages to be plugged in. I really wonder what will be the impact of JSR 223 on web development...
Comments:

You can also use zamples.com, in place of installing last version of JDK. You can copy that code in the window of Zamples, and see result on your right: public class Test { public static void main(String argv[]) { String x = "var out = new java.lang.StringBuffer();\\nfor (i=0; i<3; i++)\\nout.append('hello, world! ('+i+')\\\\n');"; javax.script.ScriptEngineManager manager = new javax.script.ScriptEngineManager(); javax.script.ScriptEngine jsengine = manager.getEngineByName("js"); try { jsengine.eval(x); String out = new String((StringBuffer)jsengine.get("out")); System.out.println(out); } catch (javax.script.ScriptException e) { System.out.println("ScriptException" + e); } } }

Posted by Vincent Brabant on červenec 06, 2005 at 05:24 dop. CEST #

Interesting...

Posted by Roman Strobl on červenec 06, 2005 at 10:38 dop. CEST #

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

Roman Strobl

Search

Archives
« duben 2014
PoÚtStČtSoNe
 
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