001: /*
002: * Copyright 2005 Joe Walker
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.directwebremoting.util;
017:
018: import java.util.Collections;
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.servlet.ServletContext;
024: import javax.servlet.http.HttpSession;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: * For the benefit of anyone that wants to create a fake HttpSession
031: * that doesn't do anything other than not be null.
032: * @author Joe Walker [joe at getahead dot ltd dot uk]
033: */
034: public class FakeHttpSession implements HttpSession {
035: /**
036: * Setup the creation time
037: */
038: public FakeHttpSession() {
039: creationTime = System.currentTimeMillis();
040: }
041:
042: /**
043: * Setup the creation time
044: * @param id The new session id
045: */
046: public FakeHttpSession(String id) {
047: this .id = id;
048: creationTime = System.currentTimeMillis();
049: }
050:
051: /* (non-Javadoc)
052: * @see javax.servlet.http.HttpSession#getCreationTime()
053: */
054: public long getCreationTime() {
055: return creationTime;
056: }
057:
058: /* (non-Javadoc)
059: * @see javax.servlet.http.HttpSession#getId()
060: */
061: public String getId() {
062: if (id == null) {
063: log
064: .warn("Inventing data in FakeHttpSession.getId() to remain plausible.");
065: id = "fake";
066: }
067:
068: return id;
069: }
070:
071: /* (non-Javadoc)
072: * @see javax.servlet.http.HttpSession#getLastAccessedTime()
073: */
074: public long getLastAccessedTime() {
075: return creationTime;
076: }
077:
078: /* (non-Javadoc)
079: * @see javax.servlet.http.HttpSession#getServletContext()
080: */
081: public ServletContext getServletContext() {
082: return null;
083: }
084:
085: /* (non-Javadoc)
086: * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
087: */
088: public void setMaxInactiveInterval(int maxInactiveInterval) {
089: this .maxInactiveInterval = maxInactiveInterval;
090: }
091:
092: /* (non-Javadoc)
093: * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
094: */
095: public int getMaxInactiveInterval() {
096: return maxInactiveInterval;
097: }
098:
099: /**
100: * @see javax.servlet.http.HttpSession#getSessionContext()
101: * @deprecated
102: */
103: @SuppressWarnings({"UnnecessaryFullyQualifiedName"})
104: @Deprecated
105: public javax.servlet.http.HttpSessionContext getSessionContext() {
106: return null;
107: }
108:
109: /* (non-Javadoc)
110: * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
111: */
112: public Object getAttribute(String name) {
113: return attributes.get(name);
114: }
115:
116: /* (non-Javadoc)
117: * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
118: */
119: @Deprecated
120: public Object getValue(String name) {
121: return attributes.get(name);
122: }
123:
124: /* (non-Javadoc)
125: * @see javax.servlet.http.HttpSession#getAttributeNames()
126: */
127: public Enumeration<String> getAttributeNames() {
128: return Collections.enumeration(attributes.keySet());
129: }
130:
131: /* (non-Javadoc)
132: * @see javax.servlet.http.HttpSession#getValueNames()
133: */
134: @Deprecated
135: public String[] getValueNames() {
136: return attributes.keySet().toArray(
137: new String[attributes.keySet().size()]);
138: }
139:
140: /* (non-Javadoc)
141: * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String, java.lang.Object)
142: */
143: public void setAttribute(String name, Object value) {
144: attributes.put(name, value);
145: }
146:
147: /* (non-Javadoc)
148: * @see javax.servlet.http.HttpSession#putValue(java.lang.String, java.lang.Object)
149: */
150: @Deprecated
151: public void putValue(String name, Object value) {
152: attributes.put(name, value);
153: }
154:
155: /* (non-Javadoc)
156: * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
157: */
158: public void removeAttribute(String name) {
159: attributes.remove(name);
160: }
161:
162: /* (non-Javadoc)
163: * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
164: */
165: @Deprecated
166: public void removeValue(String name) {
167: attributes.remove(name);
168: }
169:
170: /* (non-Javadoc)
171: * @see javax.servlet.http.HttpSession#invalidate()
172: */
173: public void invalidate() {
174: }
175:
176: /* (non-Javadoc)
177: * @see javax.servlet.http.HttpSession#isNew()
178: */
179: public boolean isNew() {
180: return true;
181: }
182:
183: /**
184: * The session id
185: */
186: private String id = null;
187:
188: /**
189: * The list of attributes
190: */
191: private Map<String, Object> attributes = new HashMap<String, Object>();
192:
193: /**
194: * When were we created
195: */
196: private long creationTime;
197:
198: /**
199: * How long before we timeout?
200: */
201: private int maxInactiveInterval = 30 * 60 * 1000;
202:
203: /**
204: * The log stream
205: */
206: private static final Log log = LogFactory
207: .getLog(FakeHttpSession.class);
208: }
|