Search in sources :

Example 26 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project walkmod-core by walkmod.

the class ScriptingQueryEngine method initialize.

@Override
public void initialize(VisitorContext context, Map<String, Object> parameters) {
    if (engine == null) {
        ScriptEngineManager factory = new ScriptEngineManager(context.getClassLoader());
        engine = factory.getEngineByName(language);
        if (engine instanceof GroovyScriptEngineImpl) {
            ((GroovyScriptEngineImpl) engine).setClassLoader(new GroovyClassLoader(context.getClassLoader(), new CompilerConfiguration()));
        }
    }
    this.context = context;
    bindings = engine.createBindings();
    Set<String> keys = parameters.keySet();
    if (keys != null) {
        for (String key : keys) {
            Object value = parameters.get(key);
            if (key.equals("node")) {
                rootNode = value;
            }
            bindings.put(key, value);
        }
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyScriptEngineImpl(org.codehaus.groovy.jsr223.GroovyScriptEngineImpl) ScriptEngineManager(javax.script.ScriptEngineManager) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 27 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project beakerx by twosigma.

the class GroovyClassLoaderFactory method newEvaluator.

public static GroovyClassLoader newEvaluator(Imports imports, Classpath classpath, String outDir, ImportCustomizer icz, ClassLoader parent) {
    try {
        Class.forName("org.codehaus.groovy.control.customizers.ImportCustomizer");
    } catch (ClassNotFoundException e1) {
        String gjp = System.getenv(GROOVY_JAR_PATH);
        String errorMsg = null;
        if (gjp != null && !gjp.isEmpty()) {
            errorMsg = "Groovy libary not found, GROOVY_JAR_PATH = " + gjp;
        } else {
            errorMsg = "Default groovy libary not found. No GROOVY_JAR_PATH variable set.";
        }
        throw new GroovyNotFoundException(errorMsg);
    }
    icz = addImportsCustomizer(icz, imports);
    CompilerConfiguration config = new CompilerConfiguration().addCompilationCustomizers(icz);
    String acloader_cp = Joiner.on(File.pathSeparatorChar).join(classpath.getPathsAsStrings());
    config.setClasspath(acloader_cp);
    return new GroovyClassLoader(parent, config);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 28 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project qi4j-sdk by Qi4j.

the class GroovyMixin method invokeAsObject.

private Object invokeAsObject(Method method, Object[] args, URL groovySource) throws Throwable {
    try {
        Class declaringClass = method.getDeclaringClass();
        GroovyObject groovyObject = groovyObjects.get(declaringClass);
        if (groovyObject == null) {
            InputStream is = null;
            final Class groovyClass;
            try {
                is = groovySource.openStream();
                StringBuilder sourceBuilder = new StringBuilder();
                Inputs.text(groovySource).transferTo(Outputs.text(sourceBuilder));
                GroovyClassLoader groovyClassLoader = new GroovyClassLoader(declaringClass.getClassLoader());
                groovyClass = groovyClassLoader.parseClass(sourceBuilder.toString());
            } finally {
                if (is != null) {
                    is.close();
                }
            }
            groovyObject = (GroovyObject) groovyClass.newInstance();
            if (hasProperty(groovyObject, "This")) {
                groovyObject.setProperty("This", me);
            }
            groovyObjects.put(declaringClass, groovyObject);
        }
        return groovyObject.invokeMethod(method.getName(), args);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) InputStream(java.io.InputStream) MissingPropertyException(groovy.lang.MissingPropertyException) GroovyObject(groovy.lang.GroovyObject)

Example 29 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project fess by codelibs.

the class DocBoostMatcherTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    ScriptEngineFactory scriptEngineFactory = new ScriptEngineFactory();
    ComponentUtil.register(scriptEngineFactory, "scriptEngineFactory");
    new AbstractScriptEngine() {

        @Override
        public Object evaluate(String template, 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 JobProcessingException e) {
                throw e;
            } catch (final Exception e) {
                return null;
            } finally {
                final GroovyClassLoader loader = groovyShell.getClassLoader();
                loader.clearCache();
            }
        }

        @Override
        protected String getName() {
            return Constants.DEFAULT_SCRIPT;
        }
    }.register();
}
Also used : Binding(groovy.lang.Binding) JobProcessingException(org.codelibs.fess.exception.JobProcessingException) GroovyShell(groovy.lang.GroovyShell) JobProcessingException(org.codelibs.fess.exception.JobProcessingException) GroovyClassLoader(groovy.lang.GroovyClassLoader) ScriptEngineFactory(org.codelibs.fess.script.ScriptEngineFactory) Map(java.util.Map) HashMap(java.util.HashMap) AbstractScriptEngine(org.codelibs.fess.script.AbstractScriptEngine)

Example 30 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project groovy-core by groovy.

the class JavacJavaCompiler method makeParameters.

private String[] makeParameters(List<String> files, GroovyClassLoader parentClassLoader) {
    Map options = config.getJointCompilationOptions();
    LinkedList<String> paras = new LinkedList<String>();
    File target = config.getTargetDirectory();
    if (target == null)
        target = new File(".");
    // defaults
    paras.add("-d");
    paras.add(target.getAbsolutePath());
    paras.add("-sourcepath");
    paras.add(((File) options.get("stubDir")).getAbsolutePath());
    // add flags
    String[] flags = (String[]) options.get("flags");
    if (flags != null) {
        for (String flag : flags) {
            paras.add('-' + flag);
        }
    }
    boolean hadClasspath = false;
    // add namedValues
    String[] namedValues = (String[]) options.get("namedValues");
    if (namedValues != null) {
        for (int i = 0; i < namedValues.length; i += 2) {
            String name = namedValues[i];
            if (name.equals("classpath"))
                hadClasspath = true;
            paras.add('-' + name);
            paras.add(namedValues[i + 1]);
        }
    }
    // append classpath if not already defined
    if (!hadClasspath) {
        // add all classpaths that compilation unit sees
        StringBuilder resultPath = new StringBuilder(DefaultGroovyMethods.join((Iterable) config.getClasspath(), File.pathSeparator));
        ClassLoader cl = parentClassLoader;
        while (cl != null) {
            if (cl instanceof URLClassLoader) {
                for (URL u : ((URLClassLoader) cl).getURLs()) {
                    try {
                        resultPath.append(File.pathSeparator);
                        resultPath.append(new File(u.toURI()).getPath());
                    } catch (URISyntaxException e) {
                    // ignore it
                    }
                }
            }
            cl = cl.getParent();
        }
        paras.add("-classpath");
        paras.add(resultPath.toString());
    }
    // files to compile
    paras.addAll(files);
    return paras.toArray(new String[paras.size()]);
}
Also used : URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) File(java.io.File)

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