001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpSession;
009: import java.io.Serializable;
010: import java.util.*;
011:
012: /**
013: * A simple implementation of the {@link java.util.Map} interface to handle a collection of HTTP session
014: * attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
015: * Note, this will occur lazily - only when the entry set is asked for.
016: *
017: * @author <a href="mailto:rickard@middleware-company.com">Rickard �berg</a>
018: * @author Bill Lynch (docs)
019: * @author tm_jee
020: * @version $Date: 2006-09-16 19:38:35 +0200 (Sat, 16 Sep 2006) $ $Id: SessionMap.java 2723 2006-09-16 17:38:35Z tmjee $
021: */
022: public class SessionMap extends AbstractMap implements Serializable {
023:
024: protected HttpSession session;
025: protected Set entries;
026: protected HttpServletRequest request;
027:
028: /**
029: * Creates a new session map given a http servlet request. Note, ths enumeration of request
030: * attributes will occur when the map entries are asked for.
031: *
032: * @param request the http servlet request object.
033: */
034: public SessionMap(HttpServletRequest request) {
035: // note, holding on to this request and relying on lazy session initalization will not work
036: // if you are running your action invocation in a background task, such as using the
037: // "exec-and-wait" interceptor
038: this .request = request;
039: this .session = request.getSession(false);
040: }
041:
042: /**
043: * Invalidate the http session.
044: */
045: public void invalidate() {
046: if (session == null) {
047: return;
048: }
049:
050: synchronized (session) {
051: session.invalidate();
052: session = null;
053: entries = null;
054: }
055: }
056:
057: /**
058: * Removes all attributes from the session as well as clears entries in this map.
059: */
060: public void clear() {
061: if (session == null) {
062: return;
063: }
064: synchronized (session) {
065: entries = null;
066: Enumeration attributeNamesEnum = session
067: .getAttributeNames();
068: while (attributeNamesEnum.hasMoreElements()) {
069: session.removeAttribute((String) attributeNamesEnum
070: .nextElement());
071: }
072: }
073: }
074:
075: /**
076: * Returns a Set of attributes from the http session.
077: *
078: * @return a Set of attributes from the http session.
079: */
080: public Set entrySet() {
081: if (session == null) {
082: return Collections.EMPTY_SET;
083: }
084:
085: synchronized (session) {
086: if (entries == null) {
087: entries = new HashSet();
088:
089: Enumeration enumeration = session.getAttributeNames();
090:
091: while (enumeration.hasMoreElements()) {
092: final String key = enumeration.nextElement()
093: .toString();
094: final Object value = session.getAttribute(key);
095: entries.add(new Map.Entry() {
096: public boolean equals(Object obj) {
097: Map.Entry entry = (Map.Entry) obj;
098:
099: return ((key == null) ? (entry.getKey() == null)
100: : key.equals(entry.getKey()))
101: && ((value == null) ? (entry
102: .getValue() == null)
103: : value.equals(entry
104: .getValue()));
105: }
106:
107: public int hashCode() {
108: return ((key == null) ? 0 : key.hashCode())
109: ^ ((value == null) ? 0 : value
110: .hashCode());
111: }
112:
113: public Object getKey() {
114: return key;
115: }
116:
117: public Object getValue() {
118: return value;
119: }
120:
121: public Object setValue(Object obj) {
122: session.setAttribute(key.toString(), obj);
123:
124: return value;
125: }
126: });
127: }
128: }
129: }
130:
131: return entries;
132: }
133:
134: /**
135: * Returns the session attribute associated with the given key or <tt>null</tt> if it doesn't exist.
136: *
137: * @param key the name of the session attribute.
138: * @return the session attribute or <tt>null</tt> if it doesn't exist.
139: */
140: public Object get(Object key) {
141: if (session == null) {
142: return null;
143: }
144:
145: synchronized (session) {
146: return session.getAttribute(key.toString());
147: }
148: }
149:
150: /**
151: * Saves an attribute in the session.
152: *
153: * @param key the name of the session attribute.
154: * @param value the value to set.
155: * @return the object that was just set.
156: */
157: public Object put(Object key, Object value) {
158: synchronized (this ) {
159: if (session == null) {
160: session = request.getSession(true);
161: }
162: }
163:
164: synchronized (session) {
165: entries = null;
166: session.setAttribute(key.toString(), value);
167:
168: return get(key);
169: }
170: }
171:
172: /**
173: * Removes the specified session attribute.
174: *
175: * @param key the name of the attribute to remove.
176: * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
177: */
178: public Object remove(Object key) {
179: if (session == null) {
180: return null;
181: }
182:
183: synchronized (session) {
184: entries = null;
185:
186: Object value = get(key);
187: session.removeAttribute(key.toString());
188:
189: return value;
190: }
191: }
192: }
|