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;
}
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;
}
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));
}
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;
}
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<>();
}
Aggregations