1 20 package org.enhydra.barracuda.plankton.data; 21 22 import java.io.*; 23 import java.lang.ref.*; 24 import java.util.*; 25 import javax.servlet.*; 26 import javax.servlet.http.*; 27 28 import org.apache.log4j.*; 29 30 62 public class ObjectRepository extends DefaultStateMap { 63 64 private static final Class CLASS = ObjectRepository.class; 66 private static final Logger logger = Logger.getLogger(CLASS); 67 68 70 protected static ObjectRepository global = new ThreadsafeRepository("GlobalOR"); 71 protected static ObjectRepository weakGlobal = new WeakThreadsafeRepository("WeakGlobalOR"); protected static ObjectRepository softGlobal = new SoftThreadsafeRepository("SoftGlobalOR"); protected static SoftHashMap rawSessions = new SoftHashMap(); protected static Map session = new HashMap(); 75 protected static Map sessionIDs = new HashMap(); protected static Map weaksession = new HashMap(); 77 protected static Map softsession = new HashMap(); protected static Map local = new HashMap(); 79 protected static Map custom = new HashMap(); 80 81 protected String name = "[unnamed]"; 82 83 84 87 protected ObjectRepository() { 88 this(null); 89 } 90 protected ObjectRepository(String iname) { 91 if (iname!=null) name = iname; 92 } 93 94 100 public static ObjectRepository getGlobalRepository() { 101 return global; 102 } 103 104 111 public static ObjectRepository getWeakGlobalRepository() { 112 return weakGlobal; 113 } 114 115 123 public static ObjectRepository getSoftGlobalRepository() { 124 return softGlobal; 125 } 126 127 134 public static void setupSessionRepository(HttpServletRequest req) { 135 String key = "SessionOR_"+Thread.currentThread().getName(); 136 if (logger.isInfoEnabled()) logger.info("Setting up session repository: "+key); 137 synchronized (session) { 138 session.put(key, new SessionRepository(req)); 139 } 140 } 141 142 147 public static ObjectRepository getSessionRepository() { 148 String key = "SessionOR_"+Thread.currentThread().getName(); 149 return getSessionRepository(key); 150 } 151 protected static ObjectRepository getSessionRepository(String key) { 152 if (logger.isInfoEnabled()) logger.info("Getting session repository: "+key); 153 ObjectRepository or = (ObjectRepository) session.get(key); 154 if (or==null) { 155 if (logger.isInfoEnabled()) logger.info("Setting up default session respository: "+key); 156 or = new ObjectRepository(key); 157 synchronized (session) { 158 session.put(key, or); 159 } 160 } 161 return or; 162 } 163 164 167 public static void removeSessionRepository() { 168 String key = "SessionOR_"+Thread.currentThread().getName(); 169 synchronized (session) { 170 SessionRepository sr = (SessionRepository) session.remove(key); 172 HttpServletRequest hsr = (sr!=null ? sr.req : null); 173 HttpSession hs = (hsr!=null ? hsr.getSession(false) : null); 174 175 if (hs!=null) { 178 try { 179 hs.getAttributeNames(); synchronized(rawSessions) { 181 rawSessions.put(hs.getId(), hs); 182 } 183 } catch (IllegalStateException e) { 184 } 187 synchronized(session) { 188 sessionIDs.remove(hs.getId()); 189 } 190 } 191 192 if (sr!=null) { 197 sr.req = null; 198 sr.map = null; 199 } 200 } 201 if (logger.isInfoEnabled()) logger.info("Removed session repository: "+key); 202 } 203 204 210 public static ObjectRepository getWeakSessionRepository() { 211 String key = "WeakSessionOR_$$"; 212 if (logger.isInfoEnabled()) logger.info("Getting weak session repository: "+key); 213 ObjectRepository session_or = getSessionRepository(); 214 ObjectRepository weak_or = (ObjectRepository) session_or.getState(key); 215 if (weak_or==null) { 216 if (logger.isInfoEnabled()) logger.info("Setting up weak session respository: "+key); 217 weak_or = new WeakRepository(key); 218 synchronized (session_or) { 219 session_or.putState(key, weak_or); 220 } 221 } 222 return weak_or; 223 } 224 225 231 public static ObjectRepository getSoftSessionRepository() { 232 String key = "SoftSessionOR_$$"; 233 if (logger.isInfoEnabled()) logger.info("Getting soft session repository: "+key); 234 ObjectRepository session_or = getSessionRepository(); 235 ObjectRepository soft_or = (ObjectRepository) session_or.getState(key); 236 if (soft_or==null) { 237 if (logger.isInfoEnabled()) logger.info("Setting up soft session respository: "+key); 238 soft_or = new SoftRepository(key); 239 synchronized (session_or) { 240 session_or.putState(key, soft_or); 241 } 242 } 243 return soft_or; 244 } 245 246 252 public static ObjectRepository getLocalRepository() { 253 String key = "LocalOR_"+Thread.currentThread().getName(); 254 if (logger.isInfoEnabled()) logger.info("Getting local repository: "+key); 255 ObjectRepository or = (ObjectRepository) local.get(key); 256 if (or==null) { 257 if (logger.isInfoEnabled()) logger.info("Creating new local repository: "+key); 258 or = new ObjectRepository(key); 259 local.put(key, or); 260 } 261 return or; 262 } 263 264 267 public static void removeLocalRepository() { 268 String key = "LocalOR_"+Thread.currentThread().getName(); 269 local.remove(key); 270 if (logger.isInfoEnabled()) logger.info("Removed local repository: "+key); 271 } 272 273 278 public static ObjectRepository getObjectRepository(NameSpace ns) { 279 if (logger.isInfoEnabled()) logger.info("Getting custom repository: "+ns); 280 ObjectRepository or = (ObjectRepository) custom.get(ns); 281 if (or==null) { 282 if (logger.isInfoEnabled()) logger.info("Creating new custom repository: "+ns); 283 or = new ObjectRepository(ns.getName()); 284 synchronized (custom) { 285 custom.put(ns, or); 286 } 287 } 288 return or; 289 } 290 291 294 public static void removeObjectRepository(NameSpace ns) { 295 synchronized (custom) { 296 custom.remove(ns); 297 } 298 if (logger.isInfoEnabled()) logger.info("Removed custom repository: "+ns); 299 } 300 301 306 public static ObjectRepository getObjectRepository(String name) { 307 if (logger.isInfoEnabled()) logger.info("Getting custom repository: "+name); 308 ObjectRepository or = (ObjectRepository) custom.get(name); 309 if (or==null) { 310 if (logger.isInfoEnabled()) logger.info("Creating new custom repository: "+name); 311 or = new ObjectRepository(name); 312 synchronized (custom) { 313 custom.put(name, or); 314 } 315 } 316 return or; 317 } 318 319 322 public static void removeObjectRepository(String name) { 323 synchronized (custom) { 324 custom.remove(name); 325 } 326 if (logger.isInfoEnabled()) logger.info("Removed custom repository: "+name); 327 } 328 329 332 public String getName() { 333 return name; 334 } 335 336 340 public static HttpSession getRawSession() { 341 HttpServletRequest req = ((SessionRepository) getSessionRepository()).req; 342 return (req!=null ? req.getSession() : null); 343 } 344 345 349 public static void invalidateSession() { 350 HttpServletRequest req = ((SessionRepository) getSessionRepository()).req; 351 HttpSession hs = (req!=null ? req.getSession(false) : null); 352 if (hs!=null) invalidateSession(hs.getId()); 353 } 354 355 359 public static void invalidateSession(String sessionID) { 360 synchronized (session) { 362 String key = (String ) sessionIDs.remove(sessionID); 363 if (key!=null) { 364 SessionRepository sr = (SessionRepository) session.remove(key); 365 if (sr!=null) { 366 sr.map=null; 367 sr.registered = false; 368 } 369 } 370 } 371 372 synchronized (rawSessions) { 374 HttpSession hs = (HttpSession) rawSessions.get(sessionID); 375 if (hs!=null) hs.invalidate(); 376 rawSessions.remove(sessionID); 377 } 378 } 379 380 385 public static void invalidateAllSessions() { 386 synchronized (session) { 388 session = new HashMap(); 389 sessionIDs = new HashMap(); 390 } 391 392 synchronized (rawSessions) { 394 Iterator it = rawSessions.values().iterator(); 395 while (it.hasNext()) { 396 HttpSession hs = (HttpSession) it.next(); 397 if (hs!=null) hs.invalidate(); 398 } 399 rawSessions = new SoftHashMap(); 400 } 401 } 402 403 411 public static Map getSessionStore() { 412 synchronized (rawSessions) { 414 Map smap = new HashMap(rawSessions.size()); 415 Iterator it = rawSessions.entrySet().iterator(); 416 while (it.hasNext()) { 417 Map.Entry me = (Map.Entry) it.next(); 418 Object key = me.getKey(); 419 Object val = me.getValue(); 420 if (key==null || val==null) continue; 421 if (key instanceof SoftReference) key = ((SoftReference) key).get(); 422 if (val instanceof SoftReference) val = ((SoftReference) val).get(); 423 smap.put(key, val); 424 } 425 return smap; 426 } 427 } 428 429 434 public static SoftHashMap getRawSessionStore() { 435 return rawSessions; 436 } 437 438 444 public static Map getObjectRepositoryStore() { 445 Map m = new TreeMap(); 447 m.put("OR1-global", global); 448 m.put("OR2-weak global", weakGlobal); 449 m.put("OR3-soft global", softGlobal); 450 m.put("OR4-rawSessions", getSessionStore()); 451 m.put("OR5-weak session", weaksession); 452 m.put("OR6-soft session", softsession); 453 m.put("OR7-local", local); 454 m.put("OR8-custom", custom); 455 456 return m; 457 } 458 459 public static void printStackTrace() { 460 printStackTrace(null); 461 } 462 public static void printStackTrace(String msg) { 463 printStackTrace(msg, System.out); 464 } 465 public static void printStackTrace(String msg, OutputStream out) { 466 try { 467 out.write(("\n\n\n\n\nObjectRepository StackTrace: "+(msg!=null ? "["+msg+"]" : "")+"\n").getBytes()); 468 CollectionsUtil.printStackTrace(getObjectRepositoryStore(), out); 469 out.write("\ndone.\n\n\n\n".getBytes()); 470 } catch (IOException e) { 471 } 472 } 473 474 477 static class ThreadsafeRepository extends ObjectRepository { 478 public ThreadsafeRepository() { 479 super(); 480 props = Collections.synchronizedMap(new HashMap()); 481 } 482 483 public ThreadsafeRepository(String iname) { 484 super(iname); 485 props = Collections.synchronizedMap(new HashMap()); 486 } 487 } 488 489 495 static class WeakRepository extends ObjectRepository { 496 public WeakRepository() { 497 super(); 498 props = new WeakHashMap(); 499 } 500 501 public WeakRepository(String iname) { 502 super(iname); 503 props = new WeakHashMap(); 504 } 505 } 506 507 510 static class WeakThreadsafeRepository extends WeakRepository { 511 public WeakThreadsafeRepository() { 512 super(); 513 props = Collections.synchronizedMap(props); 514 } 515 516 public WeakThreadsafeRepository(String iname) { 517 super(iname); 518 props = Collections.synchronizedMap(props); 519 } 520 } 521 522 528 static class SoftRepository extends ObjectRepository { 529 public SoftRepository() { 530 super(); 531 props = new SoftHashMap(); 532 } 533 534 public SoftRepository(String iname) { 535 super(iname); 536 props = new SoftHashMap(); 537 } 538 } 539 540 543 static class SoftThreadsafeRepository extends SoftRepository { 544 public SoftThreadsafeRepository() { 545 super(); 546 props = Collections.synchronizedMap(props); 547 } 548 549 public SoftThreadsafeRepository(String iname) { 550 super(iname); 551 props = Collections.synchronizedMap(props); 552 } 553 } 554 555 561 public static class SessionRepository extends ObjectRepository { 562 HttpServletRequest req = null; 563 HttpSessionStateMap map = null; 564 boolean registered = false; 565 566 public SessionRepository() {super();} 567 public SessionRepository(String iname) {super(iname);} 568 569 public SessionRepository(HttpServletRequest ireq) { 570 req = ireq; 571 registerSession(); 572 } 573 574 protected void registerSession() { 576 if (registered) return; 577 HttpSession hs = req.getSession(false); 578 if (hs!=null) { 579 try { 580 hs.getCreationTime(); 581 String key = "SessionOR_"+Thread.currentThread().getName(); 582 synchronized(rawSessions) { 583 if (!rawSessions.containsKey(hs.getId())) rawSessions.put(hs.getId(), hs); 584 } 585 synchronized(session) { 586 if (!sessionIDs.containsKey(hs.getId())) sessionIDs.put(hs.getId(), key); 587 registered = true; 588 } 589 } catch (IllegalStateException e) { 590 } 592 } 593 } 594 595 public synchronized void putState(Object key, Object val) { 596 if (map==null) { 597 HttpSession hs = req.getSession(); 598 map = new HttpSessionStateMap(hs); 599 registerSession(); 601 } 602 map.putState(key, val); 603 } 604 public synchronized Object getState(Object key) { 605 if (map==null) { 606 HttpSession hs = req.getSession(false); 607 if (hs==null) return null; 608 else map = new HttpSessionStateMap(hs); 609 } 610 return map.getState(key); 611 } 612 public synchronized Object removeState(Object key) { 613 if (map==null) { 614 HttpSession hs = req.getSession(false); 615 if (hs==null) return null; 616 else map = new HttpSessionStateMap(hs); 617 } 618 return map.removeState(key); 619 } 620 public synchronized List getStateKeys() { 621 if (map==null) { 622 HttpSession hs = req.getSession(false); 623 if (hs==null) return new ArrayList(); 624 else map = new HttpSessionStateMap(hs); 625 } 626 return map.getStateKeys(); 627 } 628 public synchronized Map getStateValues() { 629 if (map==null) { 630 HttpSession hs = req.getSession(false); 631 if (hs==null) return new HashMap(); 632 else map = new HttpSessionStateMap(hs); 633 } 634 return map.getStateValues(); 635 } 636 public synchronized void clearState() { 640 if (map==null) { 641 HttpSession hs = req.getSession(false); 642 if (hs==null) return; 643 else map = new HttpSessionStateMap(hs); 644 } 645 map.clearState(); 646 } 647 649 public HttpSession getSession() { 651 HttpSession hs = req.getSession(); 653 registerSession(); 654 return hs; 655 } 656 } 657 } | Popular Tags |