Refine search
/** * Loads a set of given beans * @param resources The resources to load * @throws IOException */ public void loadBeans(Resource[] resources) throws IOException { Closure beans = new Closure(this){ @Override public Object call(Object... args) { return beans((Closure)args[0]); } }; Binding b = new Binding(); b.setVariable("beans", beans); GroovyShell shell = classLoader != null ? new GroovyShell(classLoader,b) : new GroovyShell(b); for (Resource resource : resources) { shell.evaluate(new InputStreamReader(resource.getInputStream())); } }
private void lazyInit() { if (initialized) return; initialized = true; HttpServletResponse response = (HttpServletResponse) super.getVariable("response"); ServletOutput output = new ServletOutput(response); super.setVariable("out", output.getWriter()); super.setVariable("sout", output.getOutputStream()); MarkupBuilder builder = new MarkupBuilder(output.getWriter()); builder.setExpandEmptyElements(true); super.setVariable("html", builder); try { Class jsonBuilderClass = this.getClass().getClassLoader().loadClass("groovy.json.StreamingJsonBuilder"); Constructor writerConstructor = jsonBuilderClass.getConstructor(Writer.class); super.setVariable("json", writerConstructor.newInstance(output.getWriter())); } catch (Throwable t) { t.printStackTrace(); } // bind forward method MethodClosure c = new MethodClosure(this, "forward"); super.setVariable("forward", c); // bind include method c = new MethodClosure(this, "include"); super.setVariable("include", c); // bind redirect method c = new MethodClosure(this, "redirect"); super.setVariable("redirect", c); }
/** * Sets the binding (the variables available in the scope of the BeanBuilder) * @param b The Binding instance */ public void setBinding(Binding b) { this.binding = b.getVariables(); }
protected int run() throws Exception { // this allows the caller to manipulate the JVM state, so require the execute script privilege. Jenkins.getActiveInstance().checkPermission(Jenkins.RUN_SCRIPTS); Binding binding = new Binding(); binding.setProperty("out",new PrintWriter(stdout,true)); binding.setProperty("stdin",stdin); binding.setProperty("stdout",stdout); binding.setProperty("stderr",stderr); binding.setProperty("channel",channel); if (channel != null) { String j = getClientEnvironmentVariable("JOB_NAME"); if (j != null) { Item job = Jenkins.getActiveInstance().getItemByFullName(j); binding.setProperty("currentJob", job); String b = getClientEnvironmentVariable("BUILD_NUMBER"); if (b != null && job instanceof AbstractProject) { Run r = ((AbstractProject) job).getBuildByNumber(Integer.parseInt(b)); binding.setProperty("currentBuild", r); } } } GroovyShell groovy = new GroovyShell(Jenkins.getActiveInstance().getPluginManager().uberClassLoader, binding); groovy.run(loadScript(),"RemoteClass",remaining.toArray(new String[remaining.size()])); return 0; }
/** * Filter to run for the LegacySecurityRealm is the * ChainServletFilter legacy from /WEB-INF/security/SecurityFilters.groovy. */ @Override public Filter createFilter(FilterConfig filterConfig) { Binding binding = new Binding(); SecurityComponents sc = this.createSecurityComponents(); binding.setVariable("securityComponents", sc); binding.setVariable("securityRealm",this); BeanBuilder builder = new BeanBuilder(); builder.parse(filterConfig.getServletContext().getResourceAsStream("/WEB-INF/security/SecurityFilters.groovy"),binding); WebApplicationContext context = builder.createApplicationContext(); return (Filter) context.getBean("legacy"); }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) { GroovyShell groovyShell = new GroovyShell( this.classLoader, new Binding(arguments), this.compilerConfiguration); try { String filename = (script instanceof ResourceScriptSource ? ((ResourceScriptSource) script).getResource().getFilename() : null); if (filename != null) { return groovyShell.evaluate(script.getScriptAsString(), filename); } else { return groovyShell.evaluate(script.getScriptAsString()); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access Groovy script", ex); } catch (GroovyRuntimeException ex) { throw new ScriptCompilationException(script, ex); } }
@Override public SecurityComponents createSecurityComponents() { Binding binding = new Binding(); binding.setVariable("authenticator", new Authenticator()); BeanBuilder builder = new BeanBuilder(); builder.parse(Jenkins.getInstance().servletContext.getResourceAsStream("/WEB-INF/security/AbstractPasswordBasedSecurityRealm.groovy"),binding); WebApplicationContext context = builder.createApplicationContext(); return new SecurityComponents( findBean(AuthenticationManager.class, context), new ImpersonatingUserDetailsService(this)); }
public String call() throws RuntimeException { // if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader. if (cl==null) cl = Thread.currentThread().getContextClassLoader(); CompilerConfiguration cc = new CompilerConfiguration(); cc.addCompilationCustomizers(new ImportCustomizer().addStarImports( "jenkins", "jenkins.model", "hudson", "hudson.model")); GroovyShell shell = new GroovyShell(cl,new Binding(),cc); StringWriter out = new StringWriter(); PrintWriter pw = new PrintWriter(out); shell.setVariable("out", pw); try { Object output = shell.evaluate(script); if(output!=null) pw.println("Result: "+output); } catch (Throwable t) { Functions.printStackTrace(t, pw); } return out.toString(); } }
binding.setVariable("beans", beans); GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding); shell.evaluate(encodedResource.getReader(), "beans");
/** * 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 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 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(); } }
private GroovyShell createShell() { CompilerConfiguration compilerSettings = new CompilerConfiguration(); compilerSettings.setScriptBaseClass(DelegatingScript.class.getName()); return new GroovyShell(getClass().getClassLoader(), new Binding(), compilerSettings); } }
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)); } }
public void testGroovyScriptEngineVsGroovyShell() throws IOException, ResourceException, ScriptException { // @todo refactor this path File currentDir = new File("./src/test/groovy/bugs"); String file = "bug1567_script.groovy"; Binding binding = new Binding(); GroovyShell shell = new GroovyShell(binding); String[] test = null; Object result = shell.run(new File(currentDir, file), test); String[] roots = new String[]{currentDir.getAbsolutePath()}; GroovyScriptEngine gse = new GroovyScriptEngine(roots); binding = new Binding(); // a MME was ensued here stating no 't.start()' was available // in the script gse.run(file, binding); } }
/** * 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; }
/** * A helper constructor used in main(String[]) method calls * * @param args are the command line arguments from a main() */ public Binding(String[] args) { this(); setVariable("args", args); }
@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()); }
Binding binding = new Binding(consumer.getSession()); binding.setProperty("args", args);
/** * Parses the bean definition groovy script. */ public void parse(InputStream script) { parse(script,new Binding()); }