private Object evaluate(final String expression) { Script script; if (SCRIPTS.containsKey(expression)) { script = SCRIPTS.get(expression); } else { script = SHELL.parse(expression); SCRIPTS.put(expression, script); } return script.run(); }
/** * Run a script identified by name with a given binding. * * @param scriptName name of the script to run * @param binding the binding to pass to the script * @return an object * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public Object run(String scriptName, Binding binding) throws ResourceException, ScriptException { return createScript(scriptName, binding).run(); }
/** * Evaluates some script against the current Binding and returns the result * * @param codeSource * @throws CompilationFailedException */ public Object evaluate(GroovyCodeSource codeSource) throws CompilationFailedException { Script script = parse(codeSource); return script.run(); }
public Writer writeTo(Writer out) { Script scriptObject = InvokerHelper.createScript(script.getClass(), binding); PrintWriter pw = new PrintWriter(out); scriptObject.setProperty("out", pw); scriptObject.run(); pw.flush(); return out; }
/** * Write the template document with the set binding applied to the writer. * * @see groovy.lang.Writable#writeTo(java.io.Writer) */ public Writer writeTo(Writer writer) { Binding binding; if (map == null) binding = new Binding(); else binding = new Binding(map); Script scriptObject = InvokerHelper.createScript(script.getClass(), binding); PrintWriter pw = new PrintWriter(writer); scriptObject.setProperty("out", pw); scriptObject.run(); pw.flush(); return writer; }
return ((Script) goo).run();
public void run() { try { Script script = (Script) script1Class.newInstance(); script.run(); completed [0] = true; } catch (Exception e) { e.printStackTrace(); } } };
/** * Evaluate an expression. */ public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException { try { Class scriptClass = evalScripts.get(script); if (scriptClass == null) { scriptClass = loader.parseClass(script.toString(), source); evalScripts.put(script, scriptClass); } else { LOG.fine("eval() - Using cached script..."); } //can't cache the script because the context may be different. //but don't bother loading parsing the class again Script s = InvokerHelper.createScript(scriptClass, context); return s.run(); } catch (Exception e) { throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); } }
/** * Evaluates some script against the current Binding and returns the result * * @param in the stream reading the script * @param fileName is the logical file name of the script (which is used to create the class name of the script) */ public Object evaluate(Reader in, String fileName) throws CompilationFailedException { Script script = null; try { script = parse(in, fileName); return script.run(); } finally { if (script != null) { InvokerHelper.removeClass(script.getClass()); } } }
return ((Script) goo).run();
public void run() { try { Class cls = groovyLoader.loadClass("Script2", true, true); Script script = (Script) cls.newInstance(); script.run(); completed [1] = true; } catch (Exception e) { e.printStackTrace(); } } };
protected void assertScript(final String text, final String scriptName) throws Exception { log.info("About to execute script"); log.info(text); GroovyCodeSource gcs = (GroovyCodeSource) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return new GroovyCodeSource(text, scriptName, "/groovy/testSupport"); } }); Class groovyClass = loader.parseClass(gcs); Script script = InvokerHelper.createScript(groovyClass, new Binding()); script.run(); }
public Object parseClassFromScript(String script) { Class<?> parsedClass = groovyClassLoader.parseClass(script); Script instance = null; try { instance = (Script) parsedClass.newInstance(); instance.setBinding(scriptBinding); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return instance.run(); } }
protected void assertScriptFile(String fileName) throws Exception { log.info("About to execute script: " + fileName); Class groovyClass = loader.parseClass(new GroovyCodeSource(new File(fileName))); Script script = InvokerHelper.createScript(groovyClass, new Binding()); script.run(); }
private Object runScript(Script script) { groovyEvaluator.getScriptBinding().setVariable(Evaluator.BEAKER_VARIABLE_NAME, groovyEvaluator.getBeakerX()); script.setBinding(groovyEvaluator.getScriptBinding()); return script.run(); }
public void run() { final long id = Thread.currentThread().getId(); // run the script numIter times for (int i = 0; i < numIter; i++) { Builder builder = new Builder(); Binding binding = new Binding(); binding.setVariable("builder", builder); script = InvokerHelper.createScript(scriptClass, binding); script.run(); } latch.countDown(); } }
public Object build(Script script) { // this used to be synchronized, but we also used to remove the // metaclass. Since adding the metaclass is now a side effect, we // don't need to ensure the meta-class won't be observed and don't // need to hide the side effect. MetaClass scriptMetaClass = script.getMetaClass(); script.setMetaClass(new FactoryInterceptorMetaClass(scriptMetaClass, this)); script.setBinding(this); Object oldScriptName = getProxyBuilder().getVariables().get(SCRIPT_CLASS_NAME); try { getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, script.getClass().getName()); return script.run(); } finally { if(oldScriptName != null) { getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, oldScriptName); } else { getProxyBuilder().getVariables().remove(SCRIPT_CLASS_NAME); } } }
/** * When a method is not found in the current script, checks that it's possible to call a method closure from the binding. * * @throws IOException * @throws CompilationFailedException * @throws IllegalAccessException * @throws InstantiationException */ public void testInvokeMethodFallsThroughToMethodClosureInBinding() throws IOException, CompilationFailedException, IllegalAccessException, InstantiationException { String text = "if (method() == 3) { println 'succeeded' }"; GroovyCodeSource codeSource = new GroovyCodeSource(text, "groovy.script", "groovy.script"); GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader()); Class clazz = loader.parseClass(codeSource); Script script = ((Script) clazz.newInstance()); Binding binding = new Binding(); binding.setVariable("method", new MethodClosure(new Dummy(), "method")); script.setBinding(binding); script.run(); }
protected void executeScript(Class scriptClass, Permission missingPermission) { try { Script script = InvokerHelper.createScript(scriptClass, new Binding()); script.run(); //InvokerHelper.runScript(scriptClass, null); } catch (AccessControlException ace) { if (missingPermission != null && missingPermission.implies(ace.getPermission())) { return; } else { fail(ace.toString()); } } if (missingPermission != null) { fail("Should catch an AccessControlException"); } }
public void testCreateScriptWithScriptClass() { GroovyClassLoader classLoader = new GroovyClassLoader(); String controlProperty = "text"; String controlValue = "I am a script"; String code = controlProperty + " = '" + controlValue + "'"; GroovyCodeSource codeSource = new GroovyCodeSource(code, "testscript", "/groovy/shell"); Class scriptClass = classLoader.parseClass(codeSource, false); Script script = InvokerHelper.createScript(scriptClass, new Binding(bindingVariables)); assertEquals(bindingVariables, script.getBinding().getVariables()); script.run(); assertEquals(controlValue, script.getProperty(controlProperty)); } }