1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.scxml2.env.javascript;
19
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24
25 import javax.script.Bindings;
26 import javax.script.SimpleBindings;
27
28 import org.apache.commons.scxml2.Context;
29
30
31
32
33
34
35
36 public class JSBindings implements Bindings {
37
38 private static final String NASHORN_GLOBAL = "nashorn.global";
39
40
41
42 private Bindings bindings;
43 private Context context;
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public JSBindings(Context context, Bindings bindings) {
58
59
60 if (context == null) {
61 throw new IllegalArgumentException("Invalid SCXML context");
62 }
63
64 if (bindings == null) {
65 throw new IllegalArgumentException("Invalid script Bindings");
66 }
67
68
69
70 this.bindings = bindings;
71 this.context = context;
72 }
73
74
75
76
77
78
79
80
81
82 @Override
83 public boolean containsKey(Object key) {
84 if (hasGlobalBindings() && getGlobalBindings().containsKey(key)) {
85 return true;
86 }
87
88 if (bindings.containsKey(key)) {
89 return true;
90 }
91
92 return context.has(key.toString());
93 }
94
95
96
97
98
99
100
101
102 @Override
103 public Set<String> keySet() {
104 Set<String> keys = new HashSet<String>();
105
106 keys.addAll(context.getVars().keySet());
107 keys.addAll(bindings.keySet());
108
109 if (hasGlobalBindings()) {
110 keys.addAll(getGlobalBindings().keySet());
111 }
112
113 return keys;
114 }
115
116
117
118
119
120
121
122
123
124 @Override
125 public int size() {
126 Set<String> keys = new HashSet<String>();
127
128 keys.addAll(context.getVars().keySet());
129 keys.addAll(bindings.keySet());
130
131 if (hasGlobalBindings()) {
132 keys.addAll(getGlobalBindings().keySet());
133 }
134
135 return keys.size();
136 }
137
138
139
140
141
142
143
144
145 @Override
146 public boolean containsValue(Object value) {
147 if (hasGlobalBindings() && getGlobalBindings().containsValue(value)) {
148 return true;
149 }
150
151 if (bindings.containsValue(value)) {
152 return true;
153 }
154
155 return context.getVars().containsValue(value);
156 }
157
158
159
160
161
162
163
164
165 @Override
166 public Set<Map.Entry<String,Object>> entrySet() {
167 return union().entrySet();
168 }
169
170
171
172
173
174
175
176
177 @Override
178 public Collection<Object> values() {
179 return union().values();
180 }
181
182
183
184
185
186
187
188
189 @Override
190 public boolean isEmpty() {
191 if (hasGlobalBindings() && !getGlobalBindings().isEmpty()) {
192 return false;
193 }
194
195 if (!bindings.isEmpty()) {
196 return false;
197 }
198
199 return context.getVars().isEmpty();
200 }
201
202
203
204
205
206
207 @Override
208 public Object get(Object key) {
209
210 if (NASHORN_GLOBAL.equals(key)) {
211 return bindings.get(key);
212 }
213
214 if (hasGlobalBindings() && getGlobalBindings().containsKey(key)) {
215 return getGlobalBindings().get(key);
216 }
217
218 if (bindings.containsKey(key)) {
219 return bindings.get(key);
220 }
221
222 return context.get(key.toString());
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236 @Override
237 public Object put(String name, Object value) {
238 Object old = context.get(name);
239
240
241 if (NASHORN_GLOBAL.equals(name)) {
242 return bindings.put(name, value);
243 } else if (context.has(name)) {
244 context.set(name, value);
245 } else if (bindings.containsKey(name)) {
246 return bindings.put(name, value);
247 } else if (hasGlobalBindings() && getGlobalBindings().containsKey(name)) {
248 return getGlobalBindings().put(name, value);
249 } else {
250 context.setLocal(name, value);
251 }
252
253 return old;
254 }
255
256
257
258
259
260
261
262
263 @Override
264 public void putAll(Map<? extends String, ? extends Object> toMerge) {
265 bindings.putAll(toMerge);
266 }
267
268
269
270
271
272
273
274
275
276
277 @Override
278 public Object remove(Object key) {
279 if (hasGlobalBindings() && getGlobalBindings().containsKey(key)) {
280 getGlobalBindings().remove(key);
281 }
282
283 if (bindings.containsKey(key)) {
284 return bindings.remove(key);
285 }
286
287 if (context.has(key.toString())) {
288 return context.getVars().remove(key);
289 }
290
291 return Boolean.FALSE;
292 }
293
294
295
296
297
298
299
300
301 @Override
302 public void clear() {
303 bindings.clear();
304 }
305
306
307
308
309
310
311 private Bindings union() {
312 Bindings set = new SimpleBindings();
313
314 set.putAll(context.getVars());
315
316 for (String key : bindings.keySet()) {
317 set.put(key, bindings.get(key));
318 }
319
320 if (hasGlobalBindings()) {
321 for (String key : getGlobalBindings().keySet()) {
322 set.put(key, getGlobalBindings().get(key));
323 }
324 }
325
326 return set;
327 }
328
329
330
331
332
333
334
335
336
337 protected boolean hasGlobalBindings() {
338 if (bindings.containsKey(NASHORN_GLOBAL)) {
339 return true;
340 }
341
342 return false;
343 }
344
345
346
347
348
349 protected Bindings getGlobalBindings() {
350 if (bindings.containsKey(NASHORN_GLOBAL)) {
351 return (Bindings) bindings.get(NASHORN_GLOBAL);
352 }
353
354 return null;
355 }
356 }