1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.servlet.context;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Enumeration;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
32 import javax.servlet.http.HttpServletRequest;
33
34 import javax.servlet.http.HttpSession;
35
36 import org.apache.tiles.context.MapEntry;
37
38 /***
39 * <p>Private implementation of <code>Map</code> for HTTP session
40 * attributes.</p>
41 *
42 * @version $Rev: 581987 $ $Date: 2007-10-04 21:03:29 +0200 (Thu, 04 Oct 2007) $
43 */
44
45 final class ServletSessionScopeMap implements Map<String, Object> {
46
47
48 /***
49 * Constructor.
50 *
51 * @param request The request object to use.
52 */
53 public ServletSessionScopeMap(HttpServletRequest request) {
54 this.request = request;
55 }
56
57 /***
58 * The request object to use.
59 */
60 private HttpServletRequest request = null;
61
62 /*** {@inheritDoc} */
63 public void clear() {
64 HttpSession session = request.getSession(false);
65 if (session != null) {
66 Iterator<String> keys = keySet().iterator();
67 while (keys.hasNext()) {
68 session.removeAttribute(keys.next());
69 }
70 }
71 }
72
73
74 /*** {@inheritDoc} */
75 public boolean containsKey(Object key) {
76 HttpSession session = request.getSession(false);
77 if (session == null) {
78 return false;
79 }
80
81 return (session.getAttribute(key(key)) != null);
82 }
83
84
85 /*** {@inheritDoc} */
86 @SuppressWarnings("unchecked")
87 public boolean containsValue(Object value) {
88 HttpSession session = request.getSession(false);
89 if (session == null || value == null) {
90 return (false);
91 }
92 Enumeration<String> keys = session.getAttributeNames();
93 while (keys.hasMoreElements()) {
94 Object next = session.getAttribute(keys.nextElement());
95 if (next == value) {
96 return (true);
97 }
98 }
99 return (false);
100 }
101
102
103 /*** {@inheritDoc} */
104 @SuppressWarnings("unchecked")
105 public Set<Map.Entry<String, Object>> entrySet() {
106 HttpSession session = request.getSession(false);
107 Set<Map.Entry<String, Object>> set = new HashSet<Map.Entry<String, Object>>();
108 if (session != null) {
109 Enumeration<String> keys = session.getAttributeNames();
110 String key;
111 while (keys.hasMoreElements()) {
112 key = keys.nextElement();
113 set.add(new MapEntry<String, Object>(key,
114 session.getAttribute(key), true));
115 }
116 }
117 return (set);
118 }
119
120
121 /*** {@inheritDoc} */
122 @SuppressWarnings("unchecked")
123 public boolean equals(Object o) {
124 boolean retValue = true;
125
126 HttpSession session = request.getSession(false);
127 synchronized (session) {
128 HttpSession otherSession = ((ServletSessionScopeMap) o).request
129 .getSession(false);
130 if (session == null) {
131 retValue = otherSession == null;
132 } else {
133 for (Enumeration<String> attribs = session.getAttributeNames(); attribs
134 .hasMoreElements()
135 && retValue;) {
136 String attributeName = attribs.nextElement();
137 retValue = session.getAttribute(attributeName).equals(
138 otherSession.getAttribute(attributeName));
139 }
140 }
141 }
142
143 return retValue;
144 }
145
146
147 /*** {@inheritDoc} */
148 public Object get(Object key) {
149 HttpSession session = request.getSession(false);
150 if (session == null) {
151 return null;
152 }
153
154 return (session.getAttribute(key(key)));
155 }
156
157
158 /*** {@inheritDoc} */
159 public int hashCode() {
160 HttpSession session = request.getSession(false);
161 if (session == null) {
162 return 0;
163 }
164
165 return (session.hashCode());
166 }
167
168
169 /*** {@inheritDoc} */
170 public boolean isEmpty() {
171 return (size() < 1);
172 }
173
174
175 /*** {@inheritDoc} */
176 @SuppressWarnings("unchecked")
177 public Set<String> keySet() {
178 HttpSession session = request.getSession(false);
179 Set<String> set = new HashSet<String>();
180 if (session != null) {
181 Enumeration<String> keys = session.getAttributeNames();
182 while (keys.hasMoreElements()) {
183 set.add(keys.nextElement());
184 }
185 }
186 return (set);
187 }
188
189
190 /*** {@inheritDoc} */
191 public Object put(String key, Object value) {
192 HttpSession session = request.getSession();
193 if (value == null) {
194 return (remove(key));
195 }
196 String skey = key(key);
197 Object previous = session.getAttribute(skey);
198 session.setAttribute(skey, value);
199 return (previous);
200 }
201
202
203 /*** {@inheritDoc} */
204 public void putAll(Map<? extends String, ? extends Object> map) {
205 HttpSession session = request.getSession();
206 Iterator<? extends String> keys = map.keySet().iterator();
207 while (keys.hasNext()) {
208 String key = keys.next();
209 session.setAttribute(key, map.get(key));
210 }
211 }
212
213
214 /*** {@inheritDoc} */
215 public Object remove(Object key) {
216 HttpSession session = request.getSession(false);
217 if (session == null) {
218 return null;
219 }
220
221 String skey = key(key);
222 Object previous = session.getAttribute(skey);
223 session.removeAttribute(skey);
224 return (previous);
225 }
226
227
228 /*** {@inheritDoc} */
229 @SuppressWarnings("unchecked")
230 public int size() {
231 HttpSession session = request.getSession(false);
232 int n = 0;
233 if (session != null) {
234 Enumeration<String> keys = session.getAttributeNames();
235 while (keys.hasMoreElements()) {
236 keys.nextElement();
237 n++;
238 }
239 }
240 return (n);
241 }
242
243
244 /*** {@inheritDoc} */
245 @SuppressWarnings("unchecked")
246 public Collection<Object> values() {
247 HttpSession session = request.getSession(false);
248 List<Object> list = new ArrayList<Object>();
249 if (session != null) {
250 Enumeration<String> keys = session.getAttributeNames();
251 while (keys.hasMoreElements()) {
252 list.add(session.getAttribute(keys.nextElement()));
253 }
254 }
255 return (list);
256 }
257
258
259 /***
260 * Returns the string representation of the key.
261 *
262 * @param key The key.
263 * @return The string representation of the key.
264 * @throws IllegalArgumentException If the key is <code>null</code>.
265 */
266 private String key(Object key) {
267 if (key == null) {
268 throw new IllegalArgumentException();
269 } else if (key instanceof String) {
270 return ((String) key);
271 } else {
272 return (key.toString());
273 }
274 }
275
276
277 }