1 22 package org.jboss.web.tomcat.tc6.session; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 32 import javax.management.MBeanServer ; 33 import javax.management.MBeanServerFactory ; 34 import javax.management.ObjectName ; 35 36 import org.apache.catalina.Container; 37 import org.apache.catalina.Engine; 38 import org.apache.catalina.Host; 39 import org.apache.catalina.Lifecycle; 40 import org.apache.catalina.LifecycleException; 41 import org.apache.catalina.LifecycleListener; 42 import org.apache.catalina.Manager; 43 import org.apache.catalina.core.ContainerBase; 44 import org.apache.catalina.util.LifecycleSupport; 45 import org.apache.commons.logging.Log; 46 import org.apache.commons.logging.LogFactory; 47 import org.jboss.cache.BatchModeTransactionManagerLookup; 48 import org.jboss.cache.config.Configuration; 49 import org.jboss.cache.factories.XmlConfigurationParser; 50 import org.jboss.cache.pojo.PojoCacheFactory; 51 import org.jboss.cache.pojo.jmx.PojoCacheJmxWrapper; 52 import org.jboss.cache.pojo.jmx.PojoCacheJmxWrapperMBean; 53 import org.jboss.mx.util.MBeanProxyExt; 54 import org.jboss.mx.util.MBeanServerLocator; 55 56 67 public class JBossCacheCluster 68 implements JBossCacheClusterMBean, Lifecycle 69 { 70 72 protected static final String info = "JBossCacheCluster/2.1"; 73 74 public static Log log = LogFactory.getLog(JBossCacheCluster.class); 75 76 public static final String DEFAULT_CLUSTER_NAME = "Tomcat-Cluster"; 77 78 79 public static final String DEFAULT_ISOLATION_LEVEL = "REPEATABLE_READ"; 80 81 82 public static final String DEFAULT_CACHE_MODE = "REPL_ASYNC"; 83 84 85 public static final long DEFAULT_LOCK_TIMEOUT = 15000; 86 87 88 public static final String DEFAULT_TM_LOOKUP = 89 BatchModeTransactionManagerLookup.class.getName(); 90 91 public static final String DEFAULT_CACHE_CONFIG_PATH = "conf/cluster-cache.xml"; 92 93 95 96 private Container container = null; 97 98 99 private MBeanServer mserver = null; 100 101 102 private ObjectName objectName = null; 103 104 105 private boolean started = false; 106 107 108 private LifecycleSupport lifecycle = new LifecycleSupport(this); 109 110 111 private PojoCacheJmxWrapperMBean pojoCache = null; 112 113 114 private String pojoCacheObjectName = "jboss.cache:service=TomcatClusteringCache"; 115 116 117 private boolean pojoCacheLocal = false; 118 119 120 private String clusterName = null; 121 122 123 private String cacheConfigPath = null; 124 125 129 private String managerClassName = JBossCacheManager.class.getName(); 130 131 132 private boolean useJK = false; 133 134 135 private boolean useLocalCache = false; 136 137 141 private String defaultReplicationTrigger = null; 142 143 147 private String defaultReplicationGranularity = null; 148 149 152 private String snapshotMode = null; 153 154 157 private int snapshotInterval = 0; 158 159 160 private boolean replicationFieldBatchMode; 161 162 164 167 public JBossCacheCluster() 168 { 169 super(); 170 } 171 172 174 186 public String getCacheObjectName() 187 { 188 return pojoCacheObjectName; 189 } 190 191 198 public void setCacheObjectName(String objectName) 199 { 200 this.pojoCacheObjectName = objectName; 201 } 202 203 213 public void setClusterName(String clusterName) 214 { 215 this.clusterName = clusterName; 216 } 217 218 229 public String getCacheConfigPath() 230 { 231 return cacheConfigPath; 232 } 233 234 250 public void setCacheConfigPath(String cacheConfigPath) 251 { 252 this.cacheConfigPath = cacheConfigPath; 253 } 254 255 261 public String getManagerClassName() 262 { 263 return managerClassName; 264 } 265 266 276 public void setManagerClassName(String managerClassName) 277 { 278 this.managerClassName = managerClassName; 279 } 280 281 public void registerManager(Manager arg0) 282 { 283 } 285 286 public void removeManager(Manager arg0) 287 { 288 } 290 291 297 public boolean isUseJK() 298 { 299 return useJK; 300 } 301 302 308 public void setUseJK(boolean useJK) 309 { 310 this.useJK = useJK; 311 } 312 313 319 public boolean isUseLocalCache() 320 { 321 return useLocalCache; 322 } 323 324 330 public void setUseLocalCache(boolean useLocalCache) 331 { 332 this.useLocalCache = useLocalCache; 333 } 334 335 347 public String getDefaultReplicationGranularity() 348 { 349 return defaultReplicationGranularity; 350 } 351 352 362 public void setDefaultReplicationGranularity( 363 String defaultReplicationGranularity) 364 { 365 this.defaultReplicationGranularity = defaultReplicationGranularity; 366 } 367 368 379 public String getDefaultReplicationTrigger() 380 { 381 return defaultReplicationTrigger; 382 } 383 384 395 public void setDefaultReplicationTrigger(String defaultTrigger) 396 { 397 this.defaultReplicationTrigger = defaultTrigger; 398 } 399 400 406 public boolean getDefaultReplicationFieldBatchMode() 407 { 408 return replicationFieldBatchMode; 409 } 410 411 417 public void setDefaultReplicationFieldBatchMode(boolean replicationFieldBatchMode) 418 { 419 this.replicationFieldBatchMode = replicationFieldBatchMode; 420 } 421 422 435 public String getSnapshotMode() 436 { 437 return snapshotMode; 438 } 439 440 449 public void setSnapshotMode(String snapshotMode) 450 { 451 this.snapshotMode = snapshotMode; 452 } 453 454 463 public int getSnapshotInterval() 464 { 465 return snapshotInterval; 466 } 467 468 475 public void setSnapshotInterval(int snapshotInterval) 476 { 477 this.snapshotInterval = snapshotInterval; 478 } 479 480 482 487 public String getClusterName() 488 { 489 return clusterName; 490 } 491 492 495 public Container getContainer() 496 { 497 return container; 498 } 499 500 503 public void setContainer(Container container) 504 { 505 this.container = container; 506 } 507 508 513 public String getInfo() 514 { 515 return info; 516 } 517 518 521 public Manager createManager(String name) 522 { 523 if (log.isDebugEnabled()) 524 log.debug("Creating ClusterManager for context " + name 525 + " using class " + getManagerClassName()); 526 Manager manager = null; 527 try 528 { 529 manager = (Manager) getClass().getClassLoader().loadClass( 530 getManagerClassName()).newInstance(); 531 } 532 catch (Exception x) 533 { 534 log.error("Unable to load class for replication manager", x); 535 manager = new JBossCacheManager(); 536 } 537 finally 538 { 539 manager.setDistributable(true); 540 } 541 542 if (manager instanceof JBossCacheManager) 543 { 544 configureManager((JBossCacheManager) manager); 545 } 546 547 return manager; 548 } 549 550 556 public void backgroundProcess() 557 { 558 ; } 560 561 563 570 public String getProtocol() 571 { 572 return null; 573 } 574 575 580 public void setProtocol(String protocol) 581 { 582 ; } 584 585 590 public void startContext(String contextPath) throws IOException 591 { 592 ; } 594 595 600 public void installContext(String contextPath, URL war) 601 { 602 ; } 604 605 610 public void stop(String contextPath) throws IOException 611 { 612 ; } 614 615 617 623 public void configureManager(JBossCacheManager manager) 624 { 625 manager.setSnapshotMode(snapshotMode); 626 manager.setSnapshotInterval(snapshotInterval); 627 manager.setUseJK(useJK); 628 manager.setUseLocalCache(useLocalCache); 629 manager.setCacheObjectNameString(pojoCacheObjectName); 630 631 634 if (manager.getReplicationGranularityString() == null) 635 { 636 manager.setReplicationGranularityString(defaultReplicationGranularity); 637 } 638 639 if (manager.getReplicationTriggerString() == null) 640 { 641 manager.setReplicationTriggerString(defaultReplicationTrigger); 642 } 643 644 if (manager.isReplicationFieldBatchMode() == null) 645 { 646 manager.setReplicationFieldBatchMode(replicationFieldBatchMode); 647 } 648 } 649 650 652 658 public void start() throws LifecycleException 659 { 660 if (started) 661 { 662 throw new LifecycleException("Cluster already started"); 663 } 664 665 lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, this); 667 668 try 669 { 670 MBeanServerLocator.setJBoss(getMBeanServer()); 673 674 PojoCacheJmxWrapperMBean cache = getTreeCache(); 676 677 registerMBeans(); 678 679 if (pojoCacheLocal) 680 { 681 cache.create(); 682 cache.start(); 683 } 684 685 started = true; 686 687 lifecycle.fireLifecycleEvent(AFTER_START_EVENT, this); 689 690 } 691 catch (LifecycleException e) 692 { 693 throw e; 694 } 695 catch (Exception e) 696 { 697 log.error("Unable to start cluster.", e); 698 throw new LifecycleException(e); 699 } 700 } 701 702 708 public void stop() throws LifecycleException 709 { 710 if (!started) 711 { 712 throw new IllegalStateException ("Cluster not started"); 713 } 714 715 lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, this); 717 718 if (pojoCacheLocal) 719 { 720 pojoCache.stop(); 721 pojoCache.destroy(); 722 } 723 724 started = false; 725 lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, this); 727 728 unregisterMBeans(); 729 } 730 731 734 public void addLifecycleListener(LifecycleListener listener) 735 { 736 lifecycle.addLifecycleListener(listener); 737 } 738 739 742 public LifecycleListener[] findLifecycleListeners() 743 { 744 return lifecycle.findLifecycleListeners(); 745 } 746 747 750 public void removeLifecycleListener(LifecycleListener listener) 751 { 752 lifecycle.removeLifecycleListener(listener); 753 } 754 755 757 761 private PojoCacheJmxWrapperMBean getTreeCache() throws Exception 762 { 763 if (pojoCache == null) { 764 765 MBeanServer server = getMBeanServer(); 766 ObjectName objName = new ObjectName (pojoCacheObjectName); 767 if (server.isRegistered(objName)) 768 { 769 pojoCache = (PojoCacheJmxWrapperMBean) 771 MBeanProxyExt.create(PojoCacheJmxWrapperMBean.class, objName); 772 } 773 else 774 { 775 InputStream configIS = getCacheConfigStream(); 777 778 Configuration config = null; 779 780 if (configIS != null) 781 { 782 XmlConfigurationParser parser = new XmlConfigurationParser(); 783 config = parser.parseStream(configIS); 784 try 785 { 786 configIS.close(); 787 } 788 catch (IOException io) 789 { 790 } 792 793 if (clusterName != null) 794 { 795 config.setClusterName(clusterName); 799 } 800 } 801 else 802 { 803 config = new Configuration(); 807 String channelName = (clusterName == null) ? DEFAULT_CLUSTER_NAME 808 : clusterName; 809 config.setClusterName(channelName); 810 config.setIsolationLevel(DEFAULT_ISOLATION_LEVEL); 811 config.setCacheMode(DEFAULT_CACHE_MODE); 812 config.setLockAcquisitionTimeout(DEFAULT_LOCK_TIMEOUT); 813 config.setTransactionManagerLookupClass(DEFAULT_TM_LOOKUP); 814 } 815 816 pojoCache = new PojoCacheJmxWrapper(PojoCacheFactory.createCache(config, false)); 817 818 pojoCacheLocal = true; 819 } 820 } 821 return pojoCache; 822 } 823 824 825 826 private InputStream getCacheConfigStream() throws FileNotFoundException 827 { 828 boolean useDefault = (this.cacheConfigPath == null); 829 String path = (useDefault) ? DEFAULT_CACHE_CONFIG_PATH : cacheConfigPath; 830 File file = new File (path); 833 if (!file.isAbsolute()) 834 { 835 file = new File (System.getProperty("catalina.base"), path); 836 } 837 838 try 839 { 840 return new FileInputStream (file); 841 } 842 catch (FileNotFoundException fnf) 843 { 844 if (useDefault) 845 { 846 return null; 850 } 851 else 852 { 853 log.error("No tree cache config file found at " + 855 file.getAbsolutePath()); 856 throw fnf; 857 } 858 } 859 } 860 861 864 private void registerMBeans() 865 { 866 try 867 { 868 MBeanServer server = getMBeanServer(); 869 870 String domain; 871 if (container instanceof ContainerBase) 872 { 873 domain = ((ContainerBase) container).getDomain(); 874 } 875 else 876 { 877 domain = server.getDefaultDomain(); 878 } 879 880 String name = ":type=Cluster"; 881 if (container instanceof Host) { 882 name += ",host=" + container.getName(); 883 } 884 else if (container instanceof Engine) 885 { 886 name += ",engine=" + container.getName(); 887 } 888 889 ObjectName clusterName = new ObjectName (domain + name); 890 891 if (server.isRegistered(clusterName)) 892 { 893 log.warn("MBean " + clusterName + " already registered"); 894 } 895 else 896 { 897 this.objectName = clusterName; 898 server.registerMBean(this, objectName); 899 } 900 901 if (pojoCacheLocal) 902 { 903 ObjectName treeCacheName = new ObjectName (pojoCacheObjectName); 905 server.registerMBean(getTreeCache(), treeCacheName); 906 } 907 908 } 909 catch (Exception ex) 910 { 911 log.error(ex.getMessage(), ex); 912 } 913 } 914 915 918 private void unregisterMBeans() 919 { 920 if (mserver != null) 921 { 922 try 923 { 924 if (objectName != null) { 925 mserver.unregisterMBean(objectName); 926 } 927 if (pojoCacheLocal) 928 { 929 mserver.unregisterMBean(new ObjectName (pojoCacheObjectName)); 930 } 931 } 932 catch (Exception e) 933 { 934 log.error(e); 935 } 936 } 937 } 938 939 945 private MBeanServer getMBeanServer() throws Exception 946 { 947 if (mserver == null) 948 { 949 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 950 if (servers.size() > 0) 951 { 952 mserver = (MBeanServer ) servers.get(0); 953 } 954 else 955 { 956 mserver = MBeanServerFactory.createMBeanServer(); 957 } 958 } 959 return mserver; 960 } 961 962 } 963 | Popular Tags |