Search in sources :

Example 66 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project zuul by Netflix.

the class GroovyCompiler method compile.

/**
 * Compiles Groovy code and returns the Class of the compiles code.
 */
public Class<?> compile(String sCode, String sName) {
    GroovyClassLoader loader = getGroovyClassLoader();
    LOG.warn("Compiling filter: " + sName);
    Class<?> groovyClass = loader.parseClass(sCode, sName);
    return groovyClass;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 67 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project atlas by alibaba.

the class ClosureFactory method buildClosure.

public static Closure<?> buildClosure(String... strings) throws IOException {
    Closure<?> closure = null;
    // Create a method returning a closure
    StringBuilder sb = new StringBuilder("def closure() { { script -> ");
    sb.append(StringUtils.join(strings, "\n"));
    sb.append(" } }");
    // Create an anonymous class for the method
    GroovyClassLoader loader = new GroovyClassLoader();
    Class<?> groovyClass = loader.parseClass(sb.toString());
    try {
        // Create an instance of the class
        GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
        // Invoke the object's method and thus obtain the closure
        closure = (Closure<?>) groovyObject.invokeMethod("closure", null);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    } finally {
        loader.close();
    }
    return closure;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyObject(groovy.lang.GroovyObject)

Example 68 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project fastjson by alibaba.

the class GroovyTest method test_groovy.

public void test_groovy() throws Exception {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    GroovyClassLoader loader = new GroovyClassLoader(parent);
    // A类
    Class AClass = loader.parseClass(// 
    "class A {\n" + // 
    "    int id\n" + "}");
    // A实例
    GroovyObject a = (GroovyObject) AClass.newInstance();
    a.setProperty("id", 33);
    String textA = JSON.toJSONString(a);
    GroovyObject aa = (GroovyObject) JSON.parseObject(textA, AClass);
    Assert.assertEquals(a.getProperty("id"), aa.getProperty("id"));
    System.out.println(a);
    // B类,继承于A
    Class BClass = loader.parseClass(// 
    "class B extends A {\n" + // 
    "    String name\n" + "}");
    // B实例
    GroovyObject b = (GroovyObject) BClass.newInstance();
    b.setProperty("name", "jobs");
    String textB = JSON.toJSONString(b);
    GroovyObject bb = (GroovyObject) JSON.parseObject(textB, BClass);
    Assert.assertEquals(b.getProperty("id"), bb.getProperty("id"));
    Assert.assertEquals(b.getProperty("name"), bb.getProperty("name"));
    // 序列化失败
    System.out.println(JSON.toJSONString(b, true));
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyObject(groovy.lang.GroovyObject)

Example 69 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project cas by apereo.

the class InternalGroovyScriptDao method getAttributesForUser.

@Override
public Map<String, Object> getAttributesForUser(final String uid) {
    final Map<String, Object> finalAttributes = new HashedMap();
    casProperties.getAuthn().getAttributeRepository().getGroovy().forEach(groovy -> {
        final ClassLoader parent = getClass().getClassLoader();
        try (GroovyClassLoader loader = new GroovyClassLoader(parent)) {
            if (groovy.getConfig().getLocation() != null) {
                final File groovyFile = groovy.getConfig().getLocation().getFile();
                if (groovyFile.exists()) {
                    final Class<?> groovyClass = loader.parseClass(groovyFile);
                    LOGGER.debug("Loaded groovy class [{}] from script [{}]", groovyClass.getSimpleName(), groovyFile.getCanonicalPath());
                    final GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
                    LOGGER.debug("Created groovy object instance from class [{}]", groovyFile.getCanonicalPath());
                    final Object[] args = { uid, LOGGER, casProperties, applicationContext };
                    LOGGER.debug("Executing groovy script's run method, with parameters [{}]", args);
                    final Map<String, Object> personAttributesMap = (Map<String, Object>) groovyObject.invokeMethod("run", args);
                    LOGGER.debug("Creating person attributes with the username [{}] and attributes [{}]", uid, personAttributesMap);
                    finalAttributes.putAll(personAttributesMap);
                }
            }
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    });
    return finalAttributes;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyObject(groovy.lang.GroovyObject) HashedMap(org.apache.commons.collections.map.HashedMap) File(java.io.File) HashMap(java.util.HashMap) HashedMap(org.apache.commons.collections.map.HashedMap) Map(java.util.Map) GroovyObject(groovy.lang.GroovyObject)

Example 70 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project cas by apereo.

the class GroovyScriptAttributeReleasePolicy method getAttributesInternal.

@Override
protected Map<String, Object> getAttributesInternal(final Map<String, Object> attributes, final RegisteredService service) {
    final ClassLoader parent = getClass().getClassLoader();
    try (GroovyClassLoader loader = new GroovyClassLoader(parent)) {
        final File groovyFile = ResourceUtils.getResourceFrom(this.groovyScript).getFile();
        if (groovyFile.exists()) {
            final Class<?> groovyClass = loader.parseClass(groovyFile);
            final GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
            LOGGER.debug("Created groovy object instance from class [{}]", groovyFile.getCanonicalPath());
            final Object[] args = { attributes, LOGGER };
            LOGGER.debug("Executing groovy script's run method, with parameters [{}]", args);
            final Map<String, Object> personAttributesMap = (Map<String, Object>) groovyObject.invokeMethod("run", args);
            LOGGER.debug("Final set of attributes determined by the script are [{}]", personAttributesMap);
            return personAttributesMap;
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    LOGGER.warn("Groovy script [{}] does not exist, or cannot be loaded", groovyScript);
    return new HashMap<>();
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) HashMap(java.util.HashMap) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyObject(groovy.lang.GroovyObject) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) GroovyObject(groovy.lang.GroovyObject)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)126 File (java.io.File)26 GroovyObject (groovy.lang.GroovyObject)17 Test (org.junit.jupiter.api.Test)17 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)13 IOException (java.io.IOException)11 Binding (groovy.lang.Binding)10 URL (java.net.URL)10 HashMap (java.util.HashMap)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)10 Map (java.util.Map)9 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)8 GrailsApplication (grails.core.GrailsApplication)8 BuildException (org.apache.tools.ant.BuildException)8 ArtefactHandler (grails.core.ArtefactHandler)7 GroovyShell (groovy.lang.GroovyShell)7 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)7 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)7 Script (groovy.lang.Script)6 Decorator (com.opensymphony.module.sitemesh.Decorator)5