Refine search
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(); }
private Object runScript(Script script) { groovyEvaluator.getScriptBinding().setVariable(Evaluator.BEAKER_VARIABLE_NAME, groovyEvaluator.getBeakerX()); script.setBinding(groovyEvaluator.getScriptBinding()); return script.run(); }
String line; String lineCountName = "count"; s.setProperty(lineCountName, BigInteger.ZERO); String autoSplitName = "split"; s.setProperty("out", pw); s.setProperty("line", line); s.setProperty(lineCountName, ((BigInteger)s.getProperty(lineCountName)).add(BigInteger.ONE)); s.setProperty(autoSplitName, line.split(splitPattern)); Object o = s.run();
public void setProperty(String property, Object newValue) { if ("binding".equals(property)) setBinding((Binding) newValue); else if("metaClass".equals(property)) setMetaClass((MetaClass)newValue); else binding.setVariable(property, newValue); }
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; }
@Override public Object run(Map<String, Object> variables) { script.setBinding(variables == null ? null : new Binding(variables)); return script.run(); }
/** * 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(); }
public void set(Object value) { script.getBinding().setVariable(variable, value); } }
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 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)); } }
@Test public void testReuseBindingInScript() throws Exception { Class clazz = assertScript("ReuseBindingVerticleScript"); Script script = (Script) clazz.newInstance(); Binding binding = new Binding(); binding.setVariable("myobject", new Object()); script.setBinding(binding); ScriptVerticle verticle = new ScriptVerticle(script); assertDeploy((vertx, onDeploy) -> vertx.deployVerticle(verticle, onDeploy)); assertTrue(isStarted()); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if(request instanceof HttpServletRequest){ HttpServletRequest hr = (HttpServletRequest) request; HttpSession session = hr.getSession(false); if(session!=null){ Object userId = session.getAttribute("userId"); if(userId != null){ Groovity groovity = (Groovity) servletContext.getAttribute(GroovityServlet.SERVLET_CONTEXT_GROOVITY_INSTANCE); Script factory; try { factory = groovity.load("/data/factory", new Binding()); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new ServletException(e); } Object user = factory.invokeMethod("call", new String[]{"person",userId.toString()}); if(user!=null && user instanceof Principal){ request = new AuthenticatedRequestWrapper(hr, (Principal)user); } } } } chain.doFilter(request, response); }
public void testCreateScriptWithNullClass() { Script script = InvokerHelper.createScript(null, new Binding(bindingVariables)); assertEquals(bindingVariables, script.getBinding().getVariables()); }
/** * Return a script object with the given vars from the compiled script object */ @SuppressWarnings("unchecked") private Script createScript(Object compiledScript, Map<String, Object> vars) throws InstantiationException, IllegalAccessException { Class scriptClass = (Class) compiledScript; Script scriptObject = (Script) scriptClass.newInstance(); Binding binding = new Binding(); binding.getVariables().putAll(vars); scriptObject.setBinding(binding); return scriptObject; }
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(); }
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(); } }
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); } } }
Map bindings = script.getBinding().getVariables(); bindings.put("SQL", SQL); script.run(); bindings.clear();
private Object invokePropertyOrMissing(Object object, String methodName, Object[] originalArguments, boolean fromInsideClass, boolean isCallToSuper) { // if no method was found, try to find a closure defined as a field of the class and run it Object value = null; final MetaProperty metaProperty = this.getMetaProperty(methodName, false); if (metaProperty != null) value = metaProperty.getProperty(object); else { if (object instanceof Map) value = ((Map)object).get(methodName); } if (value instanceof Closure) { // This test ensures that value != this If you ever change this ensure that value != this Closure closure = (Closure) value; MetaClass delegateMetaClass = closure.getMetaClass(); return delegateMetaClass.invokeMethod(closure.getClass(), closure, CLOSURE_DO_CALL_METHOD, originalArguments, false, fromInsideClass); } if (object instanceof Script) { Object bindingVar = ((Script) object).getBinding().getVariables().get(methodName); if (bindingVar != null) { MetaClass bindingVarMC = ((MetaClassRegistryImpl) registry).getMetaClass(bindingVar); return bindingVarMC.invokeMethod(bindingVar, CLOSURE_CALL_METHOD, originalArguments); } } return invokeMissingMethod(object, methodName, originalArguments, null, isCallToSuper); }