Global Scope and Engine Scope Bindings

This post explains about bindings and how we can use them to pass java objects to the script code.

Java Scripting API provides map data structure which can be accessed, modified using Bindings Interface. The map entry’s key matches with the variable name in the script and their value can be a primitive value or a java object. Once bindings are populated, we need to set it to the script engine manager or script engine, which will be responsible for executing the script (the script which wants access to those java object).

Bindings set to script engine manager will be accessible to all script engines created by that script engine manager. This scope of access is called GLOBAL_SCOPE.

Whereas bindings set to a particular script engine will be accessible to only that script engine. This scope of access is called ENGINE_SCOPE.

So a script engine will have access to both its ENGINE_SCOPE and GLOBAL_SCOPE bindings. You can access these bindings belonging to a particular scope using the below method available in ScriptEngine api.

Bindings getBindings(int scope)

The below code gives you an example of bindings and how to use them in scripts

Main Code


1  package package1;
2  
3  import java.io.File;
4  import java.io.FileNotFoundException;
5  import java.io.FileReader;
6  import java.io.IOException;
7  
8  import javax.script.Bindings;
9  import javax.script.ScriptContext;
10 import javax.script.ScriptEngine;
11 import javax.script.ScriptEngineManager;
12 import javax.script.ScriptException;
13 
14 public class Demo6 {
15  public static void main(String[] args) throws FileNotFoundException, ScriptException, IOException {
16      ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
17      
18      System.out.println("Before setting global and engine scope bindings for script engines through ScriptEngineManager");
19      
20      ScriptEngine scriptEngine1 = scriptEngineManager.getEngineByName("JavaScript");
21      Bindings bindings = scriptEngine1.getBindings(ScriptContext.GLOBAL_SCOPE);
22      System.out.println("Script Engine 1 global scope bindings");
23      for(String key : bindings.keySet()) {
24          System.out.println(bindings.get(key));
25      }
26      bindings = scriptEngine1.getBindings(ScriptContext.ENGINE_SCOPE);
27      System.out.println("Script Engine 1 engine scope bindings");
28      for(String key : bindings.keySet()) {
29          System.out.println(bindings.get(key));
30      }
31      
32      ScriptEngine scriptEngine2 = scriptEngineManager.getEngineByName("JavaScript");
33      bindings = scriptEngine2.getBindings(ScriptContext.GLOBAL_SCOPE);
34      System.out.println("Script Engine 2 global scope bindings");
35      for(String key : bindings.keySet()) {
36          System.out.println(bindings.get(key));
37      }
38      bindings = scriptEngine2.getBindings(ScriptContext.ENGINE_SCOPE);
39      System.out.println("Script Engine 2 engine scope bindings");
40      for(String key : bindings.keySet()) {
41          System.out.println(bindings.get(key));
42      }
43      
44      System.out.println();
45      System.out.println("After setting global and engine scope bindings through ScriptEngineManager");
46      scriptEngineManager.getBindings().put("key1", "value1");
47      
48      bindings = scriptEngine1.getBindings(ScriptContext.GLOBAL_SCOPE);
49      System.out.println("Script Engine 1 global scope bindings");
50      for(String key : bindings.keySet()) {
51          System.out.println(bindings.get(key));
52      }
53      
54      scriptEngine1.getBindings(ScriptContext.ENGINE_SCOPE).put("key2", "value2");
55      bindings = scriptEngine1.getBindings(ScriptContext.ENGINE_SCOPE);
56      System.out.println("Script Engine 1 engine scope bindings");
57      for(String key : bindings.keySet()) {
58          System.out.println(bindings.get(key));
59      }
60      
61      bindings = scriptEngine2.getBindings(ScriptContext.GLOBAL_SCOPE);
62      System.out.println("Script Engine 2 global scope bindings");
63      for(String key : bindings.keySet()) {
64          System.out.println(bindings.get(key));
65      }
66      bindings = scriptEngine2.getBindings(ScriptContext.ENGINE_SCOPE);
67      System.out.println("Script Engine 2 engine scope bindings");
68      for(String key : bindings.keySet()) {
69          System.out.println(bindings.get(key));
70      }
71      
72      System.out.println();
73      System.out.println("Result of 'test2.js' script execution by Script engine 1");
74      File file = new File("test2.js");
75      try(FileReader fr = new FileReader(file);) {
76          scriptEngine1.eval(fr);
77      }
78  }
79 }

Explanation

As you can see from the above code we create two script engines named scriptEngine1 and scriptEngine2 (refer line at 20 and 32) with the help of an instance of ScriptEngineManager. Initally none of them has any data in Global or engine scope.

At line 46, we add an entry in global scope which is immediately avaiable to both the script engines, this you can see when you loop through their bindings.

At line 54, we add an entry in engine scope of scriptEngine1, which is immediately available to scripts executed by scriptEngine1 but not to scriptEngine2.
This you can see when you loop through their bindings.

Script Code ‘test2.js’


print(key1);
print(key2);

Explanation

As you can see from the javascript code. I am referring the keys “key1” and “key2” (created in java code) in the javascript code.

Output

Before setting global and engine scope bindings for script engines through ScriptEngineManager
Script Engine 1 global scope bindings
Script Engine 1 engine scope bindings
Script Engine 2 global scope bindings
Script Engine 2 engine scope bindings

After setting global and engine scope bindings through ScriptEngineManager
Script Engine 1 global scope bindings
value1
Script Engine 1 engine scope bindings
value2
Script Engine 2 global scope bindings
value1
Script Engine 2 engine scope bindings

Result of ‘test2.js’ script execution by Script engine 1
value1
value2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s