Java Reference
In-Depth Information
The ScriptContext Interface and the
SimpleScriptContext Class
An instance of the ScriptContext interface acts as a bridge between the Java host
application and the script engine. It is used to pass the execution context of the Java
host application to the script engine. The script engine may use the context information
while executing a script. A script engine may store its state in an instance of a class that
implements the ScriptContext interface, which may be accessible to the Java host
application.
The SimpleScriptContext class is an implementation of the ScriptContext
interface.
The ScriptException Class
The ScriptException class is an exception class. A script engine throws a
ScriptException if an error occurs during the execution, compilation, or invocation
of a script. The class contains three useful methods called getLineNumber() ,
getColumnNumber() , and getFileName() . These methods report the line number,
the column number, and the file name of the script in which the error occurs. The
ScriptException class overrides the getMessage() method of the Throwable class and
includes the line number, column number, and the file name in the message that it
returns.
Discovering and Instantiating ScriptEngines
You can create a script engine using a ScriptEngineFactory or ScriptEngineManager .
Who is actually responsible for creating a script engine: ScriptEngineFactory ,
ScriptEngineManager , or both? The short answer is that a ScriptEngineFactory is
always responsible for creating instances of a script engine. The next question is “What is
the role of a ScriptEngineManager ?”
A ScriptEngineManager uses the service provider mechanism to locate all
available script engine factories. It searches all JAR files in the CLASSPATH and other
standard directories. It looks for a resource file, which is a text file named javax.
script.ScriptEngineFactory under a directory named META-INF/services . The
resource file consists of the fully qualified names of the classes implementing the
ScriptEngineFactory interface. Each class name is specified in a separate line. The file
may include comments that start with a # character. A sample resource file may have the
following contents that include class names for two script engine factories:
#Java Kishori Script Engine Factory class
com.jdojo.script.JKScriptEngineFactory
#Another factory class
com.jdojo.script.FunScriptFactory
 
Search WWH ::




Custom Search