1 16 17 package org.apache.catalina.cluster.session; 18 19 import java.io.IOException ; 20 21 import org.apache.catalina.Container; 22 import org.apache.catalina.Context; 23 import org.apache.catalina.LifecycleException; 24 import org.apache.catalina.Session; 25 import org.apache.catalina.cluster.CatalinaCluster; 26 import org.apache.catalina.cluster.ClusterMessage; 27 import org.apache.catalina.cluster.MessageListener; 28 import org.apache.catalina.core.StandardEngine; 29 import org.apache.catalina.util.StringManager; 30 31 38 public class JvmRouteSessionIDBinderListener implements MessageListener { 39 40 public static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 41 .getLog(JvmRouteSessionIDBinderListener.class); 42 43 46 protected static final String info = "org.apache.catalina.session.JvmRouteSessionIDBinderListener/1.0"; 47 48 49 50 53 private StringManager sm = StringManager.getManager(Constants.Package); 54 55 protected CatalinaCluster cluster = null; 56 57 protected boolean started = false; 58 59 62 private long numberOfSessions = 0; 63 64 65 66 public JvmRouteSessionIDBinderListener() { 67 } 68 69 70 71 74 public String getInfo() { 75 76 return (info); 77 78 } 79 80 83 public long getNumberOfSessions() { 84 return numberOfSessions; 85 } 86 87 92 public void start() throws LifecycleException { 93 if (started) 94 return; 95 getCluster().addClusterListener(this); 96 started = true; 97 if (log.isInfoEnabled()) 98 log.info(sm.getString("jvmRoute.clusterListener.started")); 99 } 100 101 106 public void stop() throws LifecycleException { 107 started = false; 108 getCluster().removeClusterListener(this); 109 if (log.isInfoEnabled()) 110 log.info(sm.getString("jvmRoute.clusterListener.stopped")); 111 } 112 113 120 public void messageReceived(ClusterMessage msg) { 121 if (msg instanceof SessionIDMessage && msg != null) { 122 SessionIDMessage sessionmsg = (SessionIDMessage) msg; 123 if (log.isDebugEnabled()) 124 log.debug(sm.getString( 125 "jvmRoute.receiveMessage.sessionIDChanged", sessionmsg 126 .getOrignalSessionID(), sessionmsg 127 .getBackupSessionID(), sessionmsg 128 .getContextPath())); 129 Container host = getCluster().getContainer(); 130 if (host != null) { 131 Context context = (Context) host.findChild(sessionmsg 132 .getContextPath()); 133 if (context != null) { 134 try { 135 Session session = context.getManager().findSession( 136 sessionmsg.getOrignalSessionID()); 137 if (session != null) { 138 session.setId(sessionmsg.getBackupSessionID()); 139 } else if (log.isInfoEnabled()) 140 log.info(sm.getString("jvmRoute.lostSession", 141 sessionmsg.getOrignalSessionID(), 142 sessionmsg.getContextPath())); 143 } catch (IOException e) { 144 log.error(e); 145 } 146 147 } else if (log.isErrorEnabled()) 148 log.error(sm.getString("jvmRoute.contextNotFound", 149 sessionmsg.getContextPath(), ((StandardEngine) host 150 .getParent()).getJvmRoute())); 151 } else if (log.isErrorEnabled()) 152 log.error(sm.getString("jvmRoute.hostNotFound", sessionmsg.getContextPath())); 153 } 154 } 155 156 165 public boolean accept(ClusterMessage msg) { 166 return (msg instanceof SessionIDMessage); 167 } 168 169 170 public CatalinaCluster getCluster() { 171 return cluster; 172 } 173 174 public void setCluster(CatalinaCluster cluster) { 175 this.cluster = cluster; 176 } 177 178 public boolean equals(Object listener) { 179 return super.equals(listener); 180 } 181 182 public int hashCode() { 183 return super.hashCode(); 184 } 185 186 } 187 188 | Popular Tags |