1 2 24 25 package com.lutris.appserver.server.sessionEnhydra; 26 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Vector ; 30 31 import com.lutris.appserver.server.Enhydra; 32 import com.lutris.appserver.server.session.SessionException; 33 import com.lutris.logging.Logger; 34 import com.lutris.util.Config; 35 import com.lutris.util.ConfigException; 36 37 63 public class BasicSessionHome implements StandardSessionHome { 64 65 69 private Hashtable activeCache = new Hashtable (); 70 71 76 private Hashtable activeThreadCache = new Hashtable (); 77 78 83 private Hashtable passiveCache = new Hashtable (); 84 85 88 private long maxSessions = -1; 89 90 93 private final String MAX_SESSIONS_KEY = "MaxSessions"; 94 private final long UNDEFINED_MAX_SESSIONS = -1; 95 96 99 private StandardSessionManager sessionMgr; 100 101 110 public BasicSessionHome(StandardSessionManager sessionMgr, 111 Config config) 112 throws ConfigException { 113 this.sessionMgr = sessionMgr; 114 if (config.containsKey(MAX_SESSIONS_KEY)) { 115 maxSessions = config.getLong(MAX_SESSIONS_KEY); 116 debug(MAX_SESSIONS_KEY + " = " + maxSessions); 117 } 118 if (maxSessions <= 0) { 119 maxSessions = UNDEFINED_MAX_SESSIONS; 120 } 121 } 122 123 134 public BasicSessionHome(StandardSessionManager sessionMgr, 135 Config config, ClassLoader loader) 136 throws ConfigException { 137 this(sessionMgr, config); 138 } 139 140 159 public synchronized StandardSession createSession(String sessionKey) 160 throws CreateSessionException, DuplicateKeyException, SessionException { 161 if (containsKey(sessionKey)) { 162 throw new DuplicateKeyException("Session key " + 163 sessionKey + " is already in use."); 164 } 165 if ((maxSessions != UNDEFINED_MAX_SESSIONS) && 166 (maxSessions <= size())) { 167 cleanupNewSession(); 168 if (maxSessions <= size()) { 169 throw new CreateSessionException("Maximum session limit (" 170 + maxSessions + 171 ") has been reached."); 172 } 173 } 174 BasicSession session = new BasicSession(sessionMgr, sessionKey); 175 SessionThread activeKey = new SessionThread(Thread.currentThread(), 176 sessionKey); 177 session.incrementRefCount(); 179 activeCache.put(sessionKey, session); 180 activeThreadCache.put(activeKey, session); 181 debug(3, "create session: key = " + sessionKey); 182 return (StandardSession)session; 183 } 184 185 201 public synchronized StandardSession getSession(String sessionKey) 202 throws SessionException { 203 debug(3, "get session: key = " + sessionKey); 204 SessionThread activeKey = new SessionThread(Thread.currentThread(), 205 sessionKey); 206 return (BasicSession)activeThreadCache.get(activeKey); 207 } 208 209 230 public synchronized StandardSession getSession(Thread thread, String sessionKey) 231 throws SessionException { 232 debug(3, "get session: key = " + sessionKey); 233 SessionThread activeKey = new SessionThread(thread, sessionKey); 234 BasicSession s = null; 235 if (passiveCache.containsKey(sessionKey)) { 237 s = (BasicSession)passiveCache.remove(sessionKey); 238 activeCache.put(sessionKey, s); 239 activeThreadCache.put(activeKey, s); 240 s.incrementRefCount(); 241 } else { 242 if (activeThreadCache.containsKey(activeKey)) { 244 s = (BasicSession)activeThreadCache.get(activeKey); 246 } else { 247 s = (BasicSession)activeCache.get(sessionKey); 249 if (s != null) { 250 activeThreadCache.put(activeKey, s); 251 s.incrementRefCount(); 254 } 255 } 256 } 257 return (StandardSession)s; 258 } 259 260 261 270 public synchronized void removeSession(String sessionKey) 271 throws SessionException { 272 debug(3, "remove session: key = " + sessionKey); 273 Enumeration e = activeThreadCache.keys(); 274 while (e.hasMoreElements()) { 275 SessionThread key = (SessionThread)e.nextElement(); 276 if (key.sessionKey.equals(sessionKey)) { 277 activeThreadCache.remove(key); 278 } 279 } 280 activeCache.remove(sessionKey); 281 passiveCache.remove(sessionKey); 282 } 283 284 297 public synchronized void passivateSession(Thread thread, String sessionKey) 298 throws SessionException { 299 SessionThread activeKey = new SessionThread(thread, sessionKey); 303 if (activeThreadCache.containsKey(activeKey)) { 304 BasicSession s = (BasicSession)activeThreadCache.remove(activeKey); 305 if (s.decrementRefCount() == 0) { 306 activeCache.remove(sessionKey); 307 passiveCache.put(sessionKey, s); 308 } 309 } 310 } 311 312 322 public boolean containsKey(String sessionKey) 323 throws SessionException { 324 return (activeCache.containsKey(sessionKey) 325 || passiveCache.containsKey(sessionKey)); 326 } 327 328 336 public int size() 337 throws SessionException { 338 return (activeCache.size() + passiveCache.size()); 339 } 340 341 350 public int pagedSize() throws SessionException { 351 return 0; 352 } 353 354 362 public synchronized Enumeration keys() throws SessionException { 363 Vector v = new Vector (); 364 Enumeration e; 365 e = activeCache.keys(); 366 while (e.hasMoreElements()) { 367 v.addElement(e.nextElement()); 368 } 369 e = passiveCache.keys(); 370 while (e.hasMoreElements()) { 371 v.addElement(e.nextElement()); 372 } 373 return v.elements(); 374 } 375 376 381 private void cleanupNewSession() throws SessionException { 382 Enumeration e = passiveCache.keys(); 383 StandardSession oldest = null; 384 while (e.hasMoreElements()) { 385 StandardSession s = (StandardSession)passiveCache.get(e.nextElement()); 386 if (s.isNew()) { 387 if ((oldest == null) 388 || (s.getTimeCreated() < oldest.getTimeCreated())) { 389 oldest = s; 390 } 391 } 392 } 393 if (oldest != null) { 394 removeSession(oldest.getSessionKey()); 395 } 396 } 397 398 401 public void shutdown() { 402 } 404 405 410 private void debug(String msg) { 411 debug(0, msg); 412 } 413 414 420 protected void debug(int level, String msg) { 421 int dbg = Logger.DEBUG; 422 switch (level) { 423 case 1: 424 dbg = Logger.DEBUG1; 425 break; 426 case 2: 427 dbg = Logger.DEBUG2; 428 break; 429 case 3: 430 dbg = Logger.DEBUG3; 431 break; 432 case 4: 433 dbg = Logger.DEBUG4; 434 break; 435 case 5: 436 dbg = Logger.DEBUG5; 437 break; 438 case 6: 439 dbg = Logger.DEBUG6; 440 break; 441 case 7: 442 dbg = Logger.DEBUG7; 443 break; 444 case 8: 445 dbg = Logger.DEBUG8; 446 break; 447 case 9: 448 dbg = Logger.DEBUG9; 449 break; 450 default: 451 dbg = Logger.DEBUG; 452 break; 453 } 454 Enhydra.getLogChannel().write(dbg, "PersistentSessionHome(" 455 + Thread.currentThread().getName() 456 + "): " + msg); 457 } 458 } 459 | Popular Tags |