Ajax programming with the Java Scripting API

An excerpt from 'Beginning Java SE 6 Platform: From novice to professional'

1 2 3 4 5 Page 2
Page 2 of 5

After determining the appropriate script engine, the program can invoke ScriptEngineFactory's ScriptEngine getScriptEngine() method to return an instance of the script engine associated with the factory. Although new script engines are usually returned, a factory implementation is free to pool, reuse, or share implementations. The following code fragment shows how to accomplish this task:

if (factory.getLanguageName ().equals ("ECMAScript"))
{
    engine = factory.getScriptEngine ();
    break;
}

Think of the code fragment as being part of Listing 1's for (ScriptEngineFactory factory: factories) loop; assume that the ScriptEngine variable engine already exists. If the scripting language hosted by the factory is ECMAScript (language version does not matter in this example), a script engine is obtained from the factory and the loop is terminated.

Because the previous approach to obtaining a script engine is cumbersome, ScriptEngineManager provides three convenience methods that take on this burden, listed in Table 1. These methods let you obtain a script engine based on file extension (possibly obtained via a dialog-selected script file), MIME type (possibly returned from a server), and short name (possibly chosen from a menu).

Table 1. ScriptEngineManager convenience methods for obtaining a script engine

MethodDescription
public ScriptEngine getEngineByExtension(String extension)Creates and returns a script engine that corresponds to the given extension If a script engine is not available, this method returns null. A NullPointerException is thrown if null is passed as extension.
public ScriptEngine getEngineByMimeType(String mimeType)Creates and returns a script engine that corresponds to the given MIME type If a script engine is not available, this method returns null. A NullPointerException is thrown if null is passed as mimeType.
public ScriptEngine getEngineByName(String shortName)Creates and returns a script engine that corresponds to the given short name. If a script engine is not available, this method returns null. A NullPointerException is thrown if null is passed as shortName.

Listing 2 presents an application that invokes getEngineByExtension(), getEngineByMimeType(), and getEngineByName() to obtain a Rhino script engine instance. Behind the scenes, these methods take care of enumerating factories and invoking ScriptEngineFactory's getScriptEngine() method to create the script engine.

Listing 2. ObtainScriptEngine.java

// ObtainScriptEngine.java

import javax.script.*;

public class ObtainScriptEngine
{
   public static void main (String [] args)
   {
      ScriptEngineManager manager = new ScriptEngineManager ();

      ScriptEngine engine1 = manager.getEngineByExtension ("js");
      System.out.println (engine1);

      ScriptEngine engine2 =
        manager.getEngineByMimeType ("application/javascript");
      System.out.println (engine2);

      ScriptEngine engine3 = manager.getEngineByName ("rhino");
      System.out.println (engine3);
   }
}

After compiling ObtainScriptEngine.java, running the application generates output that is similar to the following, indicating that different script engine instances are returned:

com.sun.script.javascript.RhinoScriptEngine@1f14ceb
com.sun.script.javascript.RhinoScriptEngine@f0eed6
com.sun.script.javascript.RhinoScriptEngine@691f36


Once a script engine has been obtained (via ScriptEngineFactory's getScriptEngine() method or one of ScriptEngineManager's three convenience methods), a program can access the engine's factory via ScriptEngine's convenient ScriptEngineFactory getFactory() method. The program can also invoke various ScriptEngine methods to evaluate scripts.

Evaluating scripts

After obtaining a script engine, a Java program can work with ScriptEngine's six overloaded eval() methods to evaluate scripts. Each method throws a ScriptException if there is a problem with the script. Assuming successful script evaluation, an eval() method returns the script's result as some kind of Object, or null if the script does not return a value.

1 2 3 4 5 Page 2
Page 2 of 5