View Javadoc

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