Home > Articles > Programming

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Obtain a Script Engine

The entry-point class into the javax.script package is ScriptEngineManager. This class's two constructors are responsible for discovering script engine factories (for creating script engines) on a classloader basis:

  • Constructor public ScriptEngineManager() discovers factories visible to the current thread's classloader (if this classloader is available) or the bootstrap classloader (if the current thread's classloader is not available).
  • Constructor public ScriptEngineManager(ClassLoader loader) discovers factories visible to the specified loader. Passing null to loader is equivalent to invoking the no-argument constructor.

After creating a ScriptEngineManager, you can retrieve a java.util.List of script engine factories by invoking its public List<ScriptEngineFactory> getEngineFactories() method. For each item in this list, various methods of the ScriptEngineFactory interface can be invoked to learn about and obtain an instance of the factory's associated script engine. Examine Listing 1.

Listing 1: ScriptDemo1.java

// ScriptDemo1.java

import java.util.*;

import javax.script.*;

public class ScriptDemo1
   public static void main (String [] args)
      // Create a ScriptEngineManager that discovers all script engine
      // factories (and their associated script engines) that are visible to
      // the current thread's classloader.

      ScriptEngineManager manager = new ScriptEngineManager ();

      // Obtain a list of all discovered factories as ScriptEngineFactories.

      List<ScriptEngineFactory> factories = manager.getEngineFactories ();

      // Display each script engine factory's metadata and create the script
      // engine.

      for (ScriptEngineFactory factory: factories)
           System.out.println ("Full name = " + factory.getEngineName ());

           System.out.println ("Version = " + factory.getEngineVersion ());

           System.out.println ("Extensions");
           List<String> extensions = factory.getExtensions ();
           for (String extension: extensions)
                System.out.println ("   " + extension);

           System.out.println ("Language name = " +
                               factory.getLanguageName ());

           System.out.println ("Language version = " +
                               factory.getLanguageVersion ());

           System.out.println ("MIME Types");
           List<String> mimetypes = factory.getMimeTypes ();
           for (String mimetype: mimetypes)
                System.out.println ("   " + mimetype);

           System.out.println ("Short Names");
           List<String> shortnames = factory.getNames ();
           for (String shortname: shortnames)
                System.out.println ("   " + shortname);

           String [] params =
           for (String param: params)
                System.out.printf ("Parameter %s = %s", param,
                                   factory.getParameter (param));
                System.out.println ();

           ScriptEngine engine = factory.getScriptEngine ();
           System.out.println (engine);

           System.out.println ();

ScriptDemo1 invokes ScriptEngineFactory's public String getEngineName() and public String getEngineVersion() methods to identify a script engine's implementation. In contrast, public String getLanguageName() and public String getLanguageVersion() are invoked to identify the scripting language.

Three methods are invoked to return information for determining whether the script engine can evaluate a given script (based on the script's file extension, the MIME type associated with the script, or the short name of a script engine): public List getExtensions() returns an immutable list of file extensions, public List getMimeTypes() returns an immutable list of MIME types, and public List getNames() returns an immutable list of short engine names.

This application next invokes public Object getParameter(String key) to return the values of attribute keys defined by all script engine factories. These keys are represented by ScriptEngine.ENGINE and similar constants; the values are the Strings returned by getEngineName() and other ScriptEngineFactory methods.

One key not represented by a constant is THREADING. This key's value describes the behavior of the script engine with respect to the concurrent execution of scripts. Returned values include null (not thread-safe), "MULTITHREADED", "THREAD-ISOLATED", and "STATELESS". Refer to getParameter()'s documentation to learn about these values.

Finally, ScriptDemo1 invokes public ScriptEngine getScriptEngine() to return a script engine. You can invoke this object's methods to evaluate scripts (stored in strings or accessible via readers) and perform other tasks.

The following output reveals that Mozilla Rhino-based JavaScript is the only script engine that is included with Mustang:

Full name = Mozilla Rhino
Version = 1.6 release 2
Language name = ECMAScript
Language version = 1.6
MIME Types
Short Names
Parameter javax.script.engine = Mozilla Rhino
Parameter javax.script.engine_version = 1.6 release 2
Parameter javax.script.language = ECMAScript
Parameter javax.script.language_version = 1.6
Parameter javax.script.name = javascript

Although you can work directly with ScriptEngineFactory's getExtensions(), getMimeTypes(), getNames(), and getScriptEngine() methods to obtain a script engine, you will probably find that it is more convenient to work with the following ScriptEngineManager methods, which return a script engine (or null) based on a specific extension, MIME type, or short name:

  • public ScriptEngine getEngineByExtension(String extension) obtains a script engine based on the specified extension. This method internally works with the previous getExtensions() and getScriptEngine() methods.
  • public ScriptEngine getEngineByMimeType(String mimeType) obtains a script engine based on the specified mimeType. This method internally works with the previous getMimeTypes() and getScriptEngine() methods.
  • public ScriptEngine getEngineByName(String shortName) obtains a script engine based on the specified shortName. This method internally works with the previous getNames() and getScriptEngine() methods.

The getEngineByExtension() method is useful for obtaining a script engine based on the extension of a file chooser-selected script. Similarly, the getEngineByMimeType() method is useful for obtaining a script engine based on the MIME type of a script transferred across a network, where the only way to identify the scripting language is via the MIME type. Finally, the getEngineByName() method is useful for obtaining a script engine based on the script engine's short name chosen from a menu of script engine short names.

  • + Share This
  • 🔖 Save To Your Account