1 package com.quadcap.http.server22; 2 3 40 41 import java.util.Date ; 42 import java.util.Enumeration ; 43 import java.util.Hashtable ; 44 import java.util.Vector ; 45 46 import javax.servlet.http.HttpSession ; 47 import javax.servlet.http.HttpSessionBindingEvent ; 48 import javax.servlet.http.HttpSessionBindingListener ; 49 import javax.servlet.http.HttpSessionContext ; 50 51 import com.quadcap.util.Debug; 52 53 58 public class HSession implements HttpSession { 59 WebApplication app; 60 String sessionId; 61 boolean valid = true; 62 long creationTime = new Date ().getTime(); 63 long lastAccessTime = creationTime; 64 int maxInactiveInterval; 65 Hashtable appData = new Hashtable (); 66 boolean newSession = true; 67 68 74 public HSession(WebApplication app, String sessionId, 75 int maxInactiveInterval) 76 { 77 this.app = app; 78 this.sessionId = sessionId; 79 this.maxInactiveInterval = maxInactiveInterval; 80 } 81 82 91 public String getId() { 92 if (!valid) throw new IllegalStateException ("session not valid"); 93 return sessionId; 94 } 95 96 104 public HttpSessionContext getSessionContext() { 105 return new HttpSessionContext () { 106 public Enumeration getIds() { return new Vector ().elements(); } 107 public HttpSession getSession(String s) { return null; } 108 }; 109 } 110 111 119 public long getCreationTime() { 120 if (!valid) throw new IllegalStateException ("session not valid"); 121 return creationTime; 122 } 123 124 146 public long getLastAccessedTime() { 147 if (!valid) throw new IllegalStateException ("session not valid"); 148 return lastAccessTime; 149 } 150 151 158 public void invalidate() { 159 if (!valid) throw new IllegalStateException ("session not valid"); 160 Enumeration e = appData.keys(); 161 while (e.hasMoreElements()) { 162 String key = e.nextElement().toString(); 163 removeValue(key); 164 } 165 app.removeSession(this); 166 valid = false; 167 } 168 169 183 public void setAttribute(String name, Object value) { 184 if (!valid) throw new IllegalStateException ("session not valid"); 185 appData.put(name, value); 186 if (value instanceof HttpSessionBindingListener ) { 187 HttpSessionBindingEvent event = 188 new HttpSessionBindingEvent (this, name); 189 HttpSessionBindingListener listener = 190 (HttpSessionBindingListener )value; 191 listener.valueBound(event); 192 } 193 } 194 195 205 public Object getAttribute(String name) { 206 if (!valid) throw new IllegalStateException ("session not valid"); 207 return appData.get(name); 208 } 209 210 221 public void removeAttribute(String name) { 222 if (!valid) throw new IllegalStateException ("session not valid"); 223 Object value = appData.get(name); 224 if (value != null) { 225 appData.remove(name); 226 if (value instanceof HttpSessionBindingListener ) { 227 HttpSessionBindingEvent event = 228 new HttpSessionBindingEvent (this, name); 229 HttpSessionBindingListener listener = 230 (HttpSessionBindingListener )value; 231 listener.valueUnbound(event); 232 } 233 } 234 } 235 236 242 public Enumeration getAttributeNames() { 243 if (!valid) throw new IllegalStateException ("session not valid"); 244 return appData.keys(); 245 } 246 247 259 public String [] getValueNames() { 260 if (!valid) throw new IllegalStateException ("session not valid"); 261 String [] names = new String [appData.size()]; 262 Enumeration e = appData.keys(); 263 int i = 0; 264 while (e.hasMoreElements()) { 265 names[i++] = (String )e.nextElement(); 266 } 267 return names; 268 } 269 270 273 public Object getValue(String name) { 274 return getAttribute(name); 275 } 276 277 280 public void putValue(String name, Object value) { 281 setAttribute(name, value); 282 } 283 284 287 public void removeValue(String name) { 288 removeAttribute(name); 289 } 290 291 305 public boolean isNew() { 306 if (!valid) throw new IllegalStateException ("session not valid"); 307 return newSession; 308 } 309 310 public int getMaxInactiveInterval() { 311 if (!valid) throw new IllegalStateException ("session not valid"); 312 return maxInactiveInterval; 313 } 314 public void setMaxInactiveInterval(int ivl) { 315 if (!valid) throw new IllegalStateException ("session not valid"); 316 this.maxInactiveInterval = ivl; 317 } 318 319 322 323 boolean isValid() { return valid; } 324 void setNotNew() { newSession = false; } 325 void updateLastAccess() { lastAccessTime = new Date ().getTime(); } 326 } 327 | Popular Tags |