Example usage for javax.script ScriptEngine getBindings

List of usage examples for javax.script ScriptEngine getBindings

Introduction

In this page you can find the example usage for javax.script ScriptEngine getBindings.

Prototype

public Bindings getBindings(int scope);

Source Link

Document

Returns a scope of named values.

Usage

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;/*from  www  .j av  a  2 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 {/*  w ww  .  j  a  va 2 s .  co  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:org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory.java

public static void persistCurrentContext(AuthenticationContext context, ScriptEngine engine) {

    Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    Map<String, Object> persistableMap = new HashMap<>();
    engineBindings.forEach((key, value) -> persistableMap.put(key, FrameworkUtils.toJsSerializable(value)));
    context.setProperty(JS_BINDING_CURRENT_CONTEXT, persistableMap);
}

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.JsonSerializerTest.java

private static Bindings executeJavaScript(String jsScript) throws ScriptException {
    NashornScriptEngineFactory scriptEngineFactory = new NashornScriptEngineFactory();
    ScriptEngine engine = scriptEngineFactory.getScriptEngine("-strict", "--optimistic-types");
    engine.eval(jsScript);/*from w w  w. jav a2 s  . com*/
    return engine.getBindings(ScriptContext.ENGINE_SCOPE);
}

From source file:org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory.java

public static void restoreCurrentContext(AuthenticationContext context, ScriptEngine engine)
        throws FrameworkException {

    Map<String, Object> map = (Map<String, Object>) context.getProperty(JS_BINDING_CURRENT_CONTEXT);
    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    if (map != null) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Object deserializedValue = FrameworkUtils.fromJsSerializable(entry.getValue(), engine);
            if (deserializedValue instanceof AbstractJSObjectWrapper) {
                ((AbstractJSObjectWrapper) deserializedValue).initializeContext(context);
            }// w ww . j  a  va 2  s  .c om
            bindings.put(entry.getKey(), deserializedValue);
        }
    }
}

From source file:org.rhq.bindings.ScriptEngineFactory.java

/**
 * Remove the specified bindings from the engine.
 * //from w  w w  .  j a va2s  .c  o m
 * @param engine the engine
 * @param keySet the binding keys to be removed
 */
public static void removeBindings(ScriptEngine engine, Set<String> keySet) {

    Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);

    for (String key : keySet) {
        engineBindings.remove(key);
    }

    engine.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE);
}

From source file:org.rhq.bindings.ScriptEngineFactory.java

/**
 * Injects the values provided in the bindings into the {@link ScriptContext#ENGINE_SCOPE engine scope}
 * of the provided script engine.//  w ww . ja v a 2s.  c  o  m
 * 
 * @param engine the engine
 * @param bindings the bindings
 * @param deleteExistingBindings true if the existing bindings should be replaced by the provided ones, false
 * if the provided bindings should be added to the existing ones (possibly overwriting bindings with the same name).
 */
public static void injectStandardBindings(ScriptEngine engine, StandardBindings bindings,
        boolean deleteExistingBindings) {
    bindings.preInject(engine);

    Bindings engineBindings = deleteExistingBindings ? engine.createBindings()
            : engine.getBindings(ScriptContext.ENGINE_SCOPE);

    for (Map.Entry<String, Object> entry : bindings.entrySet()) {
        engineBindings.put(entry.getKey(), entry.getValue());
    }

    engine.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE);

    bindings.postInject(engine);
}