001: /*
002: * $Id: SessionMap.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import java.io.Serializable;
024: import java.util.AbstractMap;
025: import java.util.Collections;
026: import java.util.Enumeration;
027: import java.util.HashSet;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpSession;
033:
034: /**
035: * A simple implementation of the {@link java.util.Map} interface to handle a collection of HTTP session
036: * attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
037: * Note, this will occur lazily - only when the entry set is asked for.
038: *
039: */
040: public class SessionMap extends AbstractMap implements Serializable {
041:
042: private static final long serialVersionUID = 4678843241638046854L;
043:
044: protected HttpSession session;
045: protected Set<Object> entries;
046: protected HttpServletRequest request;
047:
048: /**
049: * Creates a new session map given a http servlet request. Note, ths enumeration of request
050: * attributes will occur when the map entries are asked for.
051: *
052: * @param request the http servlet request object.
053: */
054: public SessionMap(HttpServletRequest request) {
055: // note, holding on to this request and relying on lazy session initalization will not work
056: // if you are running your action invocation in a background task, such as using the
057: // "exec-and-wait" interceptor
058: this .request = request;
059: this .session = request.getSession(false);
060: }
061:
062: /**
063: * Invalidate the http session.
064: */
065: public void invalidate() {
066: if (session == null) {
067: return;
068: }
069:
070: synchronized (session) {
071: session.invalidate();
072: session = null;
073: entries = null;
074: }
075: }
076:
077: /**
078: * Removes all attributes from the session as well as clears entries in this
079: * map.
080: */
081: public void clear() {
082: if (session == null) {
083: return;
084: }
085:
086: synchronized (session) {
087: entries = null;
088: Enumeration<String> attributeNamesEnum = session
089: .getAttributeNames();
090: while (attributeNamesEnum.hasMoreElements()) {
091: session.removeAttribute(attributeNamesEnum
092: .nextElement());
093: }
094: }
095:
096: }
097:
098: /**
099: * 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()
116: .toString();
117: final Object value = session.getAttribute(key);
118: entries.add(new Map.Entry() {
119: public boolean equals(Object obj) {
120: Map.Entry entry = (Map.Entry) obj;
121:
122: return ((key == null) ? (entry.getKey() == null)
123: : key.equals(entry.getKey()))
124: && ((value == null) ? (entry
125: .getValue() == null)
126: : value.equals(entry
127: .getValue()));
128: }
129:
130: public int hashCode() {
131: return ((key == null) ? 0 : key.hashCode())
132: ^ ((value == null) ? 0 : value
133: .hashCode());
134: }
135:
136: public Object getKey() {
137: return key;
138: }
139:
140: public Object getValue() {
141: return value;
142: }
143:
144: public Object setValue(Object obj) {
145: session.setAttribute(key.toString(), obj);
146:
147: return value;
148: }
149: });
150: }
151: }
152: }
153:
154: return entries;
155: }
156:
157: /**
158: * Returns the session attribute associated with the given key or <tt>null</tt> if it doesn't exist.
159: *
160: * @param key the name of the session attribute.
161: * @return the session attribute or <tt>null</tt> if it doesn't exist.
162: */
163: public Object get(Object key) {
164: if (session == null) {
165: return null;
166: }
167:
168: synchronized (session) {
169: return session.getAttribute(key.toString());
170: }
171: }
172:
173: /**
174: * Saves an attribute in the session.
175: *
176: * @param key the name of the session attribute.
177: * @param value the value to set.
178: * @return the object that was just set.
179: */
180: public Object put(Object key, Object value) {
181: synchronized (this ) {
182: if (session == null) {
183: session = request.getSession(true);
184: }
185: }
186:
187: synchronized (session) {
188: entries = null;
189: session.setAttribute(key.toString(), value);
190:
191: return get(key);
192: }
193: }
194:
195: /**
196: * Removes the specified session attribute.
197: *
198: * @param key the name of the attribute to remove.
199: * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
200: */
201: public Object remove(Object key) {
202: if (session == null) {
203: return null;
204: }
205:
206: synchronized (session) {
207: entries = null;
208:
209: Object value = get(key);
210: session.removeAttribute(key.toString());
211:
212: return value;
213: }
214: }
215: }
|