Bindings And Scopes : ScriptEngines « Development « Java Tutorial






import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class GetToKnowBindingsAndScopes {
  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 {
      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));
  }

  public static void dumpBindings(Bindings bindings) {
    if (bindings == null)
      System.out.println("  No bindings");
    else
      for (String key : bindings.keySet())
        System.out.println("  " + key + ": " + bindings.get(key));
    System.out.println();
  }
}








6.48.ScriptEngines
6.48.1.Obtain ScriptEngine
6.48.2.Enumerate ScriptEngines
6.48.3.Function Evaluator
6.48.4.Bindings And Scopes
6.48.5.Pass value and get return value from script
6.48.6.Temperature Conversion with script
6.48.7.Test Compilation Speed
6.48.8.Threading Behavior