1 22 package org.jboss.web.tomcat.tc6; 23 24 import java.io.IOException ; 25 import java.util.Enumeration ; 26 27 import javax.servlet.Filter ; 28 import javax.servlet.FilterChain ; 29 import javax.servlet.FilterConfig ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.ServletRequest ; 33 import javax.servlet.ServletResponse ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 import javax.servlet.http.HttpSession ; 37 38 import org.apache.catalina.Session; 39 import org.jboss.logging.Logger; 40 import org.jboss.util.NestedRuntimeException; 41 import org.jboss.web.tomcat.tc6.session.AbstractJBossManager; 42 43 55 public class JvmRouteFilter 56 implements Filter 57 { 58 protected AbstractJBossManager manager_; 59 protected static Logger log_ = Logger.getLogger(JvmRouteFilter.class); 60 61 public void init(FilterConfig filterConfig) throws ServletException 62 { 63 if (log_.isDebugEnabled()) 64 { 65 ServletContext sc = filterConfig.getServletContext(); 66 Enumeration names = sc.getAttributeNames(); 67 while (names.hasMoreElements()) 68 { 69 String name = (String ) names.nextElement(); 70 Object value = sc.getAttribute(name); 71 log_.debug("name=" + name + ", value.className: [" + value.getClass().getName() + "] value.toString: [" + value.toString() + "]"); 72 } 73 } 74 manager_ = (AbstractJBossManager) filterConfig.getServletContext().getAttribute("AbstractJBossManager"); 75 if (manager_ == null) 76 { 77 throw new RuntimeException ("JvmRouteFilter.init(): No AbstractJBossManager found for clustering support."); 78 } 79 80 if (log_.isDebugEnabled()) 81 log_.debug("JvmRouteFilter.init(): initializing JvmRouteFilter"); 82 } 83 84 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 85 throws IOException , ServletException 86 { 87 if (!(request instanceof HttpServletRequest && response instanceof HttpServletResponse )) 89 { 90 throw new RuntimeException ("JvmRouteFilter.doFilter(): Not a http request and response type."); 92 } 93 94 HttpServletRequest req = (HttpServletRequest ) request; 96 HttpServletResponse res = (HttpServletResponse ) response; 97 HttpSession session = req.getSession(false); 98 if (session != null) 99 { 100 String sessionId = session.getId(); 101 102 String jvmRoute = manager_.getJvmRoute(); 104 if (log_.isDebugEnabled()) 105 { 106 log_.debug("doFilter(): check if need to re-route based on JvmRoute. Session id: " + 107 sessionId + " jvmRoute: " + jvmRoute); 108 } 109 110 if (jvmRoute == null) 111 { 112 throw new RuntimeException ("JvmRouteFilter.doFilter(): Tomcat JvmRoute is null. " + 113 "Need to assign a value in Tomcat server.xml for load balancing."); 114 } 115 116 if (req.isRequestedSessionIdFromURL()) 119 { 120 log_.error("JvmRouteFilter.doFilter(): Can't handle clustering where session id is from URL. Will skip."); 122 } 123 else 124 { 125 handleJvmRoute(sessionId, jvmRoute, res); 126 } 127 } 128 chain.doFilter(request, response); 129 } 130 131 protected void handleJvmRoute(String sessionId, String jvmRoute, HttpServletResponse response) 132 { 133 String requestedJvmRoute = null; 136 int ind = sessionId.indexOf("."); 137 if (ind > 0) 138 { 139 requestedJvmRoute = sessionId.substring(sessionId.indexOf(".") + 1, sessionId.length()); 140 } 141 142 String sid = sessionId; 143 if (requestedJvmRoute == null) 144 { 145 sid = sessionId + "." + jvmRoute; 147 manager_.setNewSessionCookie(sid, response); 148 } 149 else if (requestedJvmRoute.equals(jvmRoute)) 150 { 151 return; } 153 else 154 { 155 if (log_.isDebugEnabled()) 157 { 158 log_.debug("handleJvmRoute(): We have detected a failover with differen jvmRoute." + 159 " old one: " + requestedJvmRoute + " new one: " + jvmRoute + ". Will reset the session id."); 160 } 161 int index = sessionId.indexOf("."); 162 if (index > 0) 163 { 164 String base = sessionId.substring(0, sessionId.indexOf(".")); 165 sid = base + "." + jvmRoute; 166 } 167 else 168 { 169 throw new RuntimeException ("JvmRouteFilter.handleJvmRoute(): session id doesn't JvmRoute."); 170 } 171 172 manager_.setNewSessionCookie(sid, response); 173 Session catalinaSession = null; 175 try 176 { 177 catalinaSession = manager_.findSession(sessionId); 178 if( catalinaSession != null ) 180 { 181 catalinaSession.setId(sid); 182 if (log_.isDebugEnabled()) 183 { 184 log_.debug("handleJvmRoute(): changed catalina session to= [" + sid + "] old one= [" + sessionId + "]"); 185 } 186 } 187 } 188 catch (IOException e) 189 { 190 if (log_.isDebugEnabled()) 191 { 192 log_.debug("handleJvmRoute(): manager_.findSession() unable to find session= [" + sessionId + "]", e); 193 } 194 throw new NestedRuntimeException("JvmRouteFilter.handleJvmRoute(): cannot find session [" + sessionId + "]", e); 195 } 196 } 197 } 198 199 200 public void destroy() 201 { 202 manager_ = null; 203 } 204 } 205 | Popular Tags |