Tuesday, February 27, 2007

Mustang released

The announcement made here is pretty old. It’s been in beta phase for some time and the final release has also been made some time back. Here I visit some of the new features in Mustang. I have been using Eclipse as a preferred java editor for a long time, though I've tried NetBeans, JDeveloper etc. Eclipse 3.2 has support for Mustang.
The feature I would talk about is the Scripting feature. This feature can be used to execute scripts in various languages. Mustang ships with the Rhino scripting engine for JavaScript. Its pretty interesting. I tried out various features of the Rhino Engine.

The major questions which I wanted an answer to were:
1. How to share data between Java and JavaScript?
2. Where is the utility of such scripting?
3. Can I add different Scripting engines?


The answer to the first question you can find in the example below. The sending of data from Java to Javascript was very simple and it even JavaScript recognized objects from user created classes (Java Car class has an object on which getName method could be invoked by the JavaScript). The return value also works. The List created in Java and modified in JS reflected in Java again. Awesome! Totally awesome!
Now the reasons given to use scripting language was what I am not convinced with.(Scripting for the Java Platform --> Reasons to Use a Scripting Language). I think the reasons to use scripting would be more specific, like using existing test scripts in some scripting language.
We can add different scripting engines according to specs.

Sample code explaining most of the concepts:-
import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class TheScript
{
private final static String METHOD_HELLO = "function helloWorld(){"
+ " println('Hello, world!'); " + " return 's';" + "}";

private final static String METHOD_HELLO_NAME = "function helloName(){"
+ " println('Hello,'+ name.getName() +' !'); " + " return 's';"
+ "}";

private final static String METHOD_HELLO_LIST = "function helloList(){"
+ "var x;" + "var names = namesListKey.toArray(); "
+ " for(x in names)" + " {" + " println('Hello,'+ names[x] +' !');"
+ " }" + " namesListKey.add(\"king\");" + "}";

private final static String THE_JAVA_SCRIPT = METHOD_HELLO
+ METHOD_HELLO_NAME + METHOD_HELLO_LIST;

public static void main(String[] args) throws ScriptException,
NoSuchMethodException
{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
jsEngine.eval(TheScript.THE_JAVA_SCRIPT);

// simple java method

// invoke method METHOD_HELLO
Invocable invocableEngine = (Invocable) jsEngine;
invocableEngine.invokeFunction("helloWorld", (Object[]) null);

System.out.println("---------------------------------");
// simple java method where java objects are used by javascript

// The parameter type is user defined object
// which is converted into a javascript object
// i.e I can use getName method of Car class
Car car = new Car();
car.setName("Sash");

// invoke method METHOD_NAME
jsEngine.put("name", car);
Object invokeFunction = invocableEngine.invokeFunction("helloName",
(Object[]) null);

// the return type should have been mapped by script provider
// Mozilla Rhino provides the mapping for number and string types
System.out.println(" --------------------------------- "
+ invokeFunction + " ----");

List namesList = new ArrayList(Arrays.asList(new String[] { "sash",
"siva", "baldie" }));
jsEngine.put("namesListKey", namesList);
invocableEngine.invokeFunction("helloList", (Object[]) null);

System.out.println(namesList);
System.out.println("---------------------------------");

}

public static class Car
{
private String name;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

}
}

References:

http://www.mozilla.org/rhino/ScriptingJava.html
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/

2 comments:

Chaithra said...

Interesting stuff.Will try this out..

~ V ~ said...

the title said Mustang... i was wondering if you were writing about the newer version of the Ford Mustang :-p