public BeanShellTileSourceTemplate(String name, String urlToLoad, String ext, int maxZoom, int minZoom, int tileSize, int bitDensity, int avgSize) { super(name, urlToLoad, ext, maxZoom, minZoom, tileSize, bitDensity, avgSize); bshInterpreter = new Interpreter(); try { bshInterpreter.eval(urlToLoad); bshInterpreter.getClassManager().setClassLoader(new ClassLoader() { @Override public URL getResource(String resName) { return null; } @Override public InputStream getResourceAsStream(String resName) { return null; } @Override public Class<?> loadClass(String className) throws ClassNotFoundException { throw new ClassNotFoundException("Error requesting " + className); } }); } catch (bsh.EvalError e) { log.error("Error executing the map init script " + urlToLoad, e); } }
/** Set an external class loader to be used as the base classloader for BeanShell. The base classloader is used for all classloading unless/until the addClasspath()/setClasspath()/reloadClasses() commands are called to modify the interpreter's classpath. At that time the new paths /updated paths are added on top of the base classloader. <p> BeanShell will use this at the same point it would otherwise use the plain Class.forName(). i.e. if no explicit classpath management is done from the script (addClassPath(), setClassPath(), reloadClasses()) then BeanShell will only use the supplied classloader. If additional classpath management is done then BeanShell will perform that in addition to the supplied external classloader. However BeanShell is not currently able to reload classes supplied through the external classloader. <p> @see BshClassManager#setClassLoader( ClassLoader ) */ public void setClassLoader( ClassLoader externalCL ) { getClassManager().setClassLoader( externalCL ); }
/** Set an external class loader to be used as the base classloader for BeanShell. The base classloader is used for all classloading unless/until the addClasspath()/setClasspath()/reloadClasses() commands are called to modify the interpreter's classpath. At that time the new paths /updated paths are added on top of the base classloader. <p> BeanShell will use this at the same point it would otherwise use the plain Class.forName(). i.e. if no explicit classpath management is done from the script (addClassPath(), setClassPath(), reloadClasses()) then BeanShell will only use the supplied classloader. If additional classpath management is done then BeanShell will perform that in addition to the supplied external classloader. However BeanShell is not currently able to reload classes supplied through the external classloader. <p> @see BshClassManager#setClassLoader( ClassLoader ) */ public void setClassLoader( ClassLoader externalCL ) { getClassManager().setClassLoader( externalCL ); }
/** A command may be implemented as a compiled Java class containing one or more static invoke() methods of the correct signature. The invoke() methods must accept two additional leading arguments of the interpreter and callstack, respectively. e.g. invoke(interpreter, callstack, ... ) This method adds the arguments and invokes the static method, returning the result. */ public static Object invokeCompiledCommand( Class<?> commandClass, Object [] args, Interpreter interpreter, CallStack callstack, SimpleNode callerInfo ) throws UtilEvalError { // add interpereter and namespace to args list Object[] invokeArgs = new Object[args.length + 2]; invokeArgs[0] = interpreter; invokeArgs[1] = callstack; System.arraycopy( args, 0, invokeArgs, 2, args.length ); BshClassManager bcm = interpreter.getClassManager(); try { return invokeStaticMethod( bcm, commandClass, "invoke", invokeArgs, callerInfo ); } catch ( InvocationTargetException e ) { throw new UtilEvalError( "Error in compiled command: " + e.getCause(), e ); } catch ( ReflectError e ) { throw new UtilEvalError("Error invoking compiled command: " + e, e ); } }
/*package*/ NS newNS(Scope scope) { scope = getIdSpace(scope); Scope p = getParentIdSpace(scope); return new NS(p != null ? prepareNS(p) : _bshns, _ip.getClassManager(), scope); //Bug 1831534: we have to pass class manager //Bug 1899353: we have to use _bshns instead of null (Reason: unknown) }
/** * A command may be implemented as a compiled Java class containing one or * more static invoke() methods of the correct signature. The invoke() * methods must accept two additional leading arguments of the interpreter * and callstack, respectively. e.g. invoke(interpreter, callstack, ... ) * This method adds the arguments and invokes the static method, returning * the result. */ public static Object invokeCompiledCommand(Class commandClass, Object[] args, BshEvaluatingVisitor visitor) throws UtilEvalError { // add interpereter and namespace to args list Object[] invokeArgs = new Object[args.length + 2]; invokeArgs[0] = visitor.getInterpreter(); invokeArgs[1] = visitor.getCallstack(); System.arraycopy(args, 0, invokeArgs, 2, args.length); BshClassManager bcm = visitor.getInterpreter().getClassManager(); try { return Reflect.invokeStaticMethod(bcm, commandClass, "invoke", invokeArgs); } catch (InvocationTargetException e) { throw new UtilEvalError("Error in compiled command: " + e.getTargetException(), e); } catch (ReflectError e) { throw new UtilEvalError("Error invoking compiled command: " + e, e); } }
public Class getType(BSHType tnode) throws EvalError { // return cached type if available if ( tnode.type != null ) return tnode.type; // first node will either be PrimitiveType or AmbiguousName SimpleNode node = tnode.getTypeNode(); if ( node instanceof BSHPrimitiveType ) tnode.baseType = ((BSHPrimitiveType)node).getType(); else tnode.baseType = ambiguousNameToClass(((BSHAmbiguousName)node)); if ( tnode.arrayDims > 0 ) { try { // Get the type by constructing a prototype array with // arbitrary (zero) length in each dimension. int[] dims = new int[tnode.arrayDims]; // int array default zeros Object obj = Array.newInstance(tnode.baseType, dims); tnode.type = obj.getClass(); } catch(Exception e) { throw new EvalError("Couldn't construct array type", tnode, callstack ); } } else tnode.type = tnode.baseType; // hack... sticking to first interpreter that resolves this // see comments on type instance variable interpreter.getClassManager().addListener(tnode); return tnode.type; }
BshClassManager bcm = interpreter.getClassManager();
public void init(Page owner, String zslang) { super.init(owner, zslang); _ip = new bsh.Interpreter(); _ip.setClassLoader(Classes.getContextClassLoader(BSHInterpreter.class)); _bshns = new GlobalNS(_ip.getClassManager(), "global"); _ip.setNameSpace(_bshns); }
interpreter.getClassManager().addListener(this);
BshClassManager bcm = interpreter.getClassManager();
public Object invoke(final Map<String, ?> context) throws EvalError { final NameSpace nameSpace = new NameSpace("BeanshellExecutable", _interpreter.getClassManager()); nameSpace.setParent(_interpreter.getNameSpace()); final BshMethod method = new BshMethod(_method.getName(), _method.getReturnType(), _method.getParameterNames(), _method.getParameterTypes(), _method.getParameterModifiers(), _method.methodBody, nameSpace, _method.getModifiers()); for (final Map.Entry<String, ?> entry : context.entrySet()) { try { final Object value = entry.getValue(); nameSpace.setVariable(entry.getKey(), value != null ? value : Primitive.NULL, false); } catch (final UtilEvalError e) { throw new EvalError("cannot set variable '" + entry.getKey() + '\'', null, null, e); } } final Object result = method.invoke(Reflect.ZERO_ARGS, _interpreter); if ( Types.getType(result) == Void.TYPE ) return null; return Primitive.unwrap(result); }
public Object invoke(final Map<String, ?> context) throws EvalError { final NameSpace nameSpace = new NameSpace(_interpreter.getClassManager(), "BeanshellExecutable"); nameSpace.setParent(_interpreter.getNameSpace()); final BshMethod method = new BshMethod(_method.getName(), _method.getReturnType(), _method.getParameterNames(), _method.getParameterTypes(), _method.methodBody, nameSpace, _method.getModifiers()); for (final Map.Entry<String, ?> entry : context.entrySet()) { try { nameSpace.setVariable(entry.getKey(), entry.getValue(), false); } catch (final UtilEvalError e) { throw new EvalError("cannot set variable '" + entry.getKey() + '\'', null, null, e); } } final Object result = method.invoke(new Object[0], new BshEvaluatingVisitor(null, _interpreter)); if (result instanceof Primitive) { if (( (Primitive) result).getType() == Void.TYPE) { return null; } return ( (Primitive) result).getValue(); } return result; }
interpreter == null ? null : interpreter.getClassManager(); Class<?> clas = object.getClass();
public void initService() { bsh = new Interpreter (); BshClassManager bcm = bsh.getClassManager(); try { bcm.setClassPath(getServer().getLoader().getURLs()); } catch (UtilEvalError e) { e.printStackTrace(); } bcm.setClassLoader(getServer().getLoader()); } public void startService() {
private void initRootSystemObject() { BshClassManager bcm = getClassManager(); // bsh setu("bsh", new NameSpace(null, bcm, "Bsh Object" ).getThis( this ) ); // bsh.system setu( "bsh.system", SYSTEM_OBJECT); setu( "bsh.shared", SYSTEM_OBJECT); // alias // bsh.help This helpText = new NameSpace(null, bcm, "Bsh Command Help Text" ).getThis( this ); setu( "bsh.help", helpText ); // bsh.cwd setu( "bsh.cwd", System.getProperty("user.dir") ); // bsh.interactive setu( "bsh.interactive", interactive ? Primitive.TRUE : Primitive.FALSE ); // bsh.evalOnly setu( "bsh.evalOnly", Primitive.FALSE ); }
/** * Invoke method on arbitrary object instance. * invocation may be static (through the object instance) or dynamic. * Object may be a bsh scripted object (bsh.This type). * * @return the result of the method call */ public static Object invokeObjectMethod(Object object, String methodName, Object[] args, BshEvaluatingVisitor visitor, SimpleNode callerInfo) throws ReflectError, EvalError, InvocationTargetException { // Bsh scripted object if (object instanceof This && !This.isExposedThisMethod(methodName)) { return ((This) object).invokeMethod(methodName, args, visitor.getInterpreter(), visitor.getCallstack(), callerInfo, false/*delcaredOnly*/); } // Plain Java object, find the java method try { BshClassManager bcm = visitor.getInterpreter() == null ? null : visitor.getInterpreter().getClassManager(); Class clas = object.getClass(); Method method = resolveExpectedJavaMethod(bcm, clas, object, methodName, args, false); return invokeMethod(method, object, args); } catch (UtilEvalError e) { throw e.toEvalError(callerInfo, visitor.getCallstack()); } }
private void initRootSystemObject() { BshClassManager bcm = getClassManager(); // bsh setu("bsh", new NameSpace( bcm, "Bsh Object" ).getThis( this ) ); setu( "bsh.system", SYSTEM_OBJECT); setu( "bsh.shared", SYSTEM_OBJECT); // alias // bsh.help This helpText = new NameSpace(bcm, "Bsh Command Help Text" ).getThis( this ); setu( "bsh.help", helpText ); // bsh.cwd try { setu( "bsh.cwd", System.getProperty("user.dir") ); } catch ( SecurityException e ) { // applets can't see sys props setu( "bsh.cwd", "." ); } // bsh.interactive setu( "bsh.interactive", new Primitive(interactive) ); // bsh.evalOnly setu( "bsh.evalOnly", new Primitive(evalOnly) ); }
private Interpreter initBSH () throws UtilEvalError, EvalError { Interpreter bsh = new Interpreter (); BshClassManager bcm = bsh.getClassManager(); bcm.setClassPath(getServer().getLoader().getURLs()); bcm.setClassLoader(getServer().getLoader()); bsh.set ("qbean", this); bsh.set ("log", getLog()); bsh.eval (getPersist().getChildTextTrim ("init")); return bsh; } private ISOMsg applyRequestProps (ISOMsg m, Interpreter bsh)
String className = enclosingNameSpace.isClass ? (enclosingNameSpace.getName() + "$" + name) : name; String fqClassName = packageName == null ? className : packageName + "." + className; BshClassManager bcm = interpreter.getClassManager();