Scripting In Java Platform
Happy New Year to All
One key feature of Java 6.0 is change in (Java Specification Request (JSR) 223), helps developers to integrate Java technology and scripting languages by defining a standard framework and application programming interface (API) to do the following:
-
Access and control Java technology-based objects from a scripting environment
-
Create web content with scripting languages
-
Embed scripting environments within Java technology-based applications
This article focuses on the specification’s third goal and will show you how to use an embedded scripting environment from a Java platform application. A demo application called ScriptCalc will provide a working example of how to extend your applications with user-defined scripts in the JavaScript programming language.
Basic Reasons to Use a Scripting Language
Most scripting languages are dynamically typed. You can usually create new variables without predetermining the variable type, and you can reuse variables to store values of different types. Also, scripting languages tend to perform many type conversions automatically, for example, converting the number 10 to the text “10″ as necessary. Although some scripting languages are compiled, most languages are interpreted. Script environments generally perform the script compilation and execution within the same process. Usually, these environments also parse and compile scripts into intermediate code when they are first executed.These qualities of scripting languages help you write applications faster, execute commands repeatedly, and tie together components from different technologies.
Combining scripting languages with the Java platform provides developers an opportunity to leverage the abilities of both environments. You can continue to use scripting languages for all the reasons you already have, and you can use the powerful Java class library to extend the abilities of those languages. If you are a Java language programmer, you now have the ability to ship applications that your customers can significantly and dynamically customize. The synergy between the Java platform and scripting languages produces an environment in which developers and end users can collaborate to create more useful, dynamic applications.
JSR 223 Implementation
Version 6 of the Java Platform, Standard Edition (Java SE), does not mandate any particular script engine, but it does include the Mozilla Rhino engine for the JavaScript programming language. The Java SE 6 platform implements the java.script APIs, which allow you to use script engines that comply with JSR 223.
Ways to Use the Scripting API
The scripting API is in the javax.script package available in the Java SE 6 platform. The API is still relatively small, composed of six interfaces and six classes, as Table 1 indicates.
|
Table 1: Interfaces and Classes in the Java SE 6 Platform |
||
|
|
||
| Interface | Class | |
| Bindings | AbstractScriptEngine | |
| Compilable | CompiledScript | |
| Invocable | ScriptEngineManager | |
| ScriptContext | SimpleBindings | |
| ScriptEngine | SimpleScriptContext | |
| ScriptEngineFactory | ScriptException | |
|
|
||
Your starting point should be the ScriptEngineManager class. A ScriptEngineManager object can tell you what script engines are available to the Java Runtime Environment (JRE). It can also provide ScriptEngine objects that interpret scripts written in a specific scripting language. The simplest way to use this API is to do the following:
-
Create a ScriptEngineManager object.
-
Retrieve a ScriptEngine object from the manager.
-
Evaluate a script using the ScriptEngine object.
That sounds easy enough, but what does the code look like? Above Example performs all three steps, printing Hello, world! to the console.
Create a ScriptEngine object using the engine name.
|
ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine jsEngine = mgr.getEngineByName(”JavaScript”); try { jsEngine.eval(”print(’Hello, world!’)”); } catch (ScriptException ex) { ex.printStackTrace(); } |
Below code shows how to evaluate a file that the customer has supplied. The file name is /scripts/F1.js, and it is located under the application directory.
The eval method can read script files.
|
ScriptEngineManager engineMgr = new ScriptEngineManager(); ScriptEngine engine = engineMgr.getEngineByName(”ECMAScript”); InputStream is = this.getClass().getResourceAsStream(”/scripts/F1.js”); try { Reader reader = new InputStreamReader(is); engine.eval(reader); } catch (ScriptException ex) { ex.printStackTrace(); } |
How to Invoke a Script Procedure
Running entire scripts is useful, but you may want to invoke only specific script procedures. Some script engines implement the Invocable interface. If an engine implements this interface, you can call or invoke specific methods or functions that the engine has already evaluated. Script engines are not required to support the Invocable interface. However, the Rhino JavaScript technology implementation in the Java SE 6 platform does. If your script contains a function called sayHello, you could invoke it repeatedly by casting your ScriptEngine object to an Invocable object and by calling its invokeFunction method. Alternatively, if your script defines objects, you can call object methods using the invokeMethod method. Code Example 7 demonstrates how to use this interface. You can use the Invocable interface to call specific methods in a script.
|
jsEngine.eval(”function sayHello() {” + “ println(’Hello, world!’);” + “}”); Invocable invocableEngine = (Invocable) jsEngine; invocableEngine.invokeFunction(”sayHello”); |
Be aware that invokeMethod and invokeFunction methods can throw several exceptions, so you must be prepared to catch ScriptException, NoSuchMethodException, and perhaps even NullPointerException exceptions.
More Information : http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/
