Quadcap Embeddable Server

com/quadcap/http/server22/HSession.java

Go to the documentation of this file.
00001 package com.quadcap.http.server22; 00002 00003 /* Copyright 1998 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.Date; 00042 import java.util.Enumeration; 00043 import java.util.Hashtable; 00044 import java.util.Vector; 00045 00046 import javax.servlet.http.HttpSession; 00047 import javax.servlet.http.HttpSessionBindingEvent; 00048 import javax.servlet.http.HttpSessionBindingListener; 00049 import javax.servlet.http.HttpSessionContext; 00050 00051 import com.quadcap.util.Debug; 00052 00053 /** 00054 * This class implements the <code>javax.servlet.http.HttpSession</code> interface. 00055 * 00056 * @author Stan Bailes 00057 */ 00058 public class HSession implements HttpSession { 00059 WebApplication app; 00060 String sessionId; 00061 boolean valid = true; 00062 long creationTime = new Date().getTime(); 00063 long lastAccessTime = creationTime; 00064 int maxInactiveInterval; 00065 Hashtable appData = new Hashtable(); 00066 boolean newSession = true; 00067 00068 /** 00069 * Construct a new session object. 00070 * 00071 * @param app the server context (session context) owning this session. 00072 * @param sessionId this session's id. 00073 */ 00074 public HSession(WebApplication app, String sessionId, 00075 int maxInactiveInterval) 00076 { 00077 this.app = app; 00078 this.sessionId = sessionId; 00079 this.maxInactiveInterval = maxInactiveInterval; 00080 } 00081 00082 /** 00083 * Returns the identifier assigned to this session. An HttpSession's 00084 * identifier is a unique string that is created and maintained by 00085 * HttpSessionContext. 00086 * 00087 * @return the identifier assigned to this session 00088 * @exception IllegalStateException if an attempt is made to access 00089 * session data after the session has been invalidated 00090 */ 00091 public String getId() { 00092 if (!valid) throw new IllegalStateException("session not valid"); 00093 return sessionId; 00094 } 00095 00096 /** 00097 * Returns the context in which this session is bound. 00098 * 00099 * @return the name of the context in which this session is bound 00100 * @exception IllegalStateException if an attempt is made to access 00101 * session data after the session has been invalidated 00102 * @deprecated in 2.2 00103 */ 00104 public HttpSessionContext getSessionContext() { 00105 return new HttpSessionContext() { 00106 public Enumeration getIds() { return new Vector().elements(); } 00107 public HttpSession getSession(String s) { return null; } 00108 }; 00109 } 00110 00111 /** 00112 * Returns the time at which this session representation was created, 00113 * in milliseconds since midnight, January 1, 1970 UTC. 00114 * 00115 * @return the time when the session was created 00116 * @exception IllegalStateException if an attempt is made to access 00117 * session data after the session has been invalidated 00118 */ 00119 public long getCreationTime() { 00120 if (!valid) throw new IllegalStateException("session not valid"); 00121 return creationTime; 00122 } 00123 00124 /** 00125 * Returns the last time the client sent a request carrying the identifier 00126 * assigned to the session. Time is expressed 00127 * as milliseconds since midnight, January 1, 00128 * 1970 UTC. 00129 * Application level operations, such as getting or setting a value 00130 * associated with the session, does not affect the access time. 00131 * 00132 * <P> This information is particularly useful in session management 00133 * policies. For example, 00134 * <UL> 00135 * <LI>a session manager could leave all sessions 00136 * which have not been used in a long time 00137 * in a given context. 00138 * <LI>the sessions can be sorted according to age to optimize some task. 00139 * </UL> 00140 * 00141 * @return the last time the client sent a request carrying the identifier 00142 * assigned to the session 00143 * @exception IllegalStateException if an attempt is made to access 00144 * session data after the session has been invalidated 00145 */ 00146 public long getLastAccessedTime() { 00147 if (!valid) throw new IllegalStateException("session not valid"); 00148 return lastAccessTime; 00149 } 00150 00151 /** 00152 * Causes this representation of the session to be invalidated and removed 00153 * from its context. 00154 * 00155 * @exception IllegalStateException if an attempt is made to access 00156 * session data after the session has been invalidated 00157 */ 00158 public void invalidate() { 00159 if (!valid) throw new IllegalStateException("session not valid"); 00160 Enumeration e = appData.keys(); 00161 while (e.hasMoreElements()) { 00162 String key = e.nextElement().toString(); 00163 removeValue(key); 00164 } 00165 app.removeSession(this); 00166 valid = false; 00167 } 00168 00169 /** 00170 * Binds the specified object into the session's application layer data 00171 * with the given name. Any existing binding with the same name is 00172 * replaced. New (or existing) values that implement the 00173 * HttpSessionBindingListener interface will call its 00174 * valueBound() method. 00175 * 00176 * @param name the name to which the data object will be bound. This 00177 * parameter cannot be null. 00178 * @param value the data object to be bound. 00179 * This parameter cannot be null. 00180 * @exception IllegalStateException if an attempt is made to access 00181 * session data after the session has been invalidated. 00182 */ 00183 public void setAttribute(String name, Object value) { 00184 if (!valid) throw new IllegalStateException("session not valid"); 00185 appData.put(name, value); 00186 if (value instanceof HttpSessionBindingListener) { 00187 HttpSessionBindingEvent event = 00188 new HttpSessionBindingEvent(this, name); 00189 HttpSessionBindingListener listener = 00190 (HttpSessionBindingListener)value; 00191 listener.valueBound(event); 00192 } 00193 } 00194 00195 /** 00196 * Returns the object bound to the given name in the session's 00197 * application layer data. Returns null if there is no such binding. 00198 * 00199 * @param name the name of the binding to find 00200 * @return the value bound to that name, or null if the binding does 00201 * not exist. 00202 * @exception IllegalStateException if an attempt is made to access 00203 * HttpSession's session data after it has been invalidated 00204 */ 00205 public Object getAttribute(String name) { 00206 if (!valid) throw new IllegalStateException("session not valid"); 00207 return appData.get(name); 00208 } 00209 00210 /** 00211 * Removes the object bound to the given name in the session's 00212 * application layer data. Does nothing if there is no object 00213 * bound to the given name. The value that implements the 00214 * HttpSessionBindingListener interface will call its 00215 * valueUnbound() method. 00216 * 00217 * @param name the name of the object to remove 00218 * @exception IllegalStateException if an attempt is made to access 00219 * session data after the session has been invalidated 00220 */ 00221 public void removeAttribute(String name) { 00222 if (!valid) throw new IllegalStateException("session not valid"); 00223 Object value = appData.get(name); 00224 if (value != null) { 00225 appData.remove(name); 00226 if (value instanceof HttpSessionBindingListener) { 00227 HttpSessionBindingEvent event = 00228 new HttpSessionBindingEvent(this, name); 00229 HttpSessionBindingListener listener = 00230 (HttpSessionBindingListener)value; 00231 listener.valueUnbound(event); 00232 } 00233 } 00234 } 00235 00236 /** 00237 * Return an enumeration of the names of objects in the session's 00238 * application layer data. 00239 * 00240 * @return an enumeration of names 00241 */ 00242 public Enumeration getAttributeNames() { 00243 if (!valid) throw new IllegalStateException("session not valid"); 00244 return appData.keys(); 00245 } 00246 00247 /** 00248 * Returns an array of the names of all the application layer 00249 * data objects bound into the session. For example, if you want to delete 00250 * all of the data objects bound into the session, use this method to 00251 * obtain their names. 00252 * 00253 * @return an array containing the names of all of the application layer 00254 * data objects bound into the session 00255 * @exception IllegalStateException if an attempt is made to access 00256 * session data after the session has been invalidated 00257 * @deprecated 00258 */ 00259 public String[] getValueNames() { 00260 if (!valid) throw new IllegalStateException("session not valid"); 00261 String[] names = new String[appData.size()]; 00262 Enumeration e = appData.keys(); 00263 int i = 0; 00264 while (e.hasMoreElements()) { 00265 names[i++] = (String)e.nextElement(); 00266 } 00267 return names; 00268 } 00269 00270 /** 00271 * @deprecated 00272 */ 00273 public Object getValue(String name) { 00274 return getAttribute(name); 00275 } 00276 00277 /** 00278 * @deprecated 00279 */ 00280 public void putValue(String name, Object value) { 00281 setAttribute(name, value); 00282 } 00283 00284 /** 00285 * @deprecated 00286 */ 00287 public void removeValue(String name) { 00288 removeAttribute(name); 00289 } 00290 00291 /** 00292 * A session is considered to be "new" if it has been created by the 00293 * server, but the client has not yet acknowledged joining the session. 00294 * For example, 00295 * if the server supported only cookie-based sessions and the client had 00296 * completely disabled the use of cookies, then calls to 00297 * HttpServletRequest.getSession() would 00298 * always return "new" sessions. 00299 * 00300 * @return true if the session has been created by the server but the 00301 * client has not yet acknowledged joining the session; false otherwise 00302 * @exception IllegalStateException if an attempt is made to access 00303 * session data after the session has been invalidated 00304 */ 00305 public boolean isNew() { 00306 if (!valid) throw new IllegalStateException("session not valid"); 00307 return newSession; 00308 } 00309 00310 public int getMaxInactiveInterval() { 00311 if (!valid) throw new IllegalStateException("session not valid"); 00312 return maxInactiveInterval; 00313 } 00314 public void setMaxInactiveInterval(int ivl) { 00315 if (!valid) throw new IllegalStateException("session not valid"); 00316 this.maxInactiveInterval = ivl; 00317 } 00318 00319 /** 00320 * ---- Package-private implementation ----- 00321 */ 00322 00323 boolean isValid() { return valid; } 00324 void setNotNew() { newSession = false; } 00325 void updateLastAccess() { lastAccessTime = new Date().getTime(); } 00326 }