use of groovy.lang.GroovyClassLoader in project webpieces by deanhiller.
the class TemplateCompilerTask method compileImpl.
public void compileImpl(TemplateCompileOptions options) throws IOException {
File buildDir = getProject().getBuildDir();
//need to make customizable...
File groovySrcGen = new File(buildDir, "groovysrc");
System.out.println("groovy src directory=" + groovySrcGen);
Charset encoding = Charset.forName(options.getEncoding());
TemplateCompileConfig config = new TemplateCompileConfig(false);
config.setFileEncoding(encoding);
config.setPluginClient(true);
config.setGroovySrcWriteDirectory(groovySrcGen);
System.out.println("custom tags=" + options.getCustomTags());
config.setCustomTagsFromPlugin(options.getCustomTags());
LogLevel logLevel = getProject().getGradle().getStartParameter().getLogLevel();
File destinationDir = getDestinationDir();
System.out.println("destDir=" + destinationDir);
File routeIdFile = new File(destinationDir, ProdTemplateModule.ROUTE_META_FILE);
if (routeIdFile.exists())
routeIdFile.delete();
routeIdFile.createNewFile();
System.out.println("routeId.txt file=" + routeIdFile.getAbsolutePath());
FileCollection srcCollection = getSource();
Set<File> files = srcCollection.getFiles();
File firstFile = files.iterator().next();
File baseDir = findBase(firstFile);
try (FileOutputStream routeOut = new FileOutputStream(routeIdFile);
OutputStreamWriter write = new OutputStreamWriter(routeOut, encoding.name());
BufferedWriter bufWrite = new BufferedWriter(write)) {
Injector injector = Guice.createInjector(new StubModule(), new DevTemplateModule(config, new PluginCompileCallback(destinationDir, bufWrite)));
HtmlToJavaClassCompiler compiler = injector.getInstance(HtmlToJavaClassCompiler.class);
GroovyClassLoader cl = new GroovyClassLoader();
for (File f : files) {
System.out.println("file=" + f);
String fullName = findFullName(baseDir, f);
System.out.println("name=" + fullName);
String source = readSource(f);
compiler.compile(cl, fullName, source);
}
}
setDidWork(true);
}
use of groovy.lang.GroovyClassLoader in project fess by codelibs.
the class GroovyUtil method evaluate.
public static Object evaluate(final String template, final Map<String, Object> paramMap) {
final Map<String, Object> bindingMap = new HashMap<>(paramMap);
bindingMap.put("container", SingletonLaContainerFactory.getContainer());
final GroovyShell groovyShell = new GroovyShell(new Binding(bindingMap));
try {
return groovyShell.evaluate(template);
} catch (final Exception e) {
logger.warn("Failed to evalue groovy script: " + template + " => " + paramMap, e);
return null;
} finally {
final GroovyClassLoader loader = groovyShell.getClassLoader();
// StreamUtil.of(loader.getLoadedClasses()).forEach(c -> {
// try {
// GroovySystem.getMetaClassRegistry().removeMetaClass(c);
// } catch (Throwable t) {
// logger.warn("Failed to delete " + c, t);
// }
// });
loader.clearCache();
}
}
use of groovy.lang.GroovyClassLoader in project workflow-cps-plugin by jenkinsci.
the class CpsFlowExecution method cleanUpLoader.
private static void cleanUpLoader(ClassLoader loader, Set<ClassLoader> encounteredLoaders, Set<Class<?>> encounteredClasses) throws Exception {
if (loader instanceof CpsGroovyShell.TimingLoader) {
cleanUpLoader(loader.getParent(), encounteredLoaders, encounteredClasses);
return;
}
if (!(loader instanceof GroovyClassLoader)) {
LOGGER.log(Level.FINER, "ignoring {0}", loader);
return;
}
if (!encounteredLoaders.add(loader)) {
return;
}
cleanUpLoader(loader.getParent(), encounteredLoaders, encounteredClasses);
LOGGER.log(Level.FINER, "found {0}", String.valueOf(loader));
SerializableClassRegistry.getInstance().release(loader);
cleanUpGlobalClassValue(loader);
GroovyClassLoader gcl = (GroovyClassLoader) loader;
for (Class<?> clazz : gcl.getLoadedClasses()) {
if (encounteredClasses.add(clazz)) {
LOGGER.log(Level.FINER, "found {0}", clazz.getName());
Introspector.flushFromCaches(clazz);
cleanUpGlobalClassSet(clazz);
cleanUpObjectStreamClassCaches(clazz);
cleanUpLoader(clazz.getClassLoader(), encounteredLoaders, encounteredClasses);
}
}
gcl.clearCache();
}
use of groovy.lang.GroovyClassLoader in project RecordManager2 by moravianlibrary.
the class ScriptRunnerImpl method run.
@Override
public void run(String scriptPath) {
InputStream script = null;
try {
script = provider.getResource(root + scriptPath);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
try (GroovyClassLoader loader = new GroovyClassLoader()) {
Class<?> groovyClass = loader.parseClass(ResourceUtils.asString(script));
Runnable runnable = (Runnable) groovyClass.newInstance();
init(runnable);
runnable.run();
} catch (CompilationFailedException | IOException | IllegalAccessException | InstantiationException ex) {
throw new RuntimeException(ex);
}
}
use of groovy.lang.GroovyClassLoader in project dkpro-tc by dkpro.
the class ExperimentStarter method start.
/**
* Method which executes Groovy script provided in the pathToScript.
*
* @param pathToScript
* path to Groovy script.
*
* @throws InstantiationException
* if class cannot be instantiated
*
* @throws IllegalAccessException
* if an illegal access occurred
*
* @throws IOException
* general IO Exceptions
*/
public static void start(String pathToScript) throws InstantiationException, IllegalAccessException, IOException {
ClassLoader parent = ExperimentStarter.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
StringWriter writer = new StringWriter();
IOUtils.copy(parent.getResourceAsStream(pathToScript), writer, "UTF-8");
Class<?> groovyClass = loader.parseClass(writer.toString());
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
Object[] a = {};
groovyObject.invokeMethod("run", a);
loader.close();
}
Aggregations