1 22 package org.jboss.web.tomcat.tc6.session; 23 24 import java.io.IOException ; 25 import javax.servlet.http.HttpSession ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.ServletException ; 28 29 import org.apache.catalina.*; 30 import org.apache.catalina.connector.Request; 31 import org.apache.catalina.connector.Response; 32 import org.apache.catalina.util.LifecycleSupport; 33 import org.apache.catalina.valves.ValveBase; 34 import org.jboss.logging.Logger; 35 36 46 public class JvmRouteValve extends ValveBase implements Lifecycle 47 { 48 private static final String info = "JvmRouteValve/1.0"; 50 51 protected static Logger log_ = Logger.getLogger(JvmRouteValve.class); 52 53 protected LifecycleSupport support = new LifecycleSupport(this); 55 56 protected AbstractJBossManager manager_; 57 58 62 public JvmRouteValve(AbstractJBossManager manager) 63 { 64 super(); 65 manager_ = manager; 66 } 67 68 71 public String getInfo() 72 { 73 return info; 74 } 75 76 public void invoke(Request request, Response response) throws IOException , ServletException 77 { 78 79 checkJvmRoute(request, response); 82 83 getNext().invoke(request, response); 85 } 86 87 public void checkJvmRoute(Request req, Response res) 88 throws IOException , ServletException 89 { 90 HttpSession session = req.getSession(false); 91 if (session != null) 92 { 93 String sessionId = session.getId(); 94 95 String jvmRoute = manager_.getJvmRoute(); 97 if (log_.isDebugEnabled()) 98 { 99 log_.debug("checkJvmRoute(): check if need to re-route based on JvmRoute. Session id: " + 100 sessionId + " jvmRoute: " + jvmRoute); 101 } 102 103 if (jvmRoute == null) 104 { 105 throw new RuntimeException ("JvmRouteValve.checkJvmRoute(): Tomcat JvmRoute is null. " + 106 "Need to assign a value in Tomcat server.xml for load balancing."); 107 } 108 109 boolean setCookie = !req.isRequestedSessionIdFromURL(); 111 handleJvmRoute(sessionId, jvmRoute, res, setCookie); 112 } 113 } 114 115 protected void handleJvmRoute(String sessionId, 116 String jvmRoute, 117 HttpServletResponse response, 118 boolean setCookie) 119 { 120 String requestedJvmRoute = null; 123 int index = sessionId.lastIndexOf("."); 124 if (index > 0) 125 { 126 requestedJvmRoute = sessionId.substring(index + 1, sessionId.length()); 127 } 128 129 String newId = sessionId; 130 if (!jvmRoute.equals(requestedJvmRoute)) 131 { 132 if (requestedJvmRoute == null) 133 { 134 newId = sessionId + "." + jvmRoute; 137 } 138 else 139 { 140 if (log_.isDebugEnabled()) 143 { 144 log_.debug("handleJvmRoute(): We have detected a failover with different jvmRoute." + 145 " old one: " + requestedJvmRoute + " new one: " + jvmRoute + ". Will reset the session id."); 146 } 147 148 String base = sessionId.substring(0, index); 149 newId = base + "." + jvmRoute; 150 } 151 152 resetSessionId(sessionId, newId); 153 154 if (setCookie) 155 manager_.setNewSessionCookie(newId, response); 156 } 157 } 158 159 private void resetSessionId(String oldId, String newId) 160 { 161 try 162 { 163 ClusteredSession session = (ClusteredSession)manager_.findSession(oldId); 164 if( session != null ) 166 { 167 session.resetIdWithRouteInfo(newId); 169 if (log_.isDebugEnabled()) 170 { 171 log_.debug("resetSessionId(): changed catalina session to= [" + newId + "] old one= [" + oldId + "]"); 172 } 173 } 174 else if (log_.isDebugEnabled()) 175 { 176 log_.debug("resetSessionId(): no session with id " + newId + " found"); 177 } 178 } 179 catch (IOException e) 180 { 181 if (log_.isDebugEnabled()) 182 { 183 log_.debug("resetSessionId(): manager_.findSession() unable to find session= [" + oldId + "]", e); 184 } 185 throw new RuntimeException ("JvmRouteValve.resetSessionId(): cannot find session [" + oldId + "]", e); 186 } 187 } 188 189 public void addLifecycleListener(LifecycleListener listener) 191 { 192 support.addLifecycleListener(listener); 193 } 194 195 public void removeLifecycleListener(LifecycleListener listener) 196 { 197 support.removeLifecycleListener(listener); 198 } 199 200 public LifecycleListener[] findLifecycleListeners() 201 { 202 return support.findLifecycleListeners(); 203 } 204 205 public void start() throws LifecycleException 206 { 207 support.fireLifecycleEvent(START_EVENT, this); 208 } 209 210 public void stop() throws LifecycleException 211 { 212 support.fireLifecycleEvent(STOP_EVENT, this); 213 } 214 215 } 216 | Popular Tags |