Java Reference
In-Depth Information
Summary
You can pass parameters to the script using the
ScriptContext
. An instance of the
Bindings
interface serves as the parameter holder. The
Bindings
interface inherits from
the
Map
interface imposing a restriction that its keys must be nonempty, non-null
String
s.
The
SimpleBinding
class is an implementation of the
Bindings
interface. It is preferred
to use the
createBindings()
method of
ScriptEngine
to get an instance of the
Bindings
interface giving
ScriptEngine
a chance to returned a specialized implementation of the
Bindings
interface. A
Bindings
is associated with a scope that can be an engine scope
or a global scope. Parameters passed in the engine scope takes precedence over the
parameters passed in the global scope. Parameters passed can be local to a script engine,
local to a script execution, or global to all script engines created by a
ScriptManager
.
A script engine executes a script in a context that consists of four components: a set
of
Bindings
, where each
Bindings
is associated with a different scope, a
Reader
that is
used by the script engine to read inputs, a
Writer
that is used by the script engine to write
outputs, and an error
Writer
that is used by the script engine to write error outputs. An
instance of the
ScriptContext
interface represents a context for the script execution. The
SimpleScriptContext
class is an implementation of the
ScriptContext
interface.
The set of
Bindings
in a context is used to pass parameters to the script. A reader
and writers in a context control input source and output destinations of the script,
respectively. For example, by setting a file writer as a writer, you can send all outputs from
a script to a file. Every
ScriptEngine
has a default
ScriptContext
that is used to execute
scripts when no
ScriptContext
is passed to the
eval()
method. The
getContext()
method of
ScriptEngine
returns the default context of the engine. You can also pass a
separate
ScriptContext
with the
eval()
method that will be used to execute your script,
leaving the default
ScriptContext
of the engine untouched.
Search WWH ::
Custom Search