/** * Creates a Script with a given scriptName and binding. * * @param scriptName name of the script to run * @param binding the binding to pass to the script * @return the script object * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public Script createScript(String scriptName, Binding binding) throws ResourceException, ScriptException { return InvokerHelper.createScript(loadScriptByName(scriptName), binding); }
/** * Parses the given script and returns it ready to be run. When running in a secure environment * (-Djava.security.manager) codeSource.getCodeSource() determines what policy grants should be * given to the script. * * @param codeSource * @return ready to run script */ public Script parse(final GroovyCodeSource codeSource) throws CompilationFailedException { return InvokerHelper.createScript(parseClass(codeSource), context); }
public static Object runScript(Class scriptClass, String[] args) { Binding context = new Binding(args); Script script = createScript(scriptClass, context); return invokeMethod(script, "run", EMPTY_ARGS); }
public Object build(Class viewClass) { if (Script.class.isAssignableFrom(viewClass)) { Script script = InvokerHelper.createScript(viewClass, this); return build(script); } else { throw new RuntimeException("Only scripts can be executed via build(Class)"); } }
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; }
/** * 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); } }
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(); }
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(); }
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 void testCreateScriptWithNullClass() { Script script = InvokerHelper.createScript(null, new Binding(bindingVariables)); assertEquals(bindingVariables, script.getBinding().getVariables()); }
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)); } }
} else { Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
public GroovyScript getLifeCycle(ShellSession session, String name) throws CommandException, NullPointerException { TimestampedObject<Class<? extends GroovyScript>> ref = scriptCache.getClass(name); if (ref != null) { Class<? extends GroovyScript> scriptClass = ref.getObject(); GroovyScript script = (GroovyScript)InvokerHelper.createScript(scriptClass, new Binding(session)); script.setBinding(new Binding(session)); return script; } else { return null; } }
return scriptClass; } else { final Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
} else { Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
public static Object runScript(Class scriptClass, String[] args) { Binding context = new Binding(args); Script script = createScript(scriptClass, context); return invokeMethod(script, "run", EMPTY_ARGS); }
public static Object runScript(Class scriptClass, String[] args) { Binding context = new Binding(args); Script script = createScript(scriptClass, context); return invokeMethod(script, "run", EMPTY_ARGS); }
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; }