javax.script
Class AbstractScriptEngine

java.lang.Object
  extended by javax.script.AbstractScriptEngine
All Implemented Interfaces:
ScriptEngine

public abstract class AbstractScriptEngine
extends Object
implements ScriptEngine

Provides a standard implementation for various versions of the eval() methods. Stores the ScriptContext, and provides access to the Bindings stored in the context. The class is not synchronized. The default context - SimpleScriptContext - is not synchronized either. Sub-classes must provide synchronization if necessary. See Javadoc of Java Scripting API


Field Summary
protected  ScriptContext context
          The default ScriptContext of this AbstractScriptEngine
 
Fields inherited from interface javax.script.ScriptEngine
ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME
 
Constructor Summary
AbstractScriptEngine()
          Constructs a ScriptEngine using an uninitialized SimpleScriptContext.
AbstractScriptEngine(Bindings bindings)
          Constructs a ScriptEngine using the specified Bindings as its ENGINE_SCOPE.
 
Method Summary
 Object eval(Reader reader)
          Evaluates a script obtained using the specified reader as the script source.
 Object eval(Reader reader, Bindings bindings)
          Evaluates a script obtained using a reader as the script source and using the specified namespace as the ENGINE_SCOPE.
 Object eval(String script)
          Evaluates a script contained in a String and returns the resultant object.
 Object eval(String script, Bindings bindings)
          Evaluates a piece of script using the specified namespace as the ENGINE_SCOPE.
 Object get(String key)
          Retrieves the value which is associated with the specified key in the state of the ScriptEngine.
 Bindings getBindings(int scope)
          Retrieves a reference to the associated bindings for the specified scope.
 ScriptContext getContext()
          Returns the default ScriptContext of the ScriptEngine whose Bindings, Readers and Writers are used for script executions when no ScriptContext is specified.
protected  ScriptContext getScriptContext(Bindings bindings)
          Returns a SimpleScriptContext which: uses the specified Bindings as the ScriptEngine#ENGINE_SCOPE has the same GLOBAL_SCOPE Bindings as the current context uses the same Reader, Writer and Error Writer as the current context
 void put(String key, Object value)
          Associates a key and a value in the ScriptEngine ENGINE_SCOPE bindings.
 void setBindings(Bindings bindings, int scope)
          Associates the specified bindings with the specified scope.
 void setContext(ScriptContext context)
          Sets the default ScriptContext of the ScriptEngine whose Bindings, Readers and Writers are used for script executions when no ScriptContext is specified.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.script.ScriptEngine
createBindings, eval, eval, getFactory
 

Field Detail

context

protected ScriptContext context
The default ScriptContext of this AbstractScriptEngine

Constructor Detail

AbstractScriptEngine

public AbstractScriptEngine()
Constructs a ScriptEngine using an uninitialized SimpleScriptContext.


AbstractScriptEngine

public AbstractScriptEngine(Bindings bindings)
Constructs a ScriptEngine using the specified Bindings as its ENGINE_SCOPE.

Parameters:
bindings - the Bindings to be used as the ENGINE_SCOPE
Throws:
NullPointerException - if the parameter is null
Method Detail

eval

public Object eval(Reader reader)
            throws ScriptException
Evaluates a script obtained using the specified reader as the script source. Returns null for scripts that don't return a value.

Specified by:
eval in interface ScriptEngine
Parameters:
reader - the source of the script
Returns:
the value of the evaluated script
Throws:
ScriptException - if an error occurs

eval

public Object eval(Reader reader,
                   Bindings bindings)
            throws ScriptException
Evaluates a script obtained using a reader as the script source and using the specified namespace as the ENGINE_SCOPE. Returns null for scripts that don't return a value.

Specified by:
eval in interface ScriptEngine
Parameters:
reader - the script source used to obtained the script
bindings - the bindings to be used as ENGINE_SCOPE
Returns:
the value of the evaluated script
Throws:
ScriptException - if an error occurs

eval

public Object eval(String script)
            throws ScriptException
Evaluates a script contained in a String and returns the resultant object. Returns null for scripts that don't return a value.

Specified by:
eval in interface ScriptEngine
Parameters:
script - the String representation of the script
Returns:
the value of the evaluated script
Throws:
ScriptException - if an error occurs

eval

public Object eval(String script,
                   Bindings bindings)
            throws ScriptException
Evaluates a piece of script using the specified namespace as the ENGINE_SCOPE. Returns null for scripts that don't return a value.

Specified by:
eval in interface ScriptEngine
Parameters:
script - the String representation of the script
bindings - the bindings to be used as the ENGINE_SCOPE
Returns:
the value of the evaluated script
Throws:
ScriptException - if an error occurs

get

public Object get(String key)
Retrieves the value which is associated with the specified key in the state of the ScriptEngine.

Specified by:
get in interface ScriptEngine
Parameters:
key - the key associated with value.
Returns:
an object value which is associated with the key

getBindings

public Bindings getBindings(int scope)
Retrieves a reference to the associated bindings for the specified scope. Possible scopes are: GLOBAL_SCOPE : if the ScriptEngine was created by ScriptingEngineManager then GLOBAL_SCOPE of it is returned (or null if there is no GLOBAL_SCOPE stored in the ScriptEngine). ENGINE_SCOPE : the set of key-value pairs stored in the ScriptEngine is returned.

Specified by:
getBindings in interface ScriptEngine
Parameters:
scope - the specified scope
Returns:
associated bindings for the specified scope

getScriptContext

protected ScriptContext getScriptContext(Bindings bindings)
Returns a SimpleScriptContext which:

Parameters:
bindings - the bindings to be associated with ENGINE_SCOPE
Returns:
an instance of SimpleScriptContext
Throws:
NullPointerException - if bindings is null

put

public void put(String key,
                Object value)
Associates a key and a value in the ScriptEngine ENGINE_SCOPE bindings.

Specified by:
put in interface ScriptEngine
Parameters:
key - the specified key associated with the value
value - value which is to be associated with the specified key

setBindings

public void setBindings(Bindings bindings,
                        int scope)
Associates the specified bindings with the specified scope.

Specified by:
setBindings in interface ScriptEngine
Parameters:
bindings - bindings to be associated with the specified scope
scope - scope which the bindings should be associated with

getContext

public ScriptContext getContext()
Returns the default ScriptContext of the ScriptEngine whose Bindings, Readers and Writers are used for script executions when no ScriptContext is specified.

Specified by:
getContext in interface ScriptEngine
Returns:
The default ScriptContext of the ScriptEngine

setContext

public void setContext(ScriptContext context)
Sets the default ScriptContext of the ScriptEngine whose Bindings, Readers and Writers are used for script executions when no ScriptContext is specified.

Specified by:
setContext in interface ScriptEngine
Parameters:
context - scriptContext that will replace the default ScriptContext in the ScriptEngine.


Copyright © 1999-2012 The Apache Software Foundation. All Rights Reserved.