/** * Parses the given text into a Java class capable of being run * * @param text the text of the script/class to parse * @param fileName the file name to use as the name of the class * @return the main class defined in the given script */ public Class parseClass(final String text, final String fileName) throws CompilationFailedException { GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>() { public GroovyCodeSource run() { return new GroovyCodeSource(text, fileName, "/groovy/script"); } }); gcs.setCachable(false); return parseClass(gcs); }
@SuppressWarnings("unchecked") public MarkupTemplateMaker(final URL resource, Map<String, String> modelTypes) throws IOException { boolean cache = templateConfiguration.isCacheTemplates(); GroovyCodeSource codeSource; if (cache) { // we use a map in addition to the internal caching mechanism of Groovy because the latter // will always read from the URL even if it's cached String key = resource.toExternalForm(); codeSource = codeSourceCache.get(key); if (codeSource == null) { codeSource = new GroovyCodeSource(resource); codeSourceCache.put(key, codeSource); } } else { codeSource = new GroovyCodeSource(resource); } codeSource.setCachable(cache); templateClass = groovyClassLoader.parseClass(codeSource, modelTypes); this.modeltypes = modelTypes; }
/** * Parses the given text into a Java class capable of being run * * @param text the text of the script/class to parse * @param fileName the file name to use as the name of the class * @return the main class defined in the given script */ public Class parseClass(final String text, final String fileName) throws CompilationFailedException { GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>() { public GroovyCodeSource run() { return new GroovyCodeSource(text, fileName, "/groovy/script"); } }); gcs.setCachable(false); return parseClass(gcs); }
public GroovierJellyScript parseGSP(URL res) throws IOException, ClassNotFoundException { GroovyCodeSource gcs = new GroovyCodeSource(parser.parse(res), res.toExternalForm(), res.toExternalForm()); gcs.setCachable(false); return new GroovierJellyScript(gcl.parseClass(gcs),res); } }
public GroovierJellyScript parse(URL script) throws IOException { // we do the caching on our own, so don't let GroovyClassLoader cache this. Or else // dynamic reloading won't work GroovyCodeSource gcs = new GroovyCodeSource(script); gcs.setCachable(false); return new GroovierJellyScript(gcl.parseClass(gcs),script); }
@SuppressWarnings("unchecked") public MarkupTemplateMaker(final URL resource, Map<String, String> modelTypes) throws IOException { boolean cache = templateConfiguration.isCacheTemplates(); GroovyCodeSource codeSource; if (cache) { // we use a map in addition to the internal caching mechanism of Groovy because the latter // will always read from the URL even if it's cached String key = resource.toExternalForm(); codeSource = codeSourceCache.get(key); if (codeSource == null) { codeSource = new GroovyCodeSource(resource); codeSourceCache.put(key, codeSource); } } else { codeSource = new GroovyCodeSource(resource); } codeSource.setCachable(cache); templateClass = groovyClassLoader.parseClass(codeSource, modelTypes); this.modeltypes = modelTypes; }
@Override public Object getValue(CpsScript script) throws Exception { Binding binding = script.getBinding(); CpsThread c = CpsThread.current(); if (c == null) throw new IllegalStateException("Expected to be called from CpsThread"); ClassLoader cl = getClass().getClassLoader(); String scriptPath = "dsl/" + getFunctionName() + ".groovy"; Reader r = new InputStreamReader(cl.getResourceAsStream(scriptPath), "UTF-8"); GroovyCodeSource gsc = new GroovyCodeSource(r, getFunctionName() + ".groovy", cl.getResource(scriptPath).getFile()); gsc.setCachable(true); Object pipelineDSL = c.getExecution() .getShell() .getClassLoader() .parseClass(gsc) .newInstance(); binding.setVariable(getName(), pipelineDSL); r.close(); return pipelineDSL; }
public Class<?> run() { try { GroovyCodeSource codeSource = new GroovyCodeSource(scriptSource, className, BootstrapInfo.UNTRUSTED_CODEBASE); codeSource.setCachable(false); CompilerConfiguration configuration = new CompilerConfiguration() .addCompilationCustomizers(new ImportCustomizer().addStarImports("org.joda.time").addStaticStars("java.lang.Math")) .addCompilationCustomizers(new GroovyBigDecimalTransformer(CompilePhase.CONVERSION)); // always enable invokeDynamic, not the crazy softreference-based stuff configuration.getOptimizationOptions().put(GROOVY_INDY_SETTING_NAME, true); GroovyClassLoader groovyClassLoader = new GroovyClassLoader(loader, configuration); return groovyClassLoader.parseClass(codeSource); } catch (Throwable e) { if (logger.isTraceEnabled()) { logger.trace("Exception compiling Groovy script:", e); } throw new ScriptException("Error compiling script " + className, e); } } });