001: package com.quadcap.http.server22;
002:
003: /* Copyright 1998 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Date;
042: import java.util.Enumeration;
043: import java.util.Hashtable;
044: import java.util.Vector;
045:
046: import javax.servlet.http.HttpSession;
047: import javax.servlet.http.HttpSessionBindingEvent;
048: import javax.servlet.http.HttpSessionBindingListener;
049: import javax.servlet.http.HttpSessionContext;
050:
051: import com.quadcap.util.Debug;
052:
053: /**
054: * This class implements the <code>javax.servlet.http.HttpSession</code> interface.
055: *
056: * @author Stan Bailes
057: */
058: public class HSession implements HttpSession {
059: WebApplication app;
060: String sessionId;
061: boolean valid = true;
062: long creationTime = new Date().getTime();
063: long lastAccessTime = creationTime;
064: int maxInactiveInterval;
065: Hashtable appData = new Hashtable();
066: boolean newSession = true;
067:
068: /**
069: * Construct a new session object.
070: *
071: * @param app the server context (session context) owning this session.
072: * @param sessionId this session's id.
073: */
074: public HSession(WebApplication app, String sessionId,
075: int maxInactiveInterval) {
076: this .app = app;
077: this .sessionId = sessionId;
078: this .maxInactiveInterval = maxInactiveInterval;
079: }
080:
081: /**
082: * Returns the identifier assigned to this session. An HttpSession's
083: * identifier is a unique string that is created and maintained by
084: * HttpSessionContext.
085: *
086: * @return the identifier assigned to this session
087: * @exception IllegalStateException if an attempt is made to access
088: * session data after the session has been invalidated
089: */
090: public String getId() {
091: if (!valid)
092: throw new IllegalStateException("session not valid");
093: return sessionId;
094: }
095:
096: /**
097: * Returns the context in which this session is bound.
098: *
099: * @return the name of the context in which this session is bound
100: * @exception IllegalStateException if an attempt is made to access
101: * session data after the session has been invalidated
102: * @deprecated in 2.2
103: */
104: public HttpSessionContext getSessionContext() {
105: return new HttpSessionContext() {
106: public Enumeration getIds() {
107: return new Vector().elements();
108: }
109:
110: public HttpSession getSession(String s) {
111: return null;
112: }
113: };
114: }
115:
116: /**
117: * Returns the time at which this session representation was created,
118: * in milliseconds since midnight, January 1, 1970 UTC.
119: *
120: * @return the time when the session was created
121: * @exception IllegalStateException if an attempt is made to access
122: * session data after the session has been invalidated
123: */
124: public long getCreationTime() {
125: if (!valid)
126: throw new IllegalStateException("session not valid");
127: return creationTime;
128: }
129:
130: /**
131: * Returns the last time the client sent a request carrying the identifier
132: * assigned to the session. Time is expressed
133: * as milliseconds since midnight, January 1,
134: * 1970 UTC.
135: * Application level operations, such as getting or setting a value
136: * associated with the session, does not affect the access time.
137: *
138: * <P> This information is particularly useful in session management
139: * policies. For example,
140: * <UL>
141: * <LI>a session manager could leave all sessions
142: * which have not been used in a long time
143: * in a given context.
144: * <LI>the sessions can be sorted according to age to optimize some task.
145: * </UL>
146: *
147: * @return the last time the client sent a request carrying the identifier
148: * assigned to the session
149: * @exception IllegalStateException if an attempt is made to access
150: * session data after the session has been invalidated
151: */
152: public long getLastAccessedTime() {
153: if (!valid)
154: throw new IllegalStateException("session not valid");
155: return lastAccessTime;
156: }
157:
158: /**
159: * Causes this representation of the session to be invalidated and removed
160: * from its context.
161: *
162: * @exception IllegalStateException if an attempt is made to access
163: * session data after the session has been invalidated
164: */
165: public void invalidate() {
166: if (!valid)
167: throw new IllegalStateException("session not valid");
168: Enumeration e = appData.keys();
169: while (e.hasMoreElements()) {
170: String key = e.nextElement().toString();
171: removeValue(key);
172: }
173: app.removeSession(this );
174: valid = false;
175: }
176:
177: /**
178: * Binds the specified object into the session's application layer data
179: * with the given name. Any existing binding with the same name is
180: * replaced. New (or existing) values that implement the
181: * HttpSessionBindingListener interface will call its
182: * valueBound() method.
183: *
184: * @param name the name to which the data object will be bound. This
185: * parameter cannot be null.
186: * @param value the data object to be bound.
187: * This parameter cannot be null.
188: * @exception IllegalStateException if an attempt is made to access
189: * session data after the session has been invalidated.
190: */
191: public void setAttribute(String name, Object value) {
192: if (!valid)
193: throw new IllegalStateException("session not valid");
194: appData.put(name, value);
195: if (value instanceof HttpSessionBindingListener) {
196: HttpSessionBindingEvent event = new HttpSessionBindingEvent(
197: this , name);
198: HttpSessionBindingListener listener = (HttpSessionBindingListener) value;
199: listener.valueBound(event);
200: }
201: }
202:
203: /**
204: * Returns the object bound to the given name in the session's
205: * application layer data. Returns null if there is no such binding.
206: *
207: * @param name the name of the binding to find
208: * @return the value bound to that name, or null if the binding does
209: * not exist.
210: * @exception IllegalStateException if an attempt is made to access
211: * HttpSession's session data after it has been invalidated
212: */
213: public Object getAttribute(String name) {
214: if (!valid)
215: throw new IllegalStateException("session not valid");
216: return appData.get(name);
217: }
218:
219: /**
220: * Removes the object bound to the given name in the session's
221: * application layer data. Does nothing if there is no object
222: * bound to the given name. The value that implements the
223: * HttpSessionBindingListener interface will call its
224: * valueUnbound() method.
225: *
226: * @param name the name of the object to remove
227: * @exception IllegalStateException if an attempt is made to access
228: * session data after the session has been invalidated
229: */
230: public void removeAttribute(String name) {
231: if (!valid)
232: throw new IllegalStateException("session not valid");
233: Object value = appData.get(name);
234: if (value != null) {
235: appData.remove(name);
236: if (value instanceof HttpSessionBindingListener) {
237: HttpSessionBindingEvent event = new HttpSessionBindingEvent(
238: this , name);
239: HttpSessionBindingListener listener = (HttpSessionBindingListener) value;
240: listener.valueUnbound(event);
241: }
242: }
243: }
244:
245: /**
246: * Return an enumeration of the names of objects in the session's
247: * application layer data.
248: *
249: * @return an enumeration of names
250: */
251: public Enumeration getAttributeNames() {
252: if (!valid)
253: throw new IllegalStateException("session not valid");
254: return appData.keys();
255: }
256:
257: /**
258: * Returns an array of the names of all the application layer
259: * data objects bound into the session. For example, if you want to delete
260: * all of the data objects bound into the session, use this method to
261: * obtain their names.
262: *
263: * @return an array containing the names of all of the application layer
264: * data objects bound into the session
265: * @exception IllegalStateException if an attempt is made to access
266: * session data after the session has been invalidated
267: * @deprecated
268: */
269: public String[] getValueNames() {
270: if (!valid)
271: throw new IllegalStateException("session not valid");
272: String[] names = new String[appData.size()];
273: Enumeration e = appData.keys();
274: int i = 0;
275: while (e.hasMoreElements()) {
276: names[i++] = (String) e.nextElement();
277: }
278: return names;
279: }
280:
281: /**
282: * @deprecated
283: */
284: public Object getValue(String name) {
285: return getAttribute(name);
286: }
287:
288: /**
289: * @deprecated
290: */
291: public void putValue(String name, Object value) {
292: setAttribute(name, value);
293: }
294:
295: /**
296: * @deprecated
297: */
298: public void removeValue(String name) {
299: removeAttribute(name);
300: }
301:
302: /**
303: * A session is considered to be "new" if it has been created by the
304: * server, but the client has not yet acknowledged joining the session.
305: * For example,
306: * if the server supported only cookie-based sessions and the client had
307: * completely disabled the use of cookies, then calls to
308: * HttpServletRequest.getSession() would
309: * always return "new" sessions.
310: *
311: * @return true if the session has been created by the server but the
312: * client has not yet acknowledged joining the session; false otherwise
313: * @exception IllegalStateException if an attempt is made to access
314: * session data after the session has been invalidated
315: */
316: public boolean isNew() {
317: if (!valid)
318: throw new IllegalStateException("session not valid");
319: return newSession;
320: }
321:
322: public int getMaxInactiveInterval() {
323: if (!valid)
324: throw new IllegalStateException("session not valid");
325: return maxInactiveInterval;
326: }
327:
328: public void setMaxInactiveInterval(int ivl) {
329: if (!valid)
330: throw new IllegalStateException("session not valid");
331: this .maxInactiveInterval = ivl;
332: }
333:
334: /**
335: * ---- Package-private implementation -----
336: */
337:
338: boolean isValid() {
339: return valid;
340: }
341:
342: void setNotNew() {
343: newSession = false;
344: }
345:
346: void updateLastAccess() {
347: lastAccessTime = new Date().getTime();
348: }
349: }
|