001: /*
002: * @(#)SessionMap.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.servlet;
010:
011: import java.util.*;
012: import javax.servlet.http.*;
013:
014: /**
015: * Adapter from HttpSession to java.util.Map
016: */
017: class SessionMap implements Map {
018:
019: private HttpSession session;
020:
021: public SessionMap(HttpSession session) {
022: this .session = session;
023: }
024:
025: /**
026: * Gets the number of session attributes
027: */
028: public int size() {
029: if (session == null) {
030: return 0;
031: }
032: int count = 0;
033: for (Enumeration e = session.getAttributeNames(); e
034: .hasMoreElements();) {
035: count++;
036: }
037: return count;
038: }
039:
040: /**
041: * Checks if the session has any attribute
042: */
043: public boolean isEmpty() {
044: if (session == null) {
045: return true;
046: }
047: for (Enumeration e = session.getAttributeNames(); e
048: .hasMoreElements();) {
049: return false;
050: }
051: return true;
052: }
053:
054: /**
055: * Checks if the session has any attribute with the key
056: */
057: public boolean containsKey(Object key) {
058: if (session == null) {
059: return false;
060: }
061: return session.getAttribute((String) key) != null;
062: }
063:
064: /**
065: * Checks if the session has any attribute with the value
066: */
067: public boolean containsValue(Object value) {
068: if (session == null) {
069: return false;
070: }
071: for (Enumeration e = session.getAttributeNames(); e
072: .hasMoreElements();) {
073: if (value.equals(get(e.nextElement()))) {
074: return true;
075: }
076: }
077: return false;
078: }
079:
080: /**
081: * Gets the value of specified session attribute
082: */
083: public Object get(Object key) {
084: if (session == null) {
085: return null;
086: }
087: return session.getAttribute((String) key);
088: }
089:
090: /**
091: * Sets the value of specified session attribute
092: */
093: public Object put(Object key, Object value) {
094: if (session == null) {
095: return null;
096: }
097: session.setAttribute((String) key, value);
098: return null;
099: }
100:
101: /**
102: * Removes the specified session attribute
103: */
104: public Object remove(Object key) {
105: if (session == null) {
106: return null;
107: }
108: Object result = get(key);
109: session.removeAttribute((String) key);
110: return result;
111: }
112:
113: /**
114: * Sets a series of attributes to the session
115: */
116: public void putAll(Map t) {
117: if (session == null) {
118: return;
119: }
120: for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
121: Map.Entry entry = (Map.Entry) it.next();
122: put(entry.getKey(), entry.getValue());
123: }
124: }
125:
126: /**
127: * Removes all session attributes
128: */
129: public void clear() {
130: if (session == null) {
131: return;
132: }
133: for (Enumeration e = session.getAttributeNames(); e
134: .hasMoreElements();) {
135: remove((String) e.nextElement());
136: }
137: }
138:
139: public Set keySet() {
140: HashSet set = new HashSet();
141: if (session != null) {
142: for (Enumeration e = session.getAttributeNames(); e
143: .hasMoreElements();) {
144: set.add(e.nextElement());
145: }
146: }
147: return set;
148: }
149:
150: /**
151: * Collects the values of the session attributes and return them as a List
152: */
153: public Collection values() {
154: ArrayList list = new ArrayList();
155: if (session != null) {
156: for (Enumeration e = session.getAttributeNames(); e
157: .hasMoreElements();) {
158: list.add(get(e.nextElement()));
159: }
160: }
161: return list;
162: }
163:
164: /**
165: * Collects the session attributes and return them as a Set of Map.Entry objects.
166: */
167: public Set entrySet() {
168: HashSet set = new HashSet();
169: if (session != null) {
170: for (Enumeration e = session.getAttributeNames(); e
171: .hasMoreElements();) {
172: String name = (String) e.nextElement();
173: set.add(new Entry(name, get(name)));
174: }
175: }
176: return set;
177: }
178:
179: static class Entry implements Map.Entry {
180: String attr;
181: Object value;
182:
183: Entry(String attr, Object value) {
184: this .attr = attr;
185: this .value = value;
186: }
187:
188: public Object getKey() {
189: return attr;
190: }
191:
192: public Object getValue() {
193: return value;
194: }
195:
196: public Object setValue(Object value) {
197: Object r = this .value;
198: this .value = value;
199: return r;
200: }
201:
202: public int hashCode() {
203: return attr.hashCode();
204: }
205:
206: public boolean equals(Object that) {
207: if (that instanceof Entry) {
208: Entry e = (Entry) that;
209: return this .attr.equals(e.attr)
210: && this .value.equals(e.value);
211: }
212: return false;
213: }
214: }
215: }
|