1 7 package org.jboss.web.tomcat.tc5; 8 9 import java.io.IOException ; 10 import java.util.Enumeration ; 11 12 import javax.servlet.Filter ; 13 import javax.servlet.FilterChain ; 14 import javax.servlet.FilterConfig ; 15 import javax.servlet.ServletContext ; 16 import javax.servlet.ServletException ; 17 import javax.servlet.ServletRequest ; 18 import javax.servlet.ServletResponse ; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import javax.servlet.http.HttpSession ; 22 23 import org.apache.catalina.Session; 24 import org.jboss.logging.Logger; 25 import org.jboss.util.NestedRuntimeException; 26 import org.jboss.web.tomcat.tc5.session.AbstractJBossManager; 27 28 39 public class JvmRouteFilter 40 implements Filter 41 { 42 protected AbstractJBossManager manager_; 43 protected static Logger log_ = Logger.getLogger(JvmRouteFilter.class); 44 45 public void init(FilterConfig filterConfig) throws ServletException 46 { 47 if (log_.isDebugEnabled()) 48 { 49 ServletContext sc = filterConfig.getServletContext(); 50 Enumeration names = sc.getAttributeNames(); 51 while (names.hasMoreElements()) 52 { 53 String name = (String ) names.nextElement(); 54 Object value = sc.getAttribute(name); 55 log_.debug("name=" + name + ", value.className: [" + value.getClass().getName() + "] value.toString: [" + value.toString() + "]"); 56 } 57 } 58 manager_ = (AbstractJBossManager) filterConfig.getServletContext().getAttribute("AbstractJBossManager"); 59 if (manager_ == null) 60 { 61 throw new RuntimeException ("JvmRouteFilter.init(): No AbstractJBossManager found for clustering support."); 62 } 63 64 if (log_.isDebugEnabled()) 65 log_.debug("JvmRouteFilter.init(): initializing JvmRouteFilter"); 66 } 67 68 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 69 throws IOException , ServletException 70 { 71 if (!(request instanceof HttpServletRequest && response instanceof HttpServletResponse )) 73 { 74 throw new RuntimeException ("JvmRouteFilter.doFilter(): Not a http request and response type."); 76 } 77 78 HttpServletRequest req = (HttpServletRequest ) request; 80 HttpServletResponse res = (HttpServletResponse ) response; 81 HttpSession session = req.getSession(false); 82 if (session != null) 83 { 84 String sessionId = session.getId(); 85 86 String jvmRoute = manager_.getJvmRoute(); 88 if (log_.isDebugEnabled()) 89 { 90 log_.debug("doFilter(): check if need to re-route based on JvmRoute. Session id: " + 91 sessionId + " jvmRoute: " + jvmRoute); 92 } 93 94 if (jvmRoute == null) 95 { 96 throw new RuntimeException ("JvmRouteFilter.doFilter(): Tomcat JvmRoute is null. " + 97 "Need to assign a value in Tomcat server.xml for load balancing."); 98 } 99 100 if (req.isRequestedSessionIdFromURL()) 103 { 104 log_.error("JvmRouteFilter.doFilter(): Can't handle clustering where session id is from URL. Will skip."); 106 } 107 else 108 { 109 handleJvmRoute(sessionId, jvmRoute, res); 110 } 111 } 112 chain.doFilter(request, response); 113 } 114 115 protected void handleJvmRoute(String sessionId, String jvmRoute, HttpServletResponse response) 116 { 117 String requestedJvmRoute = null; 120 int ind = sessionId.indexOf("."); 121 if (ind > 0) 122 { 123 requestedJvmRoute = sessionId.substring(sessionId.indexOf(".") + 1, sessionId.length()); 124 } 125 126 String sid = sessionId; 127 if (requestedJvmRoute == null) 128 { 129 sid = sessionId + "." + jvmRoute; 131 manager_.setNewSessionCookie(sid, response); 132 } 133 else if (requestedJvmRoute.equals(jvmRoute)) 134 { 135 return; } 137 else 138 { 139 if (log_.isDebugEnabled()) 141 { 142 log_.debug("handleJvmRoute(): We have detected a failover with differen jvmRoute." + 143 " old one: " + requestedJvmRoute + " new one: " + jvmRoute + ". Will reset the session id."); 144 } 145 int index = sessionId.indexOf("."); 146 if (index > 0) 147 { 148 String base = sessionId.substring(0, sessionId.indexOf(".")); 149 sid = base + "." + jvmRoute; 150 } 151 else 152 { 153 throw new RuntimeException ("JvmRouteFilter.handleJvmRoute(): session id doesn't JvmRoute."); 154 } 155 156 manager_.setNewSessionCookie(sid, response); 157 Session catalinaSession = null; 159 try 160 { 161 catalinaSession = manager_.findSession(sessionId); 162 if( catalinaSession != null ) 164 { 165 catalinaSession.setId(sid); 166 if (log_.isDebugEnabled()) 167 { 168 log_.debug("handleJvmRoute(): changed catalina session to= [" + sid + "] old one= [" + sessionId + "]"); 169 } 170 } 171 } 172 catch (IOException e) 173 { 174 if (log_.isDebugEnabled()) 175 { 176 log_.debug("handleJvmRoute(): manager_.findSession() unable to find session= [" + sessionId + "]", e); 177 } 178 throw new NestedRuntimeException("JvmRouteFilter.handleJvmRoute(): cannot find session [" + sessionId + "]", e); 179 } 180 } 181 } 182 183 184 public void destroy() 185 { 186 manager_ = null; 187 } 188 } 189 | Popular Tags |