Search in sources :

Example 16 with GroovyClassLoader

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);
}
Also used : Charset(java.nio.charset.Charset) HtmlToJavaClassCompiler(org.webpieces.templatingdev.impl.HtmlToJavaClassCompiler) FileCollection(org.gradle.api.file.FileCollection) LogLevel(org.gradle.api.logging.LogLevel) BufferedWriter(java.io.BufferedWriter) GroovyClassLoader(groovy.lang.GroovyClassLoader) StubModule(org.webpieces.templatingdev.api.StubModule) DevTemplateModule(org.webpieces.templatingdev.api.DevTemplateModule) Injector(com.google.inject.Injector) FileOutputStream(java.io.FileOutputStream) TemplateCompileConfig(org.webpieces.templatingdev.api.TemplateCompileConfig) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 17 with GroovyClassLoader

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();
    }
}
Also used : Binding(groovy.lang.Binding) GroovyClassLoader(groovy.lang.GroovyClassLoader) HashMap(java.util.HashMap) GroovyShell(groovy.lang.GroovyShell)

Example 18 with GroovyClassLoader

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();
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 19 with GroovyClassLoader

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);
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) InputStream(java.io.InputStream) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException)

Example 20 with GroovyClassLoader

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();
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) StringWriter(java.io.StringWriter) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyObject(groovy.lang.GroovyObject) GroovyObject(groovy.lang.GroovyObject)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)136 File (java.io.File)28 GroovyObject (groovy.lang.GroovyObject)19 Test (org.junit.jupiter.api.Test)18 IOException (java.io.IOException)15 HashMap (java.util.HashMap)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Map (java.util.Map)12 Binding (groovy.lang.Binding)10 URL (java.net.URL)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)10 BuildException (org.apache.tools.ant.BuildException)9 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)9 ClassNode (org.codehaus.groovy.ast.ClassNode)9 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)9 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)8 GrailsApplication (grails.core.GrailsApplication)8 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)8 ArtefactHandler (grails.core.ArtefactHandler)7 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)7