Example usage for javax.script ScriptContext ENGINE_SCOPE

List of usage examples for javax.script ScriptContext ENGINE_SCOPE

Introduction

In this page you can find the example usage for javax.script ScriptContext ENGINE_SCOPE.

Prototype

int ENGINE_SCOPE

To view the source code for javax.script ScriptContext ENGINE_SCOPE.

Click Source Link

Document

EngineScope attributes are visible during the lifetime of a single ScriptEngine and a set of attributes is maintained for each engine.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptContext ctx = new SimpleScriptContext();
    ctx.setAttribute("year", 2015, ScriptContext.ENGINE_SCOPE);
    ctx.setAttribute("month", 9, ScriptContext.ENGINE_SCOPE);
    ctx.setAttribute("day", 21, ScriptContext.ENGINE_SCOPE);

}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngine jse = new ScriptEngineManager().getEngineByName("javascript");
    Bindings bindings = jse.getBindings(ScriptContext.ENGINE_SCOPE);
    Main test = new Main();
    bindings.put("test", test);
    String preFix = "demo";
    jse.eval("test." + preFix + "_test()");
    preFix = "actual";
    jse.eval("test." + preFix + "_test()");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    engine.put("a", 1);
    engine.put("b", 5);

    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    Object a = bindings.get("a");
    Object b = bindings.get("b");
    System.out.println("a = " + a);
    System.out.println("b = " + b);

    Object result = engine.eval("c = a + b;");
    System.out.println("a + b = " + result);
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    engine.put("a", 1);
    engine.put("b", 5);

    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    Object a = bindings.get("a");
    Object b = bindings.get("b");
    System.out.println("a = " + a);
    System.out.println("b = " + b);

    Object result;//  ww  w .j  a  v a2  s .c  o  m
    try {
        result = engine.eval("c = aaaa + bbbb;");
        System.out.println("a + b = " + result);
    } catch (ScriptException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    manager.put("global", "global bindings");

    dumpBindings(manager.getBindings());
    ScriptEngine engine = manager.getEngineByExtension("js");
    engine.put("engine", "engine bindings");

    dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE));

    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));

    Bindings bindings = engine.createBindings();
    bindings.put("engine", "overridden engine bindings");
    bindings.put("bindings", bindings);
    engine.eval("app.dumpBindings (bindings);", bindings);

    ScriptEngine engine2 = manager.getEngineByExtension("js");
    engine2.put("engine2", "engine2 bindings");

    dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE));
    dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE));
    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));
}

From source file:GetToKnowBindingsAndScopes.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    manager.put("global", "global bindings");

    dumpBindings(manager.getBindings());
    ScriptEngine engine = manager.getEngineByExtension("js");
    engine.put("engine", "engine bindings");

    dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE));

    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));

    try {/*from   w  ww.  j  a  v  a2  s .c  o  m*/
        Bindings bindings = engine.createBindings();
        bindings.put("engine", "overridden engine bindings");
        bindings.put("app", new GetToKnowBindingsAndScopes());
        bindings.put("bindings", bindings);
        engine.eval("app.dumpBindings (bindings);", bindings);
    } catch (ScriptException se) {
        System.err.println(se.getMessage());
    }

    ScriptEngine engine2 = manager.getEngineByExtension("js");
    engine2.put("engine2", "engine2 bindings");

    dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE));
    dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE));
    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));
}

From source file:io.cloudslang.lang.runtime.bindings.ScriptEvaluator.java

public Serializable evalExpr(String expr, Map<String, ? extends Serializable> context) {
    ScriptContext scriptContext = new SimpleScriptContext();
    for (Map.Entry<String, ? extends Serializable> entry : context.entrySet()) {
        scriptContext.setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE);
    }/*from  w  ww  .j ava 2s .  c om*/
    if (scriptContext.getAttribute(TRUE) == null)
        scriptContext.setAttribute(TRUE, Boolean.TRUE, ScriptContext.ENGINE_SCOPE);
    if (scriptContext.getAttribute(FALSE) == null)
        scriptContext.setAttribute(FALSE, Boolean.FALSE, ScriptContext.ENGINE_SCOPE);
    try {
        return (Serializable) engine.eval(expr, scriptContext);
    } catch (ScriptException e) {
        ScriptException scriptException = new ScriptException(e);
        throw new RuntimeException("Error in running script expression or variable reference, for expression: '"
                + expr + "',\n\tScript exception is: " + scriptException.getMessage(), scriptException);
    }
}

From source file:org.openscore.lang.runtime.bindings.ScriptEvaluator.java

public Serializable evalExpr(String expr, Map<String, ? extends Serializable> context) {
    ScriptContext scriptContext = new SimpleScriptContext();
    for (Map.Entry<String, ? extends Serializable> entry : context.entrySet()) {
        scriptContext.setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE);
    }//  w  w  w . ja va2  s.co  m
    if (scriptContext.getAttribute(TRUE) == null)
        scriptContext.setAttribute(TRUE, Boolean.TRUE, ScriptContext.ENGINE_SCOPE);
    if (scriptContext.getAttribute(FALSE) == null)
        scriptContext.setAttribute(FALSE, Boolean.FALSE, ScriptContext.ENGINE_SCOPE);
    try {
        return (Serializable) engine.eval(expr, scriptContext);
    } catch (ScriptException e) {
        ScriptException scriptException = new ScriptException(e);
        throw new RuntimeException("Error in running script expression or variable reference, for expression: '"
                + expr + "', Script exception is: \n" + scriptException.getMessage(), scriptException);
    }
}

From source file:org.paxml.test.SelfTest.java

public void testSyntax() throws Exception {
    ScriptEngine runtime = new ScriptEngineManager().getEngineByName("javascript");
    Bindings bindings = runtime.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("util", "xx");
    runtime.setBindings(new SimpleBindings() {

    }, ScriptContext.ENGINE_SCOPE);

}

From source file:io.lightlink.translator.JSBeautifyPostProcessor.java

@Override
public String process(String script) throws IOException {
    String beautifier = IOUtils.toString(Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("io/lightlink/translator/beautify.js"));

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    try {//from  ww w . j a v  a2 s .  c o m
        engine.eval(beautifier);
        engine.getBindings(ScriptContext.ENGINE_SCOPE).put("$originalScript", script);
        return engine.eval("js_beautify($originalScript, {})") + "";
    } catch (ScriptException e) {
        LOG.error(e.toString(), e);
        return script;
    }

}