Class BSHInterpreter
- All Implemented Interfaces:
HierarchicalAware,Interpreter,SerializableAware
Unlike many other implementations, it supports the hierarchical
scopes (HierarchicalAware).
That is, it uses an independent BeanShell NameSpace
(a.k.a. interpreter's scope) to store the variables/classes/methods
defined in BeanShell script for each ZK scope (Scope).
Since one-to-one relationship between BeanShell's scope and ZK scope,
the invocation of BeanShell methods can execute correctly without knowing
what scope it is.
However, if you want your codes portable across different interpreters,
you had better to call
Scopes.beforeInterpret(org.zkoss.zk.ui.ext.Scope)
to prepare the proper scope, before calling any method defined in
zscript.
How serialization work?
First, all NameSpace objects have to serialize. Second,
the top-level namespace (GlobalNS) is wrapped with NSWrap, which
is not serializable. It is serialized when SerializableAware.write(java.io.ObjectOutputStream, org.zkoss.zk.scripting.SerializableAware.Filter)
is called (triggered by PageImpl's write).
On the other hand, all non-top-level namespaces (NS) are wrapped with NSWrapSR which is serializable, so they are serialized with the attributes of a ID space owner being serialized.
- Author:
- tomyeh
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.zkoss.zk.scripting.SerializableAware
SerializableAware.Filter -
Field Summary
Fields inherited from class org.zkoss.zk.scripting.util.GenericInterpreter
UNDEFINED -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanTests whether a variable is defined in this interpreter.protected booleanTests whether a variable is defined in the interpreter's scope associated with the specified scope.voiddestroy()Reset the owner (GenericInterpreter.getOwner()) to null.protected voidExecutes the specified script.protected ObjectGets the variable from the interpreter.protected ObjectGets the variable from the interpreter's scope associated with the giving scope.Class<?>Returns null since retrieving class is not supported.org.zkoss.xel.FunctiongetFunction(String name, Class[] argTypes) Returns null since retrieving methods is not supported.org.zkoss.xel.FunctiongetFunction(Scope scope, String name, Class[] argTypes) Returns null since retrieving methods is not supported.Returns the native interpreter, or null if it is not initialized or destroyed.voidInitializes the interpreter.protected voidloadDefaultImports(bsh.NameSpace bshns) Called when the top-level BeanShell scope is created.voidReads the name and value of the variable from the specified input stream.protected voidSets the variable to the interpreter.protected voidSets the variable to the interpreter's scope associated with the giving scope.protected voidRemoves the variable from the interpreter.protected voidRemoves the variable from the interpreter.voidwrite(ObjectOutputStream s, SerializableAware.Filter filter) Writes the name and value of the variables of this namespace to the specified stream.Methods inherited from class org.zkoss.zk.scripting.util.GenericInterpreter
afterExec, afterInterpret, beforeExec, beforeInterpret, containsVariable, containsVariable, getCurrent, getFromNamespace, getFromNamespace, getImplicit, getLanguage, getOwner, getVariable, getVariable, interpret, setVariable, setVariable, unsetVariable, unsetVariableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.zkoss.zk.scripting.HierarchicalAware
containsVariable, getVariable, setVariable, unsetVariable
-
Constructor Details
-
BSHInterpreter
public BSHInterpreter()
-
-
Method Details
-
loadDefaultImports
protected void loadDefaultImports(bsh.NameSpace bshns) Called when the top-level BeanShell scope is created. By default, it does nothing.Note: to speed up the performance, this implementation disabled
NameSpace.loadDefaultImports(). It only imports the java.lang and java.util packages. If you want the built command and import packages, you can override this method. For example,protected void loadDefaultImports(NameSpace bshns) { bshns.importCommands("/bsh/commands"); }- Since:
- 3.0.2
-
exec
Description copied from class:GenericInterpreterExecutes the specified script. Deriving class shall provide an implementation of this method, rather than overridingGenericInterpreter.interpret(java.lang.String, org.zkoss.zk.ui.ext.Scope).- Specified by:
execin classGenericInterpreter
-
contains
Description copied from class:GenericInterpreterTests whether a variable is defined in this interpreter. Optional. Implement it if the interpreter can tell the difference between null and undefined.By default, it tests whether
GenericInterpreter.get(String)returns non-null.- Overrides:
containsin classGenericInterpreter
-
contains
Description copied from class:GenericInterpreterTests whether a variable is defined in the interpreter's scope associated with the specified scope. Optional. Implement it if the interpreter can tell the difference between null and undefined.By default, it tests whether
GenericInterpreter.get(Scope, String)returns non-null.- Overrides:
containsin classGenericInterpreter
-
get
Description copied from class:GenericInterpreterGets the variable from the interpreter. Optional. Implement it if you want to expose variables defined in the interpreter to Java codes.GenericInterpreter.beforeExec()is called first, before this method is invoked.An empty (and fake) scope is pushed so
GenericInterpreter.getFromNamespace(java.lang.String)always returns null.- Overrides:
getin classGenericInterpreter
-
get
Description copied from class:GenericInterpreterGets the variable from the interpreter's scope associated with the giving scope. Optional. Implement it if you want to expose variables defined in the interpreter to Java codes.This method is implemented only if the interpreter that supports hierarchical scopes (
HierarchicalAware).Default: the same as
GenericInterpreter.get(String).GenericInterpreter.beforeExec()is called first, before this method is invoked.An empty (and fake) scope is pushed so
GenericInterpreter.getFromNamespace(java.lang.String)always returns null.- Overrides:
getin classGenericInterpreter
-
set
Description copied from class:GenericInterpreterSets the variable to the interpreter. Optional. Implement it if you want to allow Java codes to define a variable in the interpreter.GenericInterpreter.beforeExec()is called first, before this method is invoked.- Overrides:
setin classGenericInterpreter
-
set
Description copied from class:GenericInterpreterSets the variable to the interpreter's scope associated with the giving scope. Optional. Implement it if you want to allow Java codes to define a variable in the interpreter.This method is implemented only if the interpreter that supports hierarchical scopes (
HierarchicalAware).Default: the same as
GenericInterpreter.set(String, Object).GenericInterpreter.beforeExec()is called first, before this method is invoked.- Overrides:
setin classGenericInterpreter
-
unset
Description copied from class:GenericInterpreterRemoves the variable from the interpreter. Optional. Implement it if you want to allow Java codes to undefine a variable from the interpreter.GenericInterpreter.beforeExec()is called first, before this method is invoked.- Overrides:
unsetin classGenericInterpreter
-
unset
Description copied from class:GenericInterpreterRemoves the variable from the interpreter. Optional. Implement it if you want to allow Java codes to undefine a variable from the interpreter.This method is implemented only if the interpreter that supports hierarchical scopes (
HierarchicalAware).Default: the same as
GenericInterpreter.unset(String).GenericInterpreter.beforeExec()is called first, before this method is invoked.- Overrides:
unsetin classGenericInterpreter
-
init
Description copied from interface:InterpreterInitializes the interpreter. It is called once when the new instance of interpreter is constructed.- Specified by:
initin interfaceInterpreter- Overrides:
initin classGenericInterpreterzslang- the language this interpreter is associated with
-
destroy
public void destroy()Description copied from class:GenericInterpreterReset the owner (GenericInterpreter.getOwner()) to null.- Specified by:
destroyin interfaceInterpreter- Overrides:
destroyin classGenericInterpreter
-
getNativeInterpreter
Returns the native interpreter, or null if it is not initialized or destroyed. From application's standpoint, it never returns null, and the returned object must be an instance ofInterpreter- Specified by:
getNativeInterpreterin interfaceInterpreter- Since:
- 3.0.2
-
getClass
Description copied from class:GenericInterpreterReturns null since retrieving class is not supported.- Specified by:
getClassin interfaceInterpreter- Overrides:
getClassin classGenericInterpreter
-
getFunction
Description copied from class:GenericInterpreterReturns null since retrieving methods is not supported.- Specified by:
getFunctionin interfaceInterpreter- Overrides:
getFunctionin classGenericInterpreterargTypes- the list of argument (a.k.a., parameter) types. If null, Class[0] is assumed.
-
getFunction
Description copied from class:GenericInterpreterReturns null since retrieving methods is not supported.- Specified by:
getFunctionin interfaceHierarchicalAware- Overrides:
getFunctionin classGenericInterpreter- Parameters:
scope- the scope used as a reference to identify the correct scope for searching the method. Note: this method doesn't look for any variable stored in scope.argTypes- the list of argument (a.k.a., parameter) types. If null, Class[0] is assumed.
-
write
Description copied from interface:SerializableAwareWrites the name and value of the variables of this namespace to the specified stream.If the variable's value is not serializable, it won't be written.
To read back, use
SerializableAware.read(java.io.ObjectInputStream).- Specified by:
writein interfaceSerializableAware- Throws:
IOException
-
read
Description copied from interface:SerializableAwareReads the name and value of the variable from the specified input stream.- Specified by:
readin interfaceSerializableAware- Throws:
IOExceptionClassNotFoundException- See Also:
-