1 16 package org.apache.catalina.cluster.session; 17 18 import javax.management.MBeanServer ; 19 import javax.management.MBeanServerFactory ; 20 import javax.management.MalformedObjectNameException ; 21 import javax.management.ObjectName ; 22 import javax.management.modelmbean.ModelMBean ; 23 24 import org.apache.catalina.Lifecycle; 25 import org.apache.catalina.LifecycleEvent; 26 import org.apache.catalina.LifecycleListener; 27 import org.apache.catalina.cluster.CatalinaCluster; 28 import org.apache.catalina.cluster.MessageListener; 29 import org.apache.catalina.core.StandardHost; 30 import org.apache.catalina.util.StringManager; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.commons.modeler.ManagedBean; 34 import org.apache.commons.modeler.Registry; 35 36 50 public class JvmRouteSessionIDBinderLifecycleListener implements 51 LifecycleListener { 52 private static Log log = LogFactory 53 .getLog(JvmRouteSessionIDBinderLifecycleListener.class); 54 55 58 private static final String info = "org.apache.catalina.cluster.session.JvmRouteSessionIDBinderLifecycleListener/1.0"; 59 60 63 protected static final StringManager sm = StringManager 64 .getManager(Constants.Package); 65 66 private boolean enabled = true; 67 68 private MBeanServer mserver = null; 69 70 private Registry registry = null; 71 72 private MessageListener sessionMoverListener; 73 74 79 public void lifecycleEvent(LifecycleEvent event) { 80 81 if (enabled && event.getSource() instanceof StandardHost) { 82 83 if (Lifecycle.AFTER_START_EVENT.equals(event.getType())) { 84 if (log.isDebugEnabled()) 85 log.debug(sm.getString("jvmRoute.listener.started")); 86 startSessionIDListener((StandardHost) event.getSource()); 87 } else if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType())) { 88 if (log.isDebugEnabled()) 89 log.debug(sm.getString("jvmRoute.listener.stopped")); 90 stopSessionIDListener((StandardHost) event.getSource()); 91 } 92 } 93 } 94 95 101 protected void stopSessionIDListener(StandardHost host) { 102 if (sessionMoverListener != null) { 103 CatalinaCluster cluster = (CatalinaCluster) host.getCluster(); 104 cluster.removeClusterListener(sessionMoverListener); 105 if (mserver != null) { 106 try { 107 ObjectName objectName = getObjectName(host); 108 mserver.unregisterMBean(objectName); 109 } catch (Exception e) { 110 log.error(e); 111 } 112 } 113 } 114 } 115 116 121 protected ObjectName getObjectName(StandardHost host) throws MalformedObjectNameException { 122 ObjectName objectName = new ObjectName ( 123 host.getDomain() 124 + ":type=Listener,name=JvmRouteSessionIDBinderListener,host=" + host.getName()); 125 return objectName; 126 } 127 128 134 protected void startSessionIDListener(StandardHost host) { 135 try { 136 ObjectName objectName = null; 137 getMBeanServer(); 138 objectName = getObjectName(host); 139 if (mserver.isRegistered(objectName)) { 140 if (log.isInfoEnabled()) 141 log.info(sm.getString("jvmRoute.run.already")); 142 return; 143 } 144 sessionMoverListener = new JvmRouteSessionIDBinderListener(); 145 mserver.registerMBean(getManagedBean(sessionMoverListener), 146 objectName); 147 CatalinaCluster cluster = (CatalinaCluster) host.getCluster(); 148 sessionMoverListener.setCluster(cluster); 149 ((JvmRouteSessionIDBinderListener) sessionMoverListener).start(); 150 } catch (Exception ex) { 151 log.error(ex.getMessage(), ex); 152 } 153 } 154 155 protected MBeanServer getMBeanServer() throws Exception { 156 if (mserver == null) { 157 if (MBeanServerFactory.findMBeanServer(null).size() > 0) { 158 mserver = (MBeanServer ) MBeanServerFactory 159 .findMBeanServer(null).get(0); 160 } else { 161 mserver = MBeanServerFactory.createMBeanServer(); 162 } 163 registry = Registry.getRegistry(null, null); 164 registry.loadMetadata(this.getClass().getResourceAsStream( 165 "mbeans-descriptors.xml")); 166 } 167 return (mserver); 168 } 169 170 179 protected ModelMBean getManagedBean(Object object) throws Exception { 180 ModelMBean mbean = null; 181 if (registry != null) { 182 ManagedBean managedBean = registry.findManagedBean(object 183 .getClass().getName()); 184 mbean = managedBean.createMBean(object); 185 } 186 return mbean; 187 } 188 189 192 public boolean isEnabled() { 193 return enabled; 194 } 195 196 200 public void setEnabled(boolean enabled) { 201 this.enabled = enabled; 202 } 203 204 209 public String getInfo() { 210 211 return (info); 212 213 } 214 } | Popular Tags |