001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.web.servlet;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Enumeration;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026: import javax.servlet.http.HttpSession;
027: import javax.servlet.http.HttpServletRequest;
028: import org.apache.commons.chain.web.MapEntry;
029:
030: /**
031: * <p>Private implementation of <code>Map</code> for HTTP session
032: * attributes.</p>
033: *
034: * @author Craig R. McClanahan
035: * @version $Revision: 412789 $ $Date: 2006-06-08 17:19:14 +0100 (Thu, 08 Jun 2006) $
036: */
037:
038: final class ServletSessionScopeMap implements Map {
039:
040: public ServletSessionScopeMap(HttpServletRequest request) {
041: this .request = request;
042: sessionExists();
043: }
044:
045: private HttpSession session = null;
046: private HttpServletRequest request = null;
047:
048: public void clear() {
049: if (sessionExists()) {
050: Iterator keys = keySet().iterator();
051: while (keys.hasNext()) {
052: session.removeAttribute((String) keys.next());
053: }
054: }
055: }
056:
057: public boolean containsKey(Object key) {
058: if (sessionExists()) {
059: return (session.getAttribute(key(key)) != null);
060: } else {
061: return false;
062: }
063: }
064:
065: public boolean containsValue(Object value) {
066: if (value == null || !sessionExists()) {
067: return (false);
068: }
069: Enumeration keys = session.getAttributeNames();
070: while (keys.hasMoreElements()) {
071: Object next = session.getAttribute((String) keys
072: .nextElement());
073: if (next == value) {
074: return (true);
075: }
076: }
077: return (false);
078: }
079:
080: public Set entrySet() {
081: Set set = new HashSet();
082: if (sessionExists()) {
083: Enumeration keys = session.getAttributeNames();
084: String key;
085: while (keys.hasMoreElements()) {
086: key = (String) keys.nextElement();
087: set.add(new MapEntry(key, session.getAttribute(key),
088: true));
089: }
090: }
091: return (set);
092: }
093:
094: public boolean equals(Object o) {
095: if (sessionExists()) {
096: return (session.equals(o));
097: } else {
098: return false;
099: }
100: }
101:
102: public Object get(Object key) {
103: if (sessionExists()) {
104: return (session.getAttribute(key(key)));
105: } else {
106: return null;
107: }
108: }
109:
110: public int hashCode() {
111: if (sessionExists()) {
112: return (session.hashCode());
113: } else {
114: return 0;
115: }
116: }
117:
118: public boolean isEmpty() {
119: if (sessionExists()
120: && session.getAttributeNames().hasMoreElements()) {
121: return false;
122: } else {
123: return true;
124: }
125: }
126:
127: public Set keySet() {
128: Set set = new HashSet();
129: if (sessionExists()) {
130: Enumeration keys = session.getAttributeNames();
131: while (keys.hasMoreElements()) {
132: set.add(keys.nextElement());
133: }
134: }
135: return (set);
136: }
137:
138: public Object put(Object key, Object value) {
139: if (value == null) {
140: return (remove(key));
141: }
142:
143: // Ensure the Session is created, if it
144: // doesn't exist
145: if (session == null) {
146: session = request.getSession();
147: request = null;
148: }
149:
150: String skey = key(key);
151: Object previous = session.getAttribute(skey);
152: session.setAttribute(skey, value);
153: return (previous);
154: }
155:
156: public void putAll(Map map) {
157: Iterator keys = map.keySet().iterator();
158: while (keys.hasNext()) {
159: Object key = keys.next();
160: put(key, map.get(key));
161: }
162: }
163:
164: public Object remove(Object key) {
165: if (sessionExists()) {
166: String skey = key(key);
167: Object previous = session.getAttribute(skey);
168: session.removeAttribute(skey);
169: return (previous);
170: } else {
171: return (null);
172: }
173: }
174:
175: public int size() {
176: int n = 0;
177: if (sessionExists()) {
178: Enumeration keys = session.getAttributeNames();
179: while (keys.hasMoreElements()) {
180: keys.nextElement();
181: n++;
182: }
183: }
184: return (n);
185: }
186:
187: public Collection values() {
188: List list = new ArrayList();
189: if (sessionExists()) {
190: Enumeration keys = session.getAttributeNames();
191: while (keys.hasMoreElements()) {
192: list.add(session.getAttribute((String) keys
193: .nextElement()));
194: }
195: }
196: return (list);
197: }
198:
199: private String key(Object key) {
200: if (key == null) {
201: throw new IllegalArgumentException();
202: } else if (key instanceof String) {
203: return ((String) key);
204: } else {
205: return (key.toString());
206: }
207: }
208:
209: private boolean sessionExists() {
210: if (session == null) {
211: session = request.getSession(false);
212: if (session != null) {
213: request = null;
214: }
215: }
216: if (session != null) {
217: return true;
218: } else {
219: return false;
220: }
221: }
222:
223: }
|