/** * Parses the bean definition groovy script. */ public void parse(InputStream script) { parse(script,new Binding()); }
public Writable make(Map map) { if (map == null) { throw new IllegalArgumentException("map must not be null"); } return new XmlWritable(script, new Binding(map)); } }
private GroovyShell createShell() { CompilerConfiguration compilerSettings = new CompilerConfiguration(); compilerSettings.setScriptBaseClass(DelegatingScript.class.getName()); return new GroovyShell(getClass().getClassLoader(), new Binding(), compilerSettings); } }
/** * 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())); } }
@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); } }
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(); } }
/** * 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); } }
/** * Creates {@link Filter} that all the incoming HTTP requests will go through * for authentication. * * <p> * The default implementation uses {@link #getSecurityComponents()} and builds * a standard filter chain from /WEB-INF/security/SecurityFilters.groovy. * But subclasses can override this to completely change the filter sequence. * * <p> * For other plugins that want to contribute {@link Filter}, see * {@link PluginServletFilter}. * * @since 1.271 */ public Filter createFilter(FilterConfig filterConfig) { LOGGER.entering(SecurityRealm.class.getName(), "createFilter"); Binding binding = new Binding(); SecurityComponents sc = getSecurityComponents(); 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("filter"); }
public Writable make(final Map map) { final Closure template = ((Closure) this.template.clone()).asWritable(); Binding binding = new Binding(map); template.setDelegate(binding); return (Writable) template; } }
@Override public void run() { try { Binding b = new Binding(); b.setVariable("number", count); result = (String) this.gse.run(script, b); } catch (Throwable t) { throw new RuntimeException("problem running script", t); } }
@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)); }
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 testClassLoader() { Binding context = new Binding(); CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(DerivedScript.class.getName()); GroovyShell shell = new GroovyShell(context, config); String script = "evaluate '''\n"+ "class XXXX{}\n"+ "assert evaluate('XXXX') == XXXX\n"+ "'''"; shell.evaluate(script); }
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(); }
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; }
/** * Test for GROOVY-6615 * @throws Exception */ public void testScriptWithCustomBodyMethod() throws Exception { Binding context = new Binding(); CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(BaseScriptCustomBodyMethod.class.getName()); GroovyShell shell = new GroovyShell(context, config); Object result = shell.evaluate("'I like ' + cheese"); assertEquals("I like Cheddar", result); }
public void testExecuteScriptWithContext() { Binding context = new Binding(); context.setVariable("test", new PropertyHolder()); GroovyShell shell = new GroovyShell(context); try { Object result = shell.evaluate(script2, "Test.groovy"); assertEquals(new Integer(2), result); } catch (Exception e) { fail(e.toString()); } }
private void reloadClassloader() { this.beakerxUrlClassLoader = newParentClassLoader(getClasspath()); this.icz = new ImportCustomizer(); this.groovyClassLoader = newEvaluator(getImports(), getClasspath(), getOutDir(), icz, beakerxUrlClassLoader); this.scriptBinding = new Binding(); }
public void testScriptWithDerivedBaseClass() throws Exception { Binding context = new Binding(); CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(DerivedScript.class.getName()); GroovyShell shell = new GroovyShell(context, config); Object result = shell.evaluate("x = 'abc'; doSomething(cheese)"); assertEquals("I like Cheddar", result); assertEquals("abc", context.getVariable("x")); }