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