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())); } }
/** * Parses the bean definition groovy script by first exporting the given {@link Binding}. */ public void parse(InputStream script, Binding binding) { if (script==null) throw new IllegalArgumentException("No script is provided"); setBinding(binding); CompilerConfiguration cc = new CompilerConfiguration(); cc.setScriptBaseClass(ClosureScript.class.getName()); GroovyShell shell = new GroovyShell(classLoader,binding,cc); ClosureScript s = (ClosureScript)shell.parse(new InputStreamReader(script)); s.setDelegate(this); s.run(); }
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(); } }
/** * Parses the given script and returns it ready to be run * * @param scriptText the text of the script */ public Script parse(String scriptText) throws CompilationFailedException { return parse(scriptText, generateScriptName()); }
/** * A helper method to allow the dynamic evaluation of groovy expressions using this * scripts binding as the variable scope * * @param expression is the Groovy script expression to evaluate */ public Object evaluate(String expression) throws CompilationFailedException { GroovyShell shell = new GroovyShell(getClass().getClassLoader(), binding); return shell.evaluate(expression); }
private GroovyShell createShell() { CompilerConfiguration compilerSettings = new CompilerConfiguration(); compilerSettings.setScriptBaseClass(DelegatingScript.class.getName()); return new GroovyShell(getClass().getClassLoader(), new Binding(), compilerSettings); } }
public static void processConfigScripts(List<String> scripts, CompilerConfiguration conf) throws IOException { if (scripts.isEmpty()) return; Binding binding = new Binding(); binding.setVariable("configuration", conf); CompilerConfiguration configuratorConfig = new CompilerConfiguration(); ImportCustomizer customizer = new ImportCustomizer(); customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder"); configuratorConfig.addCompilationCustomizers(customizer); GroovyShell shell = new GroovyShell(binding, configuratorConfig); for (String script : scripts) { shell.evaluate(new File(script)); } }
@Override public void setup() { CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass("org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.TypeCheckingDSL"); ImportCustomizer ic = new ImportCustomizer(); ic.addStarImports("org.codehaus.groovy.ast.expr"); ic.addStaticStars("org.codehaus.groovy.ast.ClassHelper"); ic.addStaticStars("org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport"); config.addCompilationCustomizers(ic); final GroovyClassLoader transformLoader = compilationUnit!=null?compilationUnit.getTransformLoader():typeCheckingVisitor.getSourceUnit().getClassLoader(); Class<?> clazz = transformLoader.loadClass(scriptPath, false, true); if (TypeCheckingDSL.class.isAssignableFrom(clazz)) { script = (TypeCheckingDSL) clazz.newInstance(); GroovyShell shell = new GroovyShell(transformLoader, new Binding(), config); script = (TypeCheckingDSL) shell.parse( new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding()) );
private void evalFiles(String[] filepaths, CommandLine cli) throws CompilationFailedException, IOException { Binding binding = setupBinding(new Binding(), cli.hasOption(MUTATIONS_ENABLED_FLAG_SHORT)); GroovyShell shell = new GroovyShell(binding); if(cli.hasOption(CLASSPATH_FLAG_SHORT)) { shell.getClassLoader().addClasspath(cli.getOptionValue(CLASSPATH_FLAG_SHORT)); } for(String filepath : filepaths) { File file = new File(filepath); shell.evaluate(file); } }
@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); } }
binding.setVariable("beans", beans); GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding); shell.evaluate(encodedResource.getReader(), "beans");
@Override protected void setUp() throws Exception { CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(GroovyScriptCommand.class.getName()); // loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config); shell = new GroovyShell(loader); }
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; }
CompilerConfiguration conf = new CompilerConfiguration(); conf.setDebug(true); shell = new GroovyShell(conf); if (addClasspath != null && addClasspath.length() > 0) { for (File fcp : Files.listPathsFiles(addClasspath)) { throw new ProcessException("Path not found `" + fcp + "` for `" + ADD_CLASSPATH.getDisplayName() + "`"); shell.getClassLoader().addClasspath(fcp.toString()); shell.getClassLoader().addClasspath(groovyClasspath); scriptText = scriptBody; script = shell.parse(PRELOADS + scriptText, scriptName); compiled = (Class<Script>) script.getClass(); script = compiled.newInstance(); Thread.currentThread().setContextClassLoader(shell.getClassLoader()); return script;
private void compile(String code) { this.compiler.setScriptBaseClass(getBaseClass()); this.shell = new GroovyShell(this.getClass() .getClassLoader(), new Binding(), compiler); this.script = shell.parse(code); }
private void configureCompiler() { if (scriptBaseClass!=null) { configuration.setScriptBaseClass(scriptBaseClass); } if (indy) { configuration.getOptimizationOptions().put("indy", Boolean.TRUE); configuration.getOptimizationOptions().put("int", Boolean.FALSE); } if (configscript!=null) { Binding binding = new Binding(); binding.setVariable("configuration", configuration); CompilerConfiguration configuratorConfig = new CompilerConfiguration(); ImportCustomizer customizer = new ImportCustomizer(); customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder"); configuratorConfig.addCompilationCustomizers(customizer); GroovyShell shell = new GroovyShell(binding, configuratorConfig); File confSrc = new File(configscript); try { shell.evaluate(confSrc); } catch (IOException e) { throw new BuildException("Unable to configure compiler using configuration file: "+confSrc, e); } } }
thread.setContextClassLoader(GroovyShell.class.getClassLoader()); @Override public GroovyClassLoader run() { return new GroovyClassLoader(baseClassLoader); final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration); try { parseAndRunScript(groovy, txt, mavenPom, scriptName, null, new AntBuilder(this)); } finally { groovy.resetLoadedClasses(); groovy.getClassLoader().clearCache(); if (contextClassLoader || maven) thread.setContextClassLoader(savedLoader);
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); } }
new GroovyShell().parse( new File( "test.groovy" ) ).invokeMethod( "hello_world", null ) ; Class scriptClass = new GroovyClassLoader().parseClass( new File( "test.groovy" ) ) ; Object scriptInstance = scriptClass.newInstance() ; scriptClass.getDeclaredMethod( "hello_world", new Class[] {} ).invoke( scriptInstance, new Object[] {} ) ;
/** * Can be used to customize the environment in which the script runs. */ protected GroovyShell createShell() { return new GroovyShell(loader, bindings); }