Search in sources :

Example 1 with RenjinScriptEngine

use of org.renjin.script.RenjinScriptEngine in project structr by structr.

the class Scripting method evaluateScript.

// ----- private methods -----
private static Object evaluateScript(final ActionContext actionContext, final GraphObject entity, final String engineName, final String script) throws FrameworkException {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName(engineName);
    if (engine == null) {
        throw new RuntimeException(engineName + " script engine could not be initialized. Check class path.");
    }
    final ScriptContext scriptContext = engine.getContext();
    final Bindings bindings = new StructrScriptBindings(actionContext, entity);
    if (!(engine instanceof RenjinScriptEngine)) {
        scriptContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
    }
    StringWriter output = new StringWriter();
    scriptContext.setWriter(output);
    try {
        engine.eval(script);
        Object extractedValue = output.toString();
        return extractedValue;
    } catch (final ScriptException e) {
        logger.error("Error while processing {} script: {}", engineName, script, e);
    }
    return null;
}
Also used : ScriptException(javax.script.ScriptException) StringWriter(java.io.StringWriter) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptContext(javax.script.ScriptContext) GraphObject(org.structr.core.GraphObject) RenjinScriptEngine(org.renjin.script.RenjinScriptEngine) Bindings(javax.script.Bindings) RenjinScriptEngine(org.renjin.script.RenjinScriptEngine) ScriptEngine(javax.script.ScriptEngine)

Aggregations

StringWriter (java.io.StringWriter)1 Bindings (javax.script.Bindings)1 ScriptContext (javax.script.ScriptContext)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 ScriptException (javax.script.ScriptException)1 RenjinScriptEngine (org.renjin.script.RenjinScriptEngine)1 GraphObject (org.structr.core.GraphObject)1