001: package com.mockrunner.mock.web;
002:
003: import java.util.ArrayList;
004: import java.util.Enumeration;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.Vector;
010:
011: import javax.servlet.ServletContext;
012: import javax.servlet.http.HttpSession;
013: import javax.servlet.http.HttpSessionAttributeListener;
014: import javax.servlet.http.HttpSessionBindingEvent;
015: import javax.servlet.http.HttpSessionBindingListener;
016: import javax.servlet.http.HttpSessionContext;
017:
018: /**
019: * Mock implementation of <code>HttpSession</code>.
020: */
021: public class MockHttpSession implements HttpSession {
022: private HashMap attributes;
023: private String sessionId;
024: private boolean isNew;
025: private boolean isValid;
026: private long creationTime;
027: private ServletContext servletContext;
028: private int maxInactiveInterval;
029: private List attributeListener;
030:
031: public MockHttpSession() {
032: resetAll();
033: }
034:
035: /**
036: * Resets the state of this object to the default values
037: */
038: public synchronized void resetAll() {
039: attributes = new HashMap();
040: isValid = true;
041: creationTime = System.currentTimeMillis();
042: sessionId = new Double(Math.random()).toString();
043: maxInactiveInterval = -1;
044: attributeListener = new ArrayList();
045: }
046:
047: public synchronized void addAttributeListener(
048: HttpSessionAttributeListener listener) {
049: attributeListener.add(listener);
050: }
051:
052: /**
053: * Set the <code>ServletContext</code>.
054: * @param servletContext the <code>ServletContext</code>
055: */
056: public synchronized void setupServletContext(
057: ServletContext servletContext) {
058: this .servletContext = servletContext;
059: }
060:
061: public synchronized ServletContext getServletContext() {
062: return servletContext;
063: }
064:
065: public synchronized boolean isValid() {
066: return isValid;
067: }
068:
069: public synchronized boolean isNew() {
070: return isNew;
071: }
072:
073: public synchronized void setUpIsNew(boolean isNew) {
074: this .isNew = isNew;
075: }
076:
077: public synchronized long getCreationTime() {
078: return creationTime;
079: }
080:
081: public synchronized void invalidate() {
082: if (!isValid)
083: throw new IllegalStateException("session invalid");
084: isValid = false;
085: Map clone = new HashMap(attributes);
086: Iterator keys = clone.keySet().iterator();
087: while (keys.hasNext()) {
088: doRemoveAttribute((String) keys.next());
089: }
090: }
091:
092: public synchronized String getId() {
093: return sessionId;
094: }
095:
096: public synchronized Object getValue(String key) {
097: if (!isValid)
098: throw new IllegalStateException("session invalid");
099: return getAttribute(key);
100: }
101:
102: public synchronized String[] getValueNames() {
103: if (!isValid)
104: throw new IllegalStateException("session invalid");
105: Vector attKeys = new Vector(attributes.keySet());
106: return (String[]) attKeys.toArray();
107: }
108:
109: public synchronized void putValue(String key, Object value) {
110: if (!isValid)
111: throw new IllegalStateException("session invalid");
112: setAttribute(key, value);
113: }
114:
115: public synchronized void removeValue(String key) {
116: if (!isValid)
117: throw new IllegalStateException("session invalid");
118: removeAttribute(key);
119: }
120:
121: public synchronized void clearAttributes() {
122: attributes.clear();
123: }
124:
125: public synchronized Object getAttribute(String key) {
126: if (!isValid)
127: throw new IllegalStateException("session invalid");
128: return attributes.get(key);
129: }
130:
131: public synchronized Enumeration getAttributeNames() {
132: if (!isValid)
133: throw new IllegalStateException("session invalid");
134: Vector attKeys = new Vector(attributes.keySet());
135: return attKeys.elements();
136: }
137:
138: public synchronized void removeAttribute(String key) {
139: if (!isValid)
140: throw new IllegalStateException("session invalid");
141: doRemoveAttribute(key);
142: }
143:
144: private void doRemoveAttribute(String key) {
145: Object value = attributes.get(key);
146: attributes.remove(key);
147: if (null != value) {
148: callValueUnboundMethod(key, value);
149: callAttributeListenersRemovedMethod(key, value);
150: }
151: }
152:
153: public synchronized void setAttribute(String key, Object value) {
154: if (!isValid)
155: throw new IllegalStateException("session invalid");
156: Object oldValue = attributes.get(key);
157: if (null == value) {
158: attributes.remove(key);
159: } else {
160: attributes.put(key, value);
161: }
162: handleBindingListenerCalls(key, value, oldValue);
163: handleAttributeListenerCalls(key, value, oldValue);
164: }
165:
166: private synchronized void handleBindingListenerCalls(String key,
167: Object value, Object oldValue) {
168: if (oldValue != null) {
169: callValueUnboundMethod(key, oldValue);
170: }
171: if (value != null) {
172: callValueBoundMethod(key, value);
173: }
174: }
175:
176: private synchronized void handleAttributeListenerCalls(String key,
177: Object value, Object oldValue) {
178: if (null != oldValue) {
179: if (value != null) {
180: callAttributeListenersReplacedMethod(key, oldValue);
181: } else {
182: callAttributeListenersRemovedMethod(key, oldValue);
183: }
184: } else {
185: if (value != null) {
186: callAttributeListenersAddedMethod(key, value);
187: }
188:
189: }
190: }
191:
192: public synchronized long getLastAccessedTime() {
193: return System.currentTimeMillis();
194: }
195:
196: public synchronized void setMaxInactiveInterval(
197: int maxInactiveInterval) {
198: this .maxInactiveInterval = maxInactiveInterval;
199: }
200:
201: public synchronized int getMaxInactiveInterval() {
202: return maxInactiveInterval;
203: }
204:
205: public synchronized HttpSessionContext getSessionContext() {
206: return new MockSessionContext();
207: }
208:
209: private synchronized void callAttributeListenersAddedMethod(
210: String key, Object value) {
211: for (int ii = 0; ii < attributeListener.size(); ii++) {
212: HttpSessionBindingEvent event = new HttpSessionBindingEvent(
213: this , key, value);
214: ((HttpSessionAttributeListener) attributeListener.get(ii))
215: .attributeAdded(event);
216: }
217: }
218:
219: private synchronized void callAttributeListenersReplacedMethod(
220: String key, Object value) {
221: for (int ii = 0; ii < attributeListener.size(); ii++) {
222: HttpSessionBindingEvent event = new HttpSessionBindingEvent(
223: this , key, value);
224: ((HttpSessionAttributeListener) attributeListener.get(ii))
225: .attributeReplaced(event);
226: }
227: }
228:
229: private synchronized void callAttributeListenersRemovedMethod(
230: String key, Object value) {
231: for (int ii = 0; ii < attributeListener.size(); ii++) {
232: HttpSessionBindingEvent event = new HttpSessionBindingEvent(
233: this , key, value);
234: ((HttpSessionAttributeListener) attributeListener.get(ii))
235: .attributeRemoved(event);
236: }
237: }
238:
239: private synchronized void callValueBoundMethod(String key,
240: Object value) {
241: if (value instanceof HttpSessionBindingListener) {
242: HttpSessionBindingEvent event = new HttpSessionBindingEvent(
243: this , key, value);
244: ((HttpSessionBindingListener) value).valueBound(event);
245: }
246: }
247:
248: private synchronized void callValueUnboundMethod(String key,
249: Object value) {
250: if (value instanceof HttpSessionBindingListener) {
251: HttpSessionBindingEvent event = new HttpSessionBindingEvent(
252: this , key, value);
253: ((HttpSessionBindingListener) value).valueUnbound(event);
254: }
255: }
256: }
|