1 /*
2 * Copyright 1999-2001,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * <strong>HttpSessionScope</strong> is a specialized <code>Scope</code>
34 * implementation corresponding the the attributes of a specified
35 * <code>HttpSession</code>.
36 *
37 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
38 * @author Craig R. McClanahan
39 */
40
41 public class HttpSessionScope extends BaseScope {
42
43
44 // ----------------------------------------------------------- Constructors
45
46
47 /**
48 * Construct a new HttpSessionScope with no attached HttpSession.
49 */
50 public HttpSessionScope() {
51
52 super();
53
54 }
55
56
57 /**
58 * Construct a HttpSessionScope associated with the specified
59 * HttpSession.
60 *
61 * @param httpSession The associated HttpSession
62 */
63 public HttpSessionScope(HttpSession httpSession) {
64
65 super();
66 setHttpSession(httpSession);
67
68 }
69
70
71 // ------------------------------------------------------------- Properties
72
73
74 /**
75 * The HTTP session with which we are associated.
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 // ------------------------------------------------------------ Map Methods
89
90
91 /**
92 * Remove all beans from this Map and call <code>scopeCleared() on
93 * all registered <code>ScopeListeners</code>.
94 */
95 public void clear() {
96
97 // Accumulate a list of the elements to be cleared
98 Enumeration names = httpSession.getAttributeNames();
99 ArrayList list = new ArrayList();
100 while (names.hasMoreElements()) {
101 list.add((String) names.nextElement());
102 }
103
104 // Erase the accumulated elements
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 * Return <code>true</code> if this map contains the specified key.
116 *
117 * @param key Key to be looked up
118 */
119 public boolean containsKey(Object key) {
120
121 return (httpSession.getAttribute((String) key) != null);
122
123 }
124
125
126 /**
127 * Return <code>true</code> if this map contains the specified value.
128 *
129 * @param value Value to be looked up
130 */
131 public boolean containsValue(Object value) {
132
133 // Check all existing attributes for a match
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 * Return a set view of the mappings contained in this map.
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 * Compare the specified object with this map for equality.
163 *
164 * @param object Object to be compared
165 */
166 public boolean equals(Object object) {
167
168 return (httpSession.equals(object));
169
170 }
171
172
173 /**
174 * Return the value to which this map maps the specified key.
175 *
176 * @param key Key to be looked up
177 */
178 public Object get(Object key) {
179
180 return (get((String) key));
181
182 }
183
184
185 /**
186 * Return the value to which this map maps the specified key.
187 *
188 * @param key Key to be looked up
189 */
190 public Object get(String key) {
191
192 return (httpSession.getAttribute(key));
193
194 }
195
196
197 /**
198 * Return the hash code value for this map.
199 */
200 public int hashCode() {
201
202 return (httpSession.hashCode());
203
204 }
205
206
207 /**
208 * Return <code>true</code> if this map is empty.
209 */
210 public boolean isEmpty() {
211
212 // Check all existing attributes
213 Enumeration names = httpSession.getAttributeNames();
214 while (names.hasMoreElements())
215 return (true);
216 return (false);
217
218 }
219
220
221 /**
222 * Return a set view of the keys contained in this map.
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 * Add or replace the bean associated with the specified key.
237 *
238 * @param key Key with which the new value should be associated
239 * (cannot be null)
240 * @param bean Bean to be associated with this key (cannot be null)
241 */
242 public Object put(Object key, Object bean) {
243
244 return (put((String) key, bean));
245
246 }
247
248
249 /**
250 * Add the specified bean, associated with the specified key, to this
251 * scope and replace any previous bean associated with this key. If
252 * the bean was added, call <code>beanAdded()</code> on all registered
253 * listeners after the add is done. If an old bean was replaced,
254 * call <code>beanReplaced()</code> (passing the old value in the event)
255 * on all registered <code>ScopeListeners</code> after the removal
256 * is done. If a bean was replaced, the old value is also returned;
257 * otherwise <code>null</code> is returned.
258 *
259 * @param key Key with which the new value should be associated
260 * (cannot be null)
261 * @param bean Bean to be associated with this key (cannot be null)
262 *
263 * @exception IllegalArgumentException if <code>key</code> or
264 * <code>bean</code> is null
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 * Copy all of the mappings from the specified map into this map,
288 * firing appropriate <code>beanAdded()</code> and
289 * <code>beanReplaced()</code> events along the way.
290 *
291 * @param in Map whose contents are to be added
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 * Remove the bean associated with the specified key (if any), and return
306 * the old value if removed.
307 *
308 * @param key Key of the bean to remove (cannot be null)
309 */
310 public Object remove(Object key) {
311
312 return (remove((String) key));
313
314 }
315
316
317
318 /**
319 * Remove the bean associated with the specified key (if any). If such
320 * a bean is found and removed, call <code>beanRemoved()</code> on all
321 * registered <code>ScopeListeners</code> after the removal is done.
322 * Return the old value (if any); otherwise return <code>null</code>.
323 *
324 * @param key Key of the bean to remove (cannot be null)
325 *
326 * @exception IllegalArgumentException if <code>key</code> is null
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 * Return the number of key-value mappings in this map.
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 * Return a Collection view of the values contained in this map.
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 }