1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.chain.web.servlet;
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.List;
26 import java.util.Map;
27 import java.util.Set;
28 import javax.servlet.http.HttpSession;
29 import javax.servlet.http.HttpServletRequest;
30 import org.apache.commons.chain.web.MapEntry;
31
32
33
34
35
36
37
38
39
40
41 final class ServletSessionScopeMap implements Map {
42
43
44 public ServletSessionScopeMap(HttpServletRequest request) {
45 this.request = request;
46 sessionExists();
47 }
48
49
50 private HttpSession session = null;
51 private HttpServletRequest request = null;
52
53
54 public void clear() {
55 if (sessionExists()) {
56 Iterator keys = keySet().iterator();
57 while (keys.hasNext()) {
58 session.removeAttribute((String) keys.next());
59 }
60 }
61 }
62
63
64 public boolean containsKey(Object key) {
65 if (sessionExists()) {
66 return (session.getAttribute(key(key)) != null);
67 } else {
68 return false;
69 }
70 }
71
72
73 public boolean containsValue(Object value) {
74 if (value == null || !sessionExists()) {
75 return (false);
76 }
77 Enumeration keys = session.getAttributeNames();
78 while (keys.hasMoreElements()) {
79 Object next = session.getAttribute((String) keys.nextElement());
80 if (value.equals(next)) {
81 return (true);
82 }
83 }
84 return (false);
85 }
86
87
88 public Set entrySet() {
89 Set set = new HashSet();
90 if (sessionExists()) {
91 Enumeration keys = session.getAttributeNames();
92 String key;
93 while (keys.hasMoreElements()) {
94 key = (String) keys.nextElement();
95 set.add(new MapEntry(key, session.getAttribute(key), true));
96 }
97 }
98 return (set);
99 }
100
101
102 public boolean equals(Object o) {
103 if (sessionExists()) {
104 return (session.equals(o));
105 } else {
106 return false;
107 }
108 }
109
110
111 public Object get(Object key) {
112 if (sessionExists()) {
113 return (session.getAttribute(key(key)));
114 } else {
115 return null;
116 }
117 }
118
119
120 public int hashCode() {
121 if (sessionExists()) {
122 return (session.hashCode());
123 } else {
124 return 0;
125 }
126 }
127
128
129 public boolean isEmpty() {
130 if (sessionExists() &&
131 session.getAttributeNames().hasMoreElements()) {
132 return false;
133 } else {
134 return true;
135 }
136 }
137
138
139 public Set keySet() {
140 Set set = new HashSet();
141 if (sessionExists()) {
142 Enumeration keys = session.getAttributeNames();
143 while (keys.hasMoreElements()) {
144 set.add(keys.nextElement());
145 }
146 }
147 return (set);
148 }
149
150
151 public Object put(Object key, Object value) {
152 if (value == null) {
153 return (remove(key));
154 }
155
156
157
158 if (session == null) {
159 session = request.getSession();
160 request = null;
161 }
162
163 String skey = key(key);
164 Object previous = session.getAttribute(skey);
165 session.setAttribute(skey, value);
166 return (previous);
167 }
168
169
170 public void putAll(Map map) {
171 Iterator entries = map.entrySet().iterator();
172 while (entries.hasNext()) {
173 Map.Entry entry = (Map.Entry)entries.next();
174 put(entry.getKey(), entry.getValue());
175 }
176 }
177
178
179 public Object remove(Object key) {
180 if (sessionExists()) {
181 String skey = key(key);
182 Object previous = session.getAttribute(skey);
183 session.removeAttribute(skey);
184 return (previous);
185 } else {
186 return (null);
187 }
188 }
189
190
191 public int size() {
192 int n = 0;
193 if (sessionExists()) {
194 Enumeration keys = session.getAttributeNames();
195 while (keys.hasMoreElements()) {
196 keys.nextElement();
197 n++;
198 }
199 }
200 return (n);
201 }
202
203
204 public Collection values() {
205 List list = new ArrayList();
206 if (sessionExists()) {
207 Enumeration keys = session.getAttributeNames();
208 while (keys.hasMoreElements()) {
209 list.add(session.getAttribute((String) keys.nextElement()));
210 }
211 }
212 return (list);
213 }
214
215
216 private String key(Object key) {
217 if (key == null) {
218 throw new IllegalArgumentException();
219 } else if (key instanceof String) {
220 return ((String) key);
221 } else {
222 return (key.toString());
223 }
224 }
225
226 private boolean sessionExists() {
227 if (session == null) {
228 session = request.getSession(false);
229 if (session != null) {
230 request = null;
231 }
232 }
233 if (session != null) {
234 return true;
235 } else {
236 return false;
237 }
238 }
239
240 }