Search in sources :

Example 1 with GroovyScriptEngineImpl

use of org.codehaus.groovy.jsr223.GroovyScriptEngineImpl 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 2 with GroovyScriptEngineImpl

use of org.codehaus.groovy.jsr223.GroovyScriptEngineImpl in project walkmod-core by walkmod.

the class ScriptProcessor method initialize.

public void initialize(VisitorContext context, Object node) {
    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()));
        }
    }
    if (queryEngine == null) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("language", "groovy");
        List<String> includes = new LinkedList<String>();
        includes.add("query.alias.groovy");
        parameters.put("includes", includes);
        Object bean = context.getBean("org.walkmod.query.ScriptingQueryEngine", parameters);
        if (bean != null) {
            if (bean instanceof QueryEngine) {
                queryEngine = (QueryEngine) bean;
            }
        } else {
            throw new WalkModException("Query Engine not found");
        }
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("node", node);
    queryEngine.initialize(context, params);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyScriptEngineImpl(org.codehaus.groovy.jsr223.GroovyScriptEngineImpl) WalkModException(org.walkmod.exceptions.WalkModException) HashMap(java.util.HashMap) ScriptEngineManager(javax.script.ScriptEngineManager) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) QueryEngine(org.walkmod.query.QueryEngine) LinkedList(java.util.LinkedList)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)2 ScriptEngineManager (javax.script.ScriptEngineManager)2 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)2 GroovyScriptEngineImpl (org.codehaus.groovy.jsr223.GroovyScriptEngineImpl)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 WalkModException (org.walkmod.exceptions.WalkModException)1 QueryEngine (org.walkmod.query.QueryEngine)1