Search in sources :

Example 36 with ScriptContext

use of javax.script.ScriptContext in project jbpm by kiegroup.

the class JavaScriptingDataTransformer method evaluateExpression.

protected Object evaluateExpression(Object expression, Map<String, Object> parameters) {
    try {
        Object result = null;
        StringWriter writer = new StringWriter();
        ScriptContext context = new SimpleScriptContext();
        for (Map.Entry<String, Object> property : engineProperties.entrySet()) {
            context.setAttribute(property.getKey(), property.getValue(), ScriptContext.ENGINE_SCOPE);
        }
        Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
        bindings.putAll(parameters);
        context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        context.setWriter(writer);
        if (expression instanceof CompiledScript) {
            logger.debug("About to evaluate compiled expression {} with bindings {} on engine", expression, parameters, scriptEngine);
            result = ((CompiledScript) expression).eval(context);
        } else {
            logger.debug("About to evaluate expression {} with bindings {} on engine", expression, parameters, scriptEngine);
            result = scriptEngine.eval(expression.toString(), context);
        }
        if (result == null) {
            result = writer.toString();
        }
        return result;
    } catch (ScriptException e) {
        throw new RuntimeException("Error when evaluating script", e);
    }
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) StringWriter(java.io.StringWriter) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) HashMap(java.util.HashMap) Map(java.util.Map) Bindings(javax.script.Bindings)

Example 37 with ScriptContext

use of javax.script.ScriptContext 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)

Example 38 with ScriptContext

use of javax.script.ScriptContext in project revapi by revapi.

the class ConfigurationValidator method getJsEngine.

private ScriptEngine getJsEngine(Writer output) throws IOException, ScriptException {
    ScriptEngine ret = null;
    if (jsEngine != null) {
        ret = jsEngine.get();
    }
    if (ret == null) {
        ret = new ScriptEngineManager().getEngineByName("javascript");
        ScriptContext ctx = new SimpleScriptContext();
        Bindings globalScope = ret.createBindings();
        ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
        initTv4(ret, globalScope);
        ret.setContext(ctx);
        jsEngine = new WeakReference<>(ret);
    }
    ret.getContext().setWriter(output);
    ret.getContext().setErrorWriter(output);
    return ret;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptEngineManager(javax.script.ScriptEngineManager) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 39 with ScriptContext

use of javax.script.ScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test7.

private static void test7() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("var foo = 23;");
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context1 = new SimpleScriptContext();
    context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context2 = new SimpleScriptContext();
    context2.getBindings(ScriptContext.ENGINE_SCOPE).put("foo", defaultBindings.get("foo"));
    engine.eval("foo = 44;", context1);
    engine.eval("print(foo);", context1);
    engine.eval("print(foo);", context2);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Example 40 with ScriptContext

use of javax.script.ScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test8.

private static void test8() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("var obj = { foo: 23 };");
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context1 = new SimpleScriptContext();
    context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context2 = new SimpleScriptContext();
    context2.getBindings(ScriptContext.ENGINE_SCOPE).put("obj", defaultBindings.get("obj"));
    engine.eval("obj.foo = 44;", context1);
    engine.eval("print(obj.foo);", context1);
    engine.eval("print(obj.foo);", context2);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Aggregations

ScriptContext (javax.script.ScriptContext)124 SimpleScriptContext (javax.script.SimpleScriptContext)81 Bindings (javax.script.Bindings)33 Test (org.junit.Test)30 SimpleBindings (javax.script.SimpleBindings)28 Test (org.junit.jupiter.api.Test)19 ScriptException (javax.script.ScriptException)17 ScriptEngine (javax.script.ScriptEngine)16 HashMap (java.util.HashMap)13 CompiledScript (javax.script.CompiledScript)11 IOException (java.io.IOException)8 Map (java.util.Map)8 ScriptEngineManager (javax.script.ScriptEngineManager)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 Test (org.testng.annotations.Test)7 StringWriter (java.io.StringWriter)6 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)6 XWikiException (com.xpn.xwiki.XWikiException)5 Reader (java.io.Reader)5 StringReader (java.io.StringReader)5