1 23 24 package com.lutris.appserver.server.sessionEnhydra; 25 26 import java.io.Serializable ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Vector ; 30 31 import javax.servlet.ServletContext ; 32 import javax.servlet.http.HttpSession ; 33 import javax.servlet.http.HttpSessionBindingEvent ; 34 import javax.servlet.http.HttpSessionBindingListener ; 35 import javax.servlet.http.HttpSessionContext ; 36 37 import com.lutris.appserver.server.session.SessionData; 38 import com.lutris.appserver.server.session.SessionException; 39 import com.lutris.appserver.server.session.SessionManager; 40 import com.lutris.appserver.server.user.User; 41 42 54 public class BasicSession implements StandardSession, HttpSession , Serializable { 55 56 protected transient StandardSessionManager sessionManager = null; 58 59 protected User user = null; 60 private String sessionKey = null; 61 private long timeCreated; private long timeLastUsed; private long timeExpires; private long maxIdleTime; private long maxNoUserIdleTime; 67 70 public SessionData data; 71 72 private int refCount = 0; 74 75 78 public BasicSession() {} 79 80 89 protected BasicSession(StandardSessionManager sessionManager, 90 String sessionKey) { 91 this(sessionManager, sessionKey, new SessionData()); 92 } 93 94 105 protected BasicSession(StandardSessionManager sessionManager, 106 String sessionKey, SessionData sessionData) { 107 this.sessionManager = sessionManager; 108 this.sessionKey = sessionKey; 109 112 long now = System.currentTimeMillis(); 113 timeCreated = now; 114 timeLastUsed = now; 115 long lifeTime = sessionManager.getMaxSessionLifeTime(); 116 if (lifeTime > 0) { 117 timeExpires = now + (lifeTime * 1000); 120 } else { 121 timeExpires = 0; 123 } 124 maxIdleTime = sessionManager.getMaxSessionIdleTime() * 1000; 125 maxNoUserIdleTime = sessionManager.getMaxNoUserSessionIdleTime() * 1000; 126 this.data = sessionData; 127 initHttpSession(); 128 } 129 130 134 public void touch() { 135 timeLastUsed = System.currentTimeMillis(); 136 } 137 138 144 public User getUser() { 145 return user; 146 } 147 148 163 public void setUser(User sessionUser) throws SessionException { 164 if (user != null) { 165 sessionManager.unregisterUser(this); 166 } 167 user = sessionUser; 168 if (sessionUser != null) { 169 sessionManager.registerUser(this); 170 } 171 } 172 173 180 public void clearUser() throws SessionException { 181 if (user != null) { 182 sessionManager.unregisterUser(this); 183 user = null; 184 } 185 } 186 187 192 public String getSessionKey() { 193 return sessionKey; 194 } 195 196 201 public SessionManager getSessionManager() { 202 return sessionManager; 203 } 204 205 211 public SessionData getSessionData() { 212 return data; 213 } 214 215 221 public long getTimeCreated() { 222 return timeCreated; 223 } 224 225 231 public long getTimeLastUsed() { 232 return timeLastUsed; 233 } 234 235 243 public long getTimeExpires() { 244 return timeExpires; 245 } 246 247 255 public void setTimeExpires(long timeExpires) { 256 this.timeExpires = timeExpires; 257 } 258 259 266 public long getMaxIdleTime() { 267 return maxIdleTime; 268 } 269 270 278 public void setMaxIdleTime(long maxIdleTime) { 279 this.maxIdleTime = maxIdleTime; 280 } 281 282 290 public long getMaxNoUserIdleTime() { 291 return maxNoUserIdleTime; 292 } 293 294 302 public void setMaxNoUserIdleTime(long maxIdleTime) { 303 this.maxNoUserIdleTime = maxIdleTime; 304 } 305 306 314 public boolean isNew() { 315 checkValid(); 316 return (timeCreated == timeLastUsed); 317 } 318 319 325 public int getRefCount() { 326 return refCount; 327 } 328 329 335 public int incrementRefCount() { 336 refCount++; 337 return refCount; 338 } 339 340 346 public int decrementRefCount() { 347 if (refCount > 0) { 348 refCount--; 349 } 350 return refCount; 351 } 352 353 359 public String toString() { 360 StringBuffer str = new StringBuffer (); 361 str.append("{"); 362 str.append("sessionKey=" + sessionKey); 363 str.append(", "); 364 if (user != null) { 365 str.append("user=" + user.getName()); 366 } else { 367 str.append("user=" + user); 368 } 369 str.append(", "); 370 str.append("timeCreated=" + timeCreated); 371 str.append(", "); 372 str.append("timeLastUsed=" + timeLastUsed); 373 str.append(", "); 374 str.append("timeExpires=" + timeExpires); 375 str.append(", "); 376 str.append("maxIdleTime=" + maxIdleTime); 377 str.append(", "); 378 str.append("maxNoUserIdleTime=" + maxNoUserIdleTime); 379 str.append(", "); 380 str.append("sessionData=" + data); 381 str.append("}"); 382 return str.toString(); 383 } 384 385 386 387 public HttpSession getHttpSession() { 388 return this; 390 } 391 392 393 394 395 398 Hashtable attributes; 399 boolean valid; 400 402 private void initHttpSession() { 403 attributes = new Hashtable (); 404 valid = true; 405 } 407 412 public long getCreationTime() { 413 checkValid(); 414 return getTimeCreated(); 415 } 416 417 public String getId() { 418 return getSessionKey(); 419 } 420 421 public long getLastAccessedTime() { 422 checkValid(); 423 return getTimeLastUsed(); 424 } 425 426 public void setMaxInactiveInterval(int interval) { 427 checkValid(); 428 setMaxIdleTime(interval*1000); 430 } 431 432 433 public int getMaxInactiveInterval() { 434 checkValid(); 435 return (int)(getMaxIdleTime()/1000); 437 } 438 439 440 public HttpSessionContext getSessionContext() { 441 checkValid(); 442 return null; 444 } 445 446 public ServletContext getServletContext(){ 447 return null; 449 } 450 451 public Object getAttribute(String name) { 452 checkValid(); 453 Object o = attributes.get(name); 454 return attributes.get(name); 455 } 456 457 public Object getValue(String name) { 458 checkValid(); 459 return getAttribute(name); 460 } 461 462 463 public Enumeration getAttributeNames() { 464 checkValid(); 465 Hashtable attributesClone = (Hashtable )attributes.clone(); 466 return (Enumeration )attributesClone.keys(); 467 } 468 469 public String [] getValueNames() { 470 checkValid(); 471 Enumeration e = getAttributeNames(); 472 Vector names = new Vector (); 473 while (e.hasMoreElements()) { 474 names.addElement(e.nextElement()); 475 } 476 String [] valueNames = new String [names.size()]; 477 names.copyInto(valueNames); 478 return valueNames; 479 } 480 481 public void setAttribute(String name, Object value) { 482 checkValid(); 483 if (name == null) { 484 String msg = "null value"; 485 throw new IllegalArgumentException (msg); 486 } 487 removeAttribute(name); 488 if (value != null && value instanceof HttpSessionBindingListener ) { 489 HttpSessionBindingEvent e = new HttpSessionBindingEvent (this, name); 490 ((HttpSessionBindingListener )value).valueBound(e); 491 } 492 attributes.put(name,value); 493 } 494 495 public void putValue(String name, Object value) { 496 checkValid(); 497 setAttribute(name, value); 498 } 499 500 public void removeAttribute(String name) { 501 checkValid(); 502 if (name == null) { 503 String msg = "null value"; 504 throw new IllegalArgumentException (msg); 505 } 506 507 Object o = attributes.get(name); 508 if (o instanceof HttpSessionBindingListener ) { 509 HttpSessionBindingEvent e = new HttpSessionBindingEvent (this,name); 510 ((HttpSessionBindingListener )o).valueUnbound(e); 511 } 512 attributes.remove(name); 513 } 514 515 public void removeValue(String name) { 516 checkValid(); 517 removeAttribute(name); 518 } 519 520 public void invalidate() { 521 checkValid(); 522 Enumeration enumeration = attributes.keys(); 523 while (enumeration.hasMoreElements()) { 524 String name = (String )enumeration.nextElement(); 525 removeAttribute(name); 526 } 527 try { 529 sessionManager.deleteSession(this); 530 } 531 catch (SessionException se) { 532 se.printStackTrace(); 533 } 534 valid = false; 536 } 537 538 539 private void checkValid() { 540 if (! valid) { 541 String msg = "Session is invalid."; 542 throw new IllegalStateException (msg); 543 } 544 545 } 546 547 548 549 } 550 551 | Popular Tags |