1 22 package org.jboss.web.tomcat.tc6.session; 23 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyChangeSupport ; 27 import java.io.IOException ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 32 import javax.management.MBeanServer ; 33 import javax.management.ObjectName ; 34 import javax.servlet.http.Cookie ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import org.apache.catalina.Container; 38 import org.apache.catalina.Context; 39 import org.apache.catalina.Engine; 40 import org.apache.catalina.Globals; 41 import org.apache.catalina.Lifecycle; 42 import org.apache.catalina.LifecycleException; 43 import org.apache.catalina.LifecycleListener; 44 import org.apache.catalina.Session; 45 import org.apache.catalina.connector.Connector; 46 import org.apache.catalina.connector.Response; 47 import org.apache.catalina.util.LifecycleSupport; 48 import org.jboss.logging.Logger; 49 import org.jboss.metadata.WebMetaData; 50 import org.jboss.metadata.web.ReplicationConfig; 51 import org.jboss.mx.util.MBeanServerLocator; 52 import org.jboss.web.tomcat.statistics.ReplicationStatistics; 53 54 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 55 56 64 public abstract class JBossManager 65 implements AbstractJBossManager, Lifecycle, 66 JBossManagerMBean, PropertyChangeListener 67 { 68 72 private static final String info_ = "JBossManager/1.0"; 73 74 protected ReplicationStatistics stats_ = new ReplicationStatistics(); 76 77 80 protected InvalidateSessionPolicy invalidateSessionPolicy_ = InvalidateSessionPolicy.SET_AND_NON_PRIMITIVE_GET; 81 84 protected ReplicationGranularity replicationGranularity_ = ReplicationGranularity.SESSION; 85 86 91 protected boolean passivationMode_ = false; 92 93 98 protected int passivationMinIdleTime_ = -1; 99 100 105 protected int passivationMaxIdleTime_ = -1; 106 107 110 protected LifecycleSupport lifecycle_ = new LifecycleSupport(this); 111 114 protected boolean started_ = false; 115 118 protected ObjectName objectName_; 119 122 protected Logger log_ = Logger.getLogger(this.getClass().getName()); 123 126 protected Container container_; 127 133 protected boolean distributable_ = true; 134 138 protected int maxInactiveInterval_ = 60; 139 142 protected int sessionIdLength_ = 16; 143 144 protected int maxActive_ = -1; 146 147 protected int createdCounter_ = 0; 149 150 protected int rejectedCounter_ = 0; 152 153 protected int activeCounter_ = 0; 155 156 protected int maxActiveCounter_ = 0; 158 159 protected int expiredCounter_ = 0; 161 162 protected long timeSinceLastReset_ = 0; 163 164 protected long processingTime_ = 0; 166 167 170 protected final Map sessions_ = new ConcurrentHashMap(); 171 172 175 protected boolean useLocalCache_ = false; 176 177 180 protected PropertyChangeSupport support_ = new PropertyChangeSupport (this); 181 182 protected SessionIDGenerator sessionIDGenerator_; 183 184 protected String jvmRoute_; 185 186 188 public JBossManager() 189 { 190 sessionIDGenerator_ = SessionIDGenerator.getInstance(); 191 192 } 193 194 public void init(String name, WebMetaData webMetaData, boolean useJK, boolean useLocalCache) 195 throws ClusteringNotSupportedException 196 { 197 ReplicationConfig rpc = webMetaData.getReplicationConfig(); 198 if (rpc != null) 199 { 200 replicationGranularity_ = ReplicationGranularity.fromString(rpc.getGranularity()); 201 invalidateSessionPolicy_ = InvalidateSessionPolicy.fromString(rpc.getTrigger()); 202 } 203 maxActive_ = webMetaData.getMaxActiveSessionsAllowed(); 204 passivationMode_ = webMetaData.getSessionPassivationMode(); 205 if (passivationMode_) 206 { 207 passivationMinIdleTime_ = webMetaData.getSessionPassivationMinIdleTime(); 208 passivationMaxIdleTime_ = webMetaData.getSessionPassivationMaxIdleTime(); 209 } 210 useLocalCache_ = useLocalCache; 211 log_.info("init(): replicationGranularity_ is " + replicationGranularity_ + 212 " and invaldateSessionPolicy is " + invalidateSessionPolicy_ + 213 " and maxActiveSessions allowed is " + maxActive_ + 214 " and passivationMode is " + passivationMode_); 215 216 try 217 { 218 objectName_ = new ObjectName ("jboss.web:service=ClusterManager,WebModule=" + name); 220 } 221 catch (Throwable e) 222 { 223 log_.error("Could not create ObjectName", e); 224 throw new ClusteringNotSupportedException(e.toString()); 225 } 226 } 227 228 public InvalidateSessionPolicy getInvalidateSessionPolicy() 229 { 230 return this.invalidateSessionPolicy_; 231 } 232 233 238 public Engine getEngine() 239 { 240 Engine e = null; 241 for (Container c = getContainer(); e == null && c != null; c = c.getParent()) 242 { 243 if (c != null && c instanceof Engine) 244 { 245 e = (Engine) c; 246 } 247 } 248 return e; 249 } 250 251 256 public String getJvmRoute() 257 { 258 if (jvmRoute_ == null) 259 { 260 Engine e = getEngine(); 261 jvmRoute_= (e == null ? null : e.getJvmRoute()); 262 } 263 return jvmRoute_; 264 } 265 266 271 protected String getNextId() 272 { 273 return sessionIDGenerator_.getSessionId(); 274 } 275 276 280 public ObjectName getObjectName() 281 { 282 return objectName_; 283 } 284 285 public boolean isUseLocalCache() 286 { 287 return useLocalCache_; 288 } 289 290 295 public void setSessionCookie(String sessionId) 296 { 297 HttpServletResponse response = SessionReplicationContext.getOriginalResponse(); 298 setNewSessionCookie(sessionId, response); 299 } 300 301 public void setNewSessionCookie(String sessionId, HttpServletResponse response) 302 { 303 if (response != null) 304 { 305 Context context = (Context) container_; 306 Connector connector = ((Response )response).getConnector(); 307 if (context.getCookies()) 308 { 309 Cookie newCookie = new Cookie (Globals.SESSION_COOKIE_NAME, sessionId); 311 if (log_.isDebugEnabled()) 312 { 313 log_.debug("Setting cookie with session id:" + sessionId + " & name:" + Globals.SESSION_COOKIE_NAME); 314 } 315 316 String contextPath = null; 317 if (!connector.getEmptySessionPath() && (context != null)) { 318 contextPath = context.getEncodedPath(); 319 } 320 321 if ((contextPath != null) && (contextPath.length() > 0)) { 322 newCookie.setPath(contextPath); 323 } else { 324 newCookie.setPath("/"); 325 } 326 327 if (connector.getSecure()) { 328 newCookie.setSecure(true); 329 } 330 331 response.addCookie(newCookie); 332 } 333 } 334 } 335 336 338 public int getMaxActiveAllowed() 340 { 341 return getMaxActive(); 342 } 343 344 public void setMaxActiveAllowed(int maxActive) 346 { 347 setMaxActive(maxActive); 348 } 349 350 public long getMaxActiveSessionCount() 351 { 352 return this.maxActiveCounter_; 353 } 354 355 public ReplicationStatistics getReplicationStatistics() 356 { 357 return stats_; 358 } 359 360 public void resetStats() 361 { 362 stats_.resetStats(); 363 activeCounter_ = 0; 364 maxActiveCounter_ = 0; 365 rejectedCounter_ = 0; 366 createdCounter_ = 0; 367 expiredCounter_ = 0; 368 processingTime_ = 0; 369 timeSinceLastReset_ = System.currentTimeMillis(); 370 } 371 372 public long timeInSecondsSinceLastReset() 373 { 374 return (System.currentTimeMillis() - timeSinceLastReset_) / (1000L); 375 } 376 377 public long getActiveSessionCount() 378 { 379 return getActiveSessions(); 380 } 381 382 public long getCreatedSessionCount() 383 { 384 return createdCounter_; 385 } 386 387 public long getExpiredSessionCount() 388 { 389 return expiredCounter_; 390 } 391 392 public long getRejectedSessionCount() 393 { 394 return rejectedCounter_; 395 } 396 397 public int getSessionMaxAliveTime() 398 { 399 return 0; 400 } 401 402 public void setSessionMaxAliveTime(int sessionMaxAliveTime) 403 { 404 } 405 406 public int getSessionAverageAliveTime() 407 { 408 return 0; 409 } 410 411 public void setSessionAverageAliveTime(int sessionAverageAliveTime) 412 { 413 } 414 415 public String reportReplicationStatistics() 416 { 417 StringBuffer tmp = new StringBuffer (); 418 HashMap copy = new HashMap (stats_.getStats()); 419 Iterator iter = copy.entrySet().iterator(); 420 tmp.append("<table><tr>"); 421 tmp.append("<th>sessionID</th>"); 422 tmp.append("<th>replicationCount</th>"); 423 tmp.append("<th>minPassivationTime</th>"); 424 tmp.append("<th>maxPassivationTime</th>"); 425 tmp.append("<th>totalPassivationTime</th>"); 426 tmp.append("<th>minReplicationTime</th>"); 427 tmp.append("<th>maxReplicationTime</th>"); 428 tmp.append("<th>totalReplicationlTime</th>"); 429 tmp.append("<th>loadCount</th>"); 430 tmp.append("<th>minLoadTime</th>"); 431 tmp.append("<th>maxLoadTime</th>"); 432 tmp.append("<th>totalLoadTime</th>"); 433 while (iter.hasNext()) 434 { 435 Map.Entry entry = (Map.Entry ) iter.next(); 436 ReplicationStatistics.TimeStatistic stat = (ReplicationStatistics.TimeStatistic) entry.getValue(); 437 if (stat != null) 438 { 439 tmp.append("<tr><td>"); 440 tmp.append(entry.getKey()); 441 tmp.append("</td><td>"); 442 tmp.append(stat.replicationCount); 443 tmp.append("</td><td>"); 444 tmp.append(stat.minPassivationTime); 445 tmp.append("</td><td>"); 446 tmp.append(stat.maxPassivationTime); 447 tmp.append("</td><td>"); 448 tmp.append(stat.totalPassivationTime); 449 tmp.append("</td><td>"); 450 tmp.append(stat.minReplicationTime); 451 tmp.append("</td><td>"); 452 tmp.append(stat.maxReplicationTime); 453 tmp.append("</td><td>"); 454 tmp.append(stat.totalReplicationlTime); 455 tmp.append("</td><td>"); 456 tmp.append(stat.loadCount); 457 tmp.append("</td><td>"); 458 tmp.append(stat.minLoadTime); 459 tmp.append("</td><td>"); 460 tmp.append(stat.maxLoadTime); 461 tmp.append("</td><td>"); 462 tmp.append(stat.totalLoadlTime); 463 tmp.append("</td></tr>"); 464 } 465 } 466 tmp.append("</table>"); 467 copy.clear(); 468 return tmp.toString(); 469 470 } 471 472 public String reportReplicationStatisticsCSV() 473 { 474 StringBuffer tmp = createCSVHeader(); 475 HashMap copy = new HashMap (stats_.getStats()); 476 Iterator iter = copy.entrySet().iterator(); 477 while (iter.hasNext()) 478 { 479 Map.Entry entry = (Map.Entry ) iter.next(); 480 ReplicationStatistics.TimeStatistic stat = (ReplicationStatistics.TimeStatistic) entry.getValue(); 481 if (stat != null) 482 { 483 tmp.append("\n"); 484 tmp.append(entry.getKey()); 485 tmp.append(","); 486 tmp.append(stat.replicationCount); 487 tmp.append(","); 488 tmp.append(stat.minPassivationTime); 489 tmp.append(","); 490 tmp.append(stat.maxPassivationTime); 491 tmp.append(","); 492 tmp.append(stat.totalPassivationTime); 493 tmp.append(","); 494 tmp.append(stat.minReplicationTime); 495 tmp.append(","); 496 tmp.append(stat.maxReplicationTime); 497 tmp.append(","); 498 tmp.append(stat.totalReplicationlTime); 499 tmp.append(","); 500 tmp.append(stat.loadCount); 501 tmp.append(","); 502 tmp.append(stat.minLoadTime); 503 tmp.append(","); 504 tmp.append(stat.maxLoadTime); 505 tmp.append(","); 506 tmp.append(stat.totalLoadlTime); 507 } 508 } 509 copy.clear(); 510 return tmp.toString(); 511 512 } 513 514 public String reportReplicationStatisticsCSV(String sessionId) 515 { 516 StringBuffer tmp = createCSVHeader(); 517 Map stats = stats_.getStats(); 518 ReplicationStatistics.TimeStatistic stat = 519 (ReplicationStatistics.TimeStatistic) stats.get(sessionId); 520 if (stat != null) 521 { 522 tmp.append("\n"); 523 tmp.append(sessionId); 524 tmp.append(","); 525 tmp.append(stat.replicationCount); 526 tmp.append(","); 527 tmp.append(stat.minPassivationTime); 528 tmp.append(","); 529 tmp.append(stat.maxPassivationTime); 530 tmp.append(","); 531 tmp.append(stat.totalPassivationTime); 532 tmp.append(","); 533 tmp.append(stat.minReplicationTime); 534 tmp.append(","); 535 tmp.append(stat.maxReplicationTime); 536 tmp.append(","); 537 tmp.append(stat.totalReplicationlTime); 538 tmp.append(","); 539 tmp.append(stat.loadCount); 540 tmp.append(","); 541 tmp.append(stat.minLoadTime); 542 tmp.append(","); 543 tmp.append(stat.maxLoadTime); 544 tmp.append(","); 545 tmp.append(stat.totalLoadlTime); 546 } 547 return tmp.toString(); 548 } 549 550 private StringBuffer createCSVHeader() 551 { 552 StringBuffer tmp = new StringBuffer (); 553 tmp.append("sessionID,"); 554 tmp.append("replicationCount,"); 555 tmp.append("minPassivationTime,"); 556 tmp.append("maxPassivationTime,"); 557 tmp.append("totalPassivationTime,"); 558 tmp.append("minReplicationTime,"); 559 tmp.append("maxReplicationTime,"); 560 tmp.append("totalReplicationlTime,"); 561 tmp.append("loadCount,"); 562 tmp.append("minLoadTime,"); 563 tmp.append("maxLoadTime,"); 564 tmp.append("totalLoadTime"); 565 566 return tmp; 567 } 568 569 571 public void addLifecycleListener(LifecycleListener listener) 572 { 573 lifecycle_.addLifecycleListener(listener); 574 } 575 576 public LifecycleListener[] findLifecycleListeners() 577 { 578 return lifecycle_.findLifecycleListeners(); 579 } 580 581 public void removeLifecycleListener(LifecycleListener listener) 582 { 583 lifecycle_.removeLifecycleListener(listener); 584 } 585 586 592 public void start() throws LifecycleException 593 { 594 startManager(); 595 } 596 597 603 public void stop() throws LifecycleException 604 { 605 resetStats(); 606 stopManager(); 607 } 608 609 620 protected void startManager() throws LifecycleException 621 { 622 log_.info("Starting JBossManager"); 623 624 if (started_) 626 throw new LifecycleException 627 ("JBossManager alreadyStarted"); 628 lifecycle_.fireLifecycleEvent(START_EVENT, null); 629 started_ = true; 630 631 try 633 { 634 MBeanServer server = MBeanServerLocator.locateJBoss(); 635 server.registerMBean(this, objectName_); 636 } 637 catch (Exception e) 638 { 639 log_.error("Could not register ClusterManagerMBean to MBeanServer", e); 640 } 641 } 642 643 653 protected void stopManager() throws LifecycleException 654 { 655 log_.info("Stopping JBossManager"); 656 657 if (!started_) 659 throw new LifecycleException 660 ("JBossManager notStarted"); 661 lifecycle_.fireLifecycleEvent(STOP_EVENT, null); 662 started_ = false; 663 664 try 666 { 667 MBeanServer server = MBeanServerLocator.locateJBoss(); 668 server.unregisterMBean(objectName_); 669 } 670 catch (Exception e) 671 { 672 log_.error("Could not unregister ClusterManagerMBean from MBeanServer", e); 673 } 674 } 675 676 public Container getContainer() 678 { 679 return container_; 680 } 681 682 public void setContainer(Container container) 683 { 684 685 if ((this.container_ != null) && (this.container_ instanceof Context)) 687 this.container_.removePropertyChangeListener(this); 688 689 this.container_ = container; 691 692 if ((this.container_ != null) && (this.container_ instanceof Context)) 694 { 695 setMaxInactiveInterval 696 (((Context) this.container_).getSessionTimeout() * 60); 697 this.container_.addPropertyChangeListener(this); 698 } 699 } 700 701 public boolean getDistributable() 702 { 703 return distributable_; 704 } 705 706 public void setDistributable(boolean distributable) 707 { 708 this.distributable_ = distributable; 709 } 710 711 public String getInfo() 712 { 713 return info_; 714 } 715 716 public int getMaxInactiveInterval() 717 { 718 return maxInactiveInterval_; 719 } 720 721 public void setMaxInactiveInterval(int interval) 722 { 723 this.maxInactiveInterval_ = interval; 724 } 725 726 public int getSessionIdLength() 727 { 728 return sessionIdLength_; 729 } 730 731 public void setSessionIdLength(int idLength) 732 { 733 this.sessionIdLength_ = idLength; 734 } 735 736 public int getSessionCounter() 737 { 738 return createdCounter_; 739 } 740 741 public void setSessionCounter(int sessionCounter) 742 { 743 this.createdCounter_ = sessionCounter; 744 } 745 746 public int getMaxActive() 747 { 748 return maxActive_; 749 } 750 751 public void setMaxActive(int maxActive) 752 { 753 this.maxActive_ = maxActive; 754 } 755 756 public int getExpiredSessions() 757 { 758 return expiredCounter_; 759 } 760 761 public void setExpiredSessions(int expiredSessions) 762 { 763 this.expiredCounter_ = expiredSessions; 764 } 765 766 public int getRejectedSessions() 767 { 768 return rejectedCounter_; 769 } 770 771 public void setRejectedSessions(int rejectedSessions) 772 { 773 this.rejectedCounter_ = rejectedSessions; 774 } 775 776 public long getProcessingTime() 777 { 778 return this.processingTime_; 779 } 780 781 public void addPropertyChangeListener(PropertyChangeListener listener) 782 { 783 support_.addPropertyChangeListener(listener); 784 } 785 786 792 public abstract void removeLocal(Session session); 793 794 799 public abstract boolean storeSession(Session session); 800 801 public int getActiveSessions() 802 { 803 return activeCounter_; 804 } 805 806 837 838 public void load() throws ClassNotFoundException , IOException 839 { 840 throw new RuntimeException ("JBossManager.load(): Method not implemented."); 842 } 843 844 public void removePropertyChangeListener(PropertyChangeListener listener) 845 { 846 support_.removePropertyChangeListener(listener); 847 } 848 849 public void unload() throws IOException 850 { 851 throw new RuntimeException ("JBossManager.load(): Method not implemented."); 853 } 854 855 856 public void backgroundProcess() 857 { 858 860 long start = System.currentTimeMillis(); 861 862 processExpires(); 863 864 long elapsed = System.currentTimeMillis() - start; 865 866 processingTime_ += elapsed; 867 } 868 869 872 protected void processExpires() 873 { 874 877 Session sessions[] = findSessions(); 879 if (log_.isDebugEnabled()) 880 { 881 log_.debug("Looking for sessions that have expired ..."); 882 } 883 884 for (int i = 0; i < sessions.length; ++i) 885 { 886 ClusteredSession session = (ClusteredSession) sessions[i]; 887 888 if (!session.isValid()) 890 { 891 continue; 892 } 893 894 923 } 924 } 925 926 public void propertyChange(PropertyChangeEvent evt) 927 { 928 } 930 931 935 abstract public ClusteredSession[] findLocalSessions(); 936 937 942 abstract public ClusteredSession findLocalSession(String realId); 943 944 } 945 | Popular Tags |