1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2;
18
19 import java.io.Serializable;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Set;
26
27
28
29
30
31
32
33 public class SCXMLSystemContext implements Context, Serializable {
34
35
36
37
38 private static final long serialVersionUID = 1L;
39
40
41
42
43
44 public static final String EVENT_KEY = "_event";
45 public static final String SESSIONID_KEY = "_sessionid";
46 public static final String SCXML_NAME_KEY = "_name";
47 public static final String IOPROCESSORS_KEY = "_ioprocessors";
48 public static final String X_KEY = "_x";
49
50
51
52 public static final String STATUS_KEY = "status";
53
54
55
56
57 private static final Set<String> PROTECTED_NAMES = new HashSet<String>(Arrays.asList(
58 new String[] {EVENT_KEY, SESSIONID_KEY, SCXML_NAME_KEY, IOPROCESSORS_KEY, X_KEY}
59 ));
60
61
62
63
64
65 private Context systemContext;
66
67
68
69
70
71 private long nextSessionSequenceId;
72
73
74
75
76
77
78 void setSystemContext(Context systemContext) {
79 if (this.systemContext != null) {
80
81 systemContext.getVars().putAll(this.systemContext.getVars());
82 }
83 else {
84
85 systemContext.setLocal(X_KEY, new HashMap<String, Object>());
86 }
87 this.systemContext = systemContext;
88 this.protectedVars = Collections.unmodifiableMap(systemContext.getVars());
89 }
90
91
92
93
94 private Map<String, Object> protectedVars;
95
96 public SCXMLSystemContext(Context systemContext) {
97 setSystemContext(systemContext);
98 }
99
100 public String generateSessionId() {
101 return getContext().get(SESSIONID_KEY) + "-" + nextSessionSequenceId++;
102 }
103
104 @Override
105 public void set(final String name, final Object value) {
106 if (PROTECTED_NAMES.contains(name)) {
107 throw new UnsupportedOperationException();
108 }
109
110 systemContext.getParent().set(name, value);
111 }
112
113 @Override
114 public void setLocal(final String name, final Object value) {
115 throw new UnsupportedOperationException();
116 }
117
118 @Override
119 public Object get(final String name) {
120 return systemContext.get(name);
121 }
122
123 @Override
124 public boolean has(final String name) {
125 return systemContext.has(name);
126 }
127
128 @Override
129 public boolean hasLocal(final String name) {
130 return systemContext.hasLocal(name);
131 }
132
133 @Override
134 public Map<String, Object> getVars() {
135 return protectedVars;
136 }
137
138 @Override
139 public void reset() {
140 throw new UnsupportedOperationException();
141 }
142
143 @Override
144 public Context getParent() {
145 return systemContext.getParent();
146 }
147
148 @Override
149 public SCXMLSystemContext getSystemContext() {
150 return this;
151 }
152
153
154
155
156 @SuppressWarnings("unchecked")
157 public Map<String, Object> getPlatformVariables() {
158 return (Map<String, Object>)get(X_KEY);
159 }
160
161
162
163
164 Context getContext() {
165 return systemContext;
166 }
167 }