Class BSHInterpreter
- java.lang.Object
-
- org.zkoss.zk.scripting.util.GenericInterpreter
-
- org.zkoss.zk.scripting.bsh.BSHInterpreter
-
- All Implemented Interfaces:
HierarchicalAware,Interpreter,SerializableAware
public class BSHInterpreter extends GenericInterpreter implements SerializableAware, HierarchicalAware
The interpreter that uses BeanShell to interpret zscript codes.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 callScopes.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 Constructor Description BSHInterpreter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancontains(java.lang.String name)Tests whether a variable is defined in this interpreter.protected booleancontains(Scope scope, java.lang.String name)Tests whether a variable is defined in the interpreter's scope associated with the specified scope.voiddestroy()Reset the owner (GenericInterpreter.getOwner()) to null.protected voidexec(java.lang.String script)Executes the specified script.protected java.lang.Objectget(java.lang.String name)Gets the variable from the interpreter.protected java.lang.Objectget(Scope scope, java.lang.String name)Gets the variable from the interpreter's scope associated with the giving scope.java.lang.Class<?>getClass(java.lang.String clsnm)Returns null since retrieving class is not supported.org.zkoss.xel.FunctiongetFunction(java.lang.String name, java.lang.Class[] argTypes)Returns null since retrieving methods is not supported.org.zkoss.xel.FunctiongetFunction(Scope scope, java.lang.String name, java.lang.Class[] argTypes)Returns null since retrieving methods is not supported.java.lang.ObjectgetNativeInterpreter()Returns the native interpreter, or null if it is not initialized or destroyed.voidinit(Page owner, java.lang.String zslang)Initializes the interpreter.protected voidloadDefaultImports(bsh.NameSpace bshns)Called when the top-level BeanShell scope is created.voidread(java.io.ObjectInputStream s)Reads the name and value of the variable from the specified input stream.protected voidset(java.lang.String name, java.lang.Object val)Sets the variable to the interpreter.protected voidset(Scope scope, java.lang.String name, java.lang.Object val)Sets the variable to the interpreter's scope associated with the giving scope.protected voidunset(java.lang.String name)Removes the variable from the interpreter.protected voidunset(Scope scope, java.lang.String name)Removes the variable from the interpreter.voidwrite(java.io.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, unsetVariable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.zkoss.zk.scripting.HierarchicalAware
containsVariable, getVariable, setVariable, unsetVariable
-
-
-
-
Method Detail
-
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
protected void exec(java.lang.String script)
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
protected boolean contains(java.lang.String name)
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
protected boolean contains(Scope scope, java.lang.String name)
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
protected java.lang.Object get(java.lang.String name)
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
protected java.lang.Object get(Scope scope, java.lang.String name)
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
protected void set(java.lang.String name, java.lang.Object val)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
protected void set(Scope scope, java.lang.String name, java.lang.Object val)
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
protected void unset(java.lang.String name)
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
protected void unset(Scope scope, java.lang.String name)
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
public void init(Page owner, java.lang.String zslang)
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
public java.lang.Object 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
public java.lang.Class<?> getClass(java.lang.String clsnm)
Description copied from class:GenericInterpreterReturns null since retrieving class is not supported.- Specified by:
getClassin interfaceInterpreter- Overrides:
getClassin classGenericInterpreter
-
getFunction
public org.zkoss.xel.Function getFunction(java.lang.String name, java.lang.Class[] argTypes)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
public org.zkoss.xel.Function getFunction(Scope scope, java.lang.String name, java.lang.Class[] argTypes)
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
public void write(java.io.ObjectOutputStream s, SerializableAware.Filter filter) throws java.io.IOExceptionDescription 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:
java.io.IOException
-
read
public void read(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundExceptionDescription copied from interface:SerializableAwareReads the name and value of the variable from the specified input stream.- Specified by:
readin interfaceSerializableAware- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException- See Also:
SerializableAware.write(java.io.ObjectOutputStream, org.zkoss.zk.scripting.SerializableAware.Filter)
-
-