ScriptContext: ENGINE_SCOPE
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Main {
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));
}
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();
}
}
Related examples in the same category