View Javadoc

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