The interpreter that uses BeanShell to interpret zscript codes.
| Method from org.zkoss.zk.scripting.bsh.BSHInterpreter Detail: |
protected boolean contains(String name) {
try {
return _ip.getNameSpace().getVariable(name) != Primitive.VOID;
//Primitive.VOID means not defined
} catch (UtilEvalError ex) {
throw UiException.Aide.wrap(ex);
}
}
|
protected boolean contains(Namespace ns,
String name) {
if (ns != null) {
final NameSpace bshns = prepareNS(ns);
//note: we have to create NameSpace (with prepareNS)
//to have the correct chain
if (bshns != _bshns) {
try {
return bshns.getVariable(name) != Primitive.VOID;
} catch (UtilEvalError ex) {
throw UiException.Aide.wrap(ex);
}
}
}
return contains(name);
}
|
public void destroy() {
getOwner().getNamespace().unsetVariable(VAR_NS, false);
//bug 1814819 ,clear variable, dennis
try{
_bshns.clear();
_ip.setNameSpace(null);
} catch (Throwable t) { //silently ignore (in case of upgrading to new bsh)
}
_ip = null;
_bshns = null;
super.destroy();
}
|
protected void exec(String script) {
try {
final Namespace ns = getCurrent();
if (ns != null) _ip.eval(script, prepareNS(ns));
else _ip.eval(script); //unlikely (but just in case)
} catch (EvalError ex) {
throw UiException.Aide.wrap(ex);
}
}
|
protected Object get(String name) {
try {
return Primitive.unwrap(_ip.get(name));
} catch (EvalError ex) {
throw UiException.Aide.wrap(ex);
}
}
|
protected Object get(Namespace ns,
String name) {
if (ns != null) {
final NameSpace bshns = prepareNS(ns);
//note: we have to create NameSpace (with prepareNS)
//to have the correct chain
if (bshns != _bshns) {
try {
return Primitive.unwrap(bshns.getVariable(name));
} catch (UtilEvalError ex) {
throw UiException.Aide.wrap(ex);
}
}
}
return get(name);
}
|
public Class getClass(String clsnm) {
try {
return _bshns.getClass(clsnm);
} catch (UtilEvalError ex) {
throw new UiException("Failed to load class "+clsnm, ex);
}
}
|
public Function getFunction(String name,
Class[] argTypes) {
return getFunction0(_bshns, name, argTypes);
}
|
public Function getFunction(Namespace ns,
String name,
Class[] argTypes) {
return getFunction0(prepareNS(ns), name, argTypes);
}
|
public Object getNativeInterpreter() {
return _ip;
}
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 of bsh.Interpreter |
public void init(Page owner,
String zslang) {
super.init(owner, zslang);
_ip = new bsh.Interpreter();
_ip.setClassLoader(Thread.currentThread().getContextClassLoader());
_bshns = new GlobalNS(_ip.getClassManager(), "global");
_ip.setNameSpace(_bshns);
}
|
protected void loadDefaultImports(NameSpace bshns) {
}
Called when the top-level BeanShell namespace is created.
By default, it does nothing.
Note: to speed up the performance, this implementation
disabled bsh.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");
}
|
public void read(ObjectInputStream s) throws ClassNotFoundException, IOException {
for (;;) {
final String nm = (String)s.readObject();
if (nm == null) break; //no more
set(nm, s.readObject());
}
try {
for (;;) {
final BshMethod mtd = (BshMethod)s.readObject();
if (mtd == null) break; //no more
//fix declaringNameSpace
Field f = null;
boolean acs = false;
try {
f = Classes.getAnyField(BshMethod.class, "declaringNameSpace");
acs = f.isAccessible();
Fields.setAccessible(f, true);
f.set(mtd, _bshns);
} catch (Throwable ex) {
throw UiException.Aide.wrap(ex);
} finally {
if (f != null) Fields.setAccessible(f, acs);
}
_bshns.setMethod(mtd.getName(), mtd);
}
} catch (UtilEvalError ex) {
throw UiException.Aide.wrap(ex);
}
for (;;) {
final String nm = (String)s.readObject();
if (nm == null) break; //no more
_bshns.importClass(nm);
}
for (;;) {
final String nm = (String)s.readObject();
if (nm == null) break; //no more
_bshns.importPackage(nm);
}
}
|
protected void set(String name,
Object val) {
try {
_ip.set(name, val);
//unlike NameSpace.setVariable, _ip.set() handles null
} catch (EvalError ex) {
throw UiException.Aide.wrap(ex);
}
}
|
protected void set(Namespace ns,
String name,
Object val) {
if (ns != null) {
final NameSpace bshns = prepareNS(ns);
//note: we have to create NameSpace (with prepareNS)
//to have the correct chain
if (bshns != _bshns) {
try {
bshns.setVariable(
name, val != null ? val: Primitive.NULL, false);
return;
} catch (UtilEvalError ex) {
throw UiException.Aide.wrap(ex);
}
}
}
set(name, val);
}
|
protected void unset(String name) {
try {
_ip.unset(name);
} catch (EvalError ex) {
throw UiException.Aide.wrap(ex);
}
}
|
protected void unset(Namespace ns,
String name) {
if (ns != null) {
final NameSpace bshns = prepareNS(ns);
//note: we have to create NameSpace (with prepareNS)
//to have the correct chain
if (bshns != _bshns) {
bshns.unsetVariable(name);
return;
}
}
unset(name);
}
|
public void write(ObjectOutputStream s,
Filter filter) throws IOException {
//1. variables
final String[] vars = _bshns.getVariableNames();
for (int j = vars != null ? vars.length: 0; --j >= 0;) {
final String nm = vars[j];
if (nm != null && !"bsh".equals(nm)) {
final Object val = get(nm);
if ((val == null || (val instanceof java.io.Serializable)
|| (val instanceof java.io.Externalizable))
&& (filter == null || filter.accept(nm, val))) {
s.writeObject(nm);
s.writeObject(val);
}
}
}
s.writeObject(null); //denote end-of-vars
//2. methods
final BshMethod[] mtds = _bshns.getMethods();
for (int j = mtds != null ? mtds.length: 0; --j >= 0;) {
final String nm = mtds[j].getName();
if (filter == null || filter.accept(nm, mtds[j])) {
//hack BeanShell 2.0b4 which cannot be serialized correctly
Field f = null;
boolean acs = false;
try {
f = Classes.getAnyField(BshMethod.class, "declaringNameSpace");
acs = f.isAccessible();
Fields.setAccessible(f, true);
final Object old = f.get(mtds[j]);
try {
f.set(mtds[j], null);
s.writeObject(mtds[j]);
} finally {
f.set(mtds[j], old);
}
} catch (java.io.IOException ex) {
throw ex;
} catch (Throwable ex) {
throw UiException.Aide.wrap(ex);
} finally {
if (f != null) Fields.setAccessible(f, acs);
}
}
}
s.writeObject(null); //denote end-of-mtds
//3. imported class
Field f = null;
boolean acs = false;
try {
f = Classes.getAnyField(NameSpace.class, "importedClasses");
acs = f.isAccessible();
Fields.setAccessible(f, true);
final Map clses = (Map)f.get(_bshns);
if (clses != null)
for (Iterator it = clses.values().iterator(); it.hasNext();) {
final String clsnm = (String)it.next();
if (!clsnm.startsWith("bsh."))
s.writeObject(clsnm);
}
} catch (java.io.IOException ex) {
throw ex;
} catch (Throwable ex) {
throw UiException.Aide.wrap(ex);
} finally {
if (f != null) Fields.setAccessible(f, acs);
}
s.writeObject(null); //denote end-of-cls
//4. imported package
f = null;
acs = false;
try {
f = Classes.getAnyField(NameSpace.class, "importedPackages");
acs = f.isAccessible();
Fields.setAccessible(f, true);
final Collection pkgs = (Collection)f.get(_bshns);
if (pkgs != null)
for (Iterator it = pkgs.iterator(); it.hasNext();) {
final String pkgnm = (String)it.next();
if (!pkgnm.startsWith("java.awt")
&& !pkgnm.startsWith("javax.swing"))
s.writeObject(pkgnm);
}
} catch (java.io.IOException ex) {
throw ex;
} catch (Throwable ex) {
throw UiException.Aide.wrap(ex);
} finally {
if (f != null) Fields.setAccessible(f, acs);
}
s.writeObject(null); //denote end-of-cls
}
|