1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2.env.groovy;
18
19 import groovy.lang.Binding;
20 import groovy.lang.MissingPropertyException;
21
22 import java.io.Serializable;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25
26
27
28
29 public class GroovyContextBinding extends Binding implements Serializable {
30
31 private static final long serialVersionUID = 1L;
32
33 private GroovyContext context;
34
35 public GroovyContextBinding(GroovyContext context) {
36 if (context == null) {
37 throw new IllegalArgumentException("Parameter context may not be null");
38 }
39 this.context = context;
40 }
41
42 GroovyContext getContext() {
43 return context;
44 }
45
46 @Override
47 public Object getVariable(String name) {
48 Object result = context.get(name);
49 if (result == null && !context.has(name)) {
50 throw new MissingPropertyException(name, this.getClass());
51 }
52 return result;
53 }
54
55 @Override
56 public void setVariable(String name, Object value) {
57 if (context.has(name)) {
58 context.set(name, value);
59 } else {
60 context.setLocal(name, value);
61 }
62 }
63
64 @Override
65 public boolean hasVariable(String name) {
66 return context.has(name);
67 }
68
69 @Override
70 public Map<String, Object> getVariables() {
71 return new LinkedHashMap<String, Object>(context.getVars());
72 }
73
74 @Override
75 public Object getProperty(String property) {
76 return getVariable(property);
77 }
78
79 @Override
80 public void setProperty(String property, Object newValue) {
81 setVariable(property, newValue);
82 }
83 }