View Javadoc

1   /*
2    * $Id: SessionMap.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import java.io.Serializable;
21  import java.util.AbstractMap;
22  import java.util.Collections;
23  import java.util.Enumeration;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpSession;
30  
31  
32  /***
33   * A simple implementation of the {@link java.util.Map} interface to handle a collection of HTTP session
34   * attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
35   * Note, this will occur lazily - only when the entry set is asked for.
36   *
37   */
38  public class SessionMap extends AbstractMap implements Serializable {
39  
40  	private static final long serialVersionUID = 4678843241638046854L;
41  	
42  	protected HttpSession session;
43      protected Set<Object> entries;
44      protected HttpServletRequest request;
45  
46  
47      /***
48       * Creates a new session map given a http servlet request. Note, ths enumeration of request
49       * attributes will occur when the map entries are asked for.
50       *
51       * @param request the http servlet request object.
52       */
53      public SessionMap(HttpServletRequest request) {
54          // note, holding on to this request and relying on lazy session initalization will not work
55          // if you are running your action invocation in a background task, such as using the
56          // "exec-and-wait" interceptor
57          this.request = request;
58          this.session = request.getSession(false);
59      }
60  
61      /***
62       * Invalidate the http session.
63       */
64      public void invalidate() {
65  		if (session == null) {
66  			return;
67  		}
68  
69  		synchronized (session) {
70  			session.invalidate();
71  			session = null;
72  			entries = null;
73  		}
74  	}
75  
76      /***
77  	 * Removes all attributes from the session as well as clears entries in this
78  	 * map.
79  	 */
80      public void clear() {
81          if (session == null ) {
82              return;
83          }
84  
85          synchronized (session) {
86          	entries = null;
87          	Enumeration<String> attributeNamesEnum = session.getAttributeNames();
88          	while(attributeNamesEnum.hasMoreElements()) {
89          		session.removeAttribute(attributeNamesEnum.nextElement());
90          	}
91          }
92          
93      }
94  
95      /***
96       * Returns a Set of attributes from the http session.
97       *
98       * @return a Set of attributes from the http session.
99       */
100     public Set entrySet() {
101         if (session == null) {
102             return Collections.EMPTY_SET;
103         }
104 
105         synchronized (session) {
106             if (entries == null) {
107                 entries = new HashSet<Object>();
108 
109                 Enumeration enumeration = session.getAttributeNames();
110 
111                 while (enumeration.hasMoreElements()) {
112                     final String key = enumeration.nextElement().toString();
113                     final Object value = session.getAttribute(key);
114                     entries.add(new Map.Entry() {
115                         public boolean equals(Object obj) {
116                             Map.Entry entry = (Map.Entry) obj;
117 
118                             return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
119                         }
120 
121                         public int hashCode() {
122                             return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
123                         }
124 
125                         public Object getKey() {
126                             return key;
127                         }
128 
129                         public Object getValue() {
130                             return value;
131                         }
132 
133                         public Object setValue(Object obj) {
134                             session.setAttribute(key.toString(), obj);
135 
136                             return value;
137                         }
138                     });
139                 }
140             }
141         }
142 
143         return entries;
144     }
145 
146     /***
147      * Returns the session attribute associated with the given key or <tt>null</tt> if it doesn't exist.
148      *
149      * @param key the name of the session attribute.
150      * @return the session attribute or <tt>null</tt> if it doesn't exist.
151      */
152     public Object get(Object key) {
153         if (session == null) {
154             return null;
155         }
156 
157         synchronized (session) {
158             return session.getAttribute(key.toString());
159         }
160     }
161 
162     /***
163      * Saves an attribute in the session.
164      *
165      * @param key   the name of the session attribute.
166      * @param value the value to set.
167      * @return the object that was just set.
168      */
169     public Object put(Object key, Object value) {
170         synchronized (this) {
171             if (session == null) {
172                 session = request.getSession(true);
173             }
174         }
175 
176         synchronized (session) {
177             entries = null;
178             session.setAttribute(key.toString(), value);
179 
180             return get(key);
181         }
182     }
183 
184     /***
185      * Removes the specified session attribute.
186      *
187      * @param key the name of the attribute to remove.
188      * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
189      */
190     public Object remove(Object key) {
191         if (session == null) {
192             return null;
193         }
194 
195         synchronized (session) {
196             entries = null;
197 
198             Object value = get(key);
199             session.removeAttribute(key.toString());
200 
201             return value;
202         }
203     }
204 }