Search in sources :

Example 11 with ScriptContext

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

the class Jdk9Application method getContext.

private ScriptContext getContext(Object handler) {
    Bindings bindings = new SimpleBindings();
    // NOI18N
    bindings.put("handler", handler);
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    return context;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Example 12 with ScriptContext

use of javax.script.ScriptContext in project Lucee by lucee.

the class ScriptEngineImpl method getContext.

private ScriptContext getContext(Bindings b) {
    ScriptContext def = getContext();
    SimpleScriptContext custom = new SimpleScriptContext();
    Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
    if (gs != null)
        custom.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
    custom.setBindings(b, ScriptContext.ENGINE_SCOPE);
    custom.setReader(def.getReader());
    custom.setWriter(def.getWriter());
    custom.setErrorWriter(def.getErrorWriter());
    return custom;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings)

Example 13 with ScriptContext

use of javax.script.ScriptContext in project accumulo by apache.

the class ScriptCommand method execute.

@Override
public int execute(String fullCommand, CommandLine cl, Shell shellState) throws Exception {
    boolean invoke = false;
    ScriptEngineManager mgr = new ScriptEngineManager();
    if (cl.hasOption(list.getOpt())) {
        listJSREngineInfo(mgr, shellState);
    } else if (cl.hasOption(file.getOpt()) || cl.hasOption(script.getOpt())) {
        String engineName = DEFAULT_ENGINE;
        if (cl.hasOption(engine.getOpt())) {
            engineName = cl.getOptionValue(engine.getOpt());
        }
        ScriptEngine engine = mgr.getEngineByName(engineName);
        if (null == engine) {
            shellState.printException(new Exception(engineName + " not found"));
            return 1;
        }
        if (cl.hasOption(object.getOpt()) || cl.hasOption(function.getOpt())) {
            if (!(engine instanceof Invocable)) {
                shellState.printException(new Exception(engineName + " does not support invoking functions or methods"));
                return 1;
            }
            invoke = true;
        }
        ScriptContext ctx = new SimpleScriptContext();
        // Put the following objects into the context so that they
        // are available to the scripts
        // TODO: What else should go in here?
        Bindings b = engine.getBindings(ScriptContext.ENGINE_SCOPE);
        b.put("connection", shellState.getConnector());
        List<Object> argValues = new ArrayList<>();
        if (cl.hasOption(args.getOpt())) {
            String[] argList = cl.getOptionValue(args.getOpt()).split(",");
            for (String arg : argList) {
                String[] parts = arg.split("=");
                if (parts.length == 0) {
                    continue;
                } else if (parts.length == 1) {
                    b.put(parts[0], null);
                    argValues.add(null);
                } else if (parts.length == 2) {
                    b.put(parts[0], parts[1]);
                    argValues.add(parts[1]);
                }
            }
        }
        ctx.setBindings(b, ScriptContext.ENGINE_SCOPE);
        Object[] argArray = argValues.toArray(new Object[argValues.size()]);
        Writer writer = null;
        if (cl.hasOption(out.getOpt())) {
            File f = new File(cl.getOptionValue(out.getOpt()));
            writer = new FileWriter(f);
            ctx.setWriter(writer);
        }
        if (cl.hasOption(file.getOpt())) {
            File f = new File(cl.getOptionValue(file.getOpt()));
            if (!f.exists()) {
                if (null != writer) {
                    writer.close();
                }
                shellState.printException(new Exception(f.getAbsolutePath() + " not found"));
                return 1;
            }
            Reader reader = new FileReader(f);
            try {
                engine.eval(reader, ctx);
                if (invoke) {
                    this.invokeFunctionOrMethod(shellState, engine, cl, argArray);
                }
            } catch (ScriptException ex) {
                shellState.printException(ex);
                return 1;
            } finally {
                reader.close();
                if (null != writer) {
                    writer.close();
                }
            }
        } else if (cl.hasOption(script.getOpt())) {
            String inlineScript = cl.getOptionValue(script.getOpt());
            try {
                if (engine instanceof Compilable) {
                    Compilable compiledEng = (Compilable) engine;
                    CompiledScript script = compiledEng.compile(inlineScript);
                    script.eval(ctx);
                    if (invoke) {
                        this.invokeFunctionOrMethod(shellState, engine, cl, argArray);
                    }
                } else {
                    engine.eval(inlineScript, ctx);
                    if (invoke) {
                        this.invokeFunctionOrMethod(shellState, engine, cl, argArray);
                    }
                }
            } catch (ScriptException ex) {
                shellState.printException(ex);
                return 1;
            } finally {
                if (null != writer) {
                    writer.close();
                }
            }
        }
        if (null != writer) {
            writer.close();
        }
    } else {
        printHelp(shellState);
    }
    return 0;
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleScriptContext(javax.script.SimpleScriptContext) FileWriter(java.io.FileWriter) Compilable(javax.script.Compilable) ScriptEngineManager(javax.script.ScriptEngineManager) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Reader(java.io.Reader) FileReader(java.io.FileReader) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) ArrayList(java.util.ArrayList) List(java.util.List) FileReader(java.io.FileReader) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 14 with ScriptContext

use of javax.script.ScriptContext in project smarthome by eclipse.

the class AbstractScriptModuleHandler method setExecutionContext.

/**
 * Adds the passed context variables of the rule engine to the context scope of the ScriptEngine, this should be
 * updated each time the module is executed
 *
 * @param engine the scriptengine that is used
 * @param context the variables and types to put into the execution context
 */
protected void setExecutionContext(ScriptEngine engine, Map<String, ?> context) {
    ScriptContext executionContext = engine.getContext();
    // Add the rule's UID to the context and make it available as "ctx".
    // Note: We don't use "context" here as it doesn't work on all JVM versions!
    final Map<String, Object> contextNew = new HashMap<>(context);
    contextNew.put("ruleUID", this.ruleUID);
    executionContext.setAttribute("ctx", contextNew, ScriptContext.ENGINE_SCOPE);
    // Add the rule's UID to the global namespace.
    executionContext.setAttribute("ruleUID", this.ruleUID, ScriptContext.ENGINE_SCOPE);
    // add the single context entries without their prefix to the scope
    for (Entry<String, ?> entry : context.entrySet()) {
        Object value = entry.getValue();
        String key = entry.getKey();
        int dotIndex = key.indexOf('.');
        if (dotIndex != -1) {
            key = key.substring(dotIndex + 1);
        }
        executionContext.setAttribute(key, value, ScriptContext.ENGINE_SCOPE);
    }
}
Also used : HashMap(java.util.HashMap) ScriptContext(javax.script.ScriptContext)

Example 15 with ScriptContext

use of javax.script.ScriptContext in project xwiki-platform by xwiki.

the class UntypedEventListener method evaluateVelocity.

private XDOM evaluateVelocity(Event event, Object source, DocumentReference userReference, String templateContent) throws Exception {
    ScriptContext currentScriptContext = scriptContextManager.getCurrentScriptContext();
    currentScriptContext.setAttribute(EVENT_BINDING_NAME, event, ScriptContext.ENGINE_SCOPE);
    currentScriptContext.setAttribute(SOURCE_BINDING_NAME, source, ScriptContext.ENGINE_SCOPE);
    try {
        Template customTemplate = templateManager.createStringTemplate(templateContent, userReference);
        return templateManager.execute(customTemplate);
    } finally {
        currentScriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
        currentScriptContext.removeAttribute(SOURCE_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
    }
}
Also used : ScriptContext(javax.script.ScriptContext) Template(org.xwiki.template.Template)

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