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