KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc6 > JvmRouteFilter


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.web.tomcat.tc6;
23
24 import java.io.IOException JavaDoc;
25 import java.util.Enumeration JavaDoc;
26
27 import javax.servlet.Filter JavaDoc;
28 import javax.servlet.FilterChain JavaDoc;
29 import javax.servlet.FilterConfig JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.ServletRequest JavaDoc;
33 import javax.servlet.ServletResponse JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import javax.servlet.http.HttpSession JavaDoc;
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 /**
44  * Web request filter to specifically handle Tomcat jvmRoute using mod_jk(2)
45  * module. We assume that the session is set by cookie only for now, i.e., no
46  * support of that from URL. Furthermore, the session id has a format of
47  * id.jvmRoute where jvmRoute is used by JK module to determine sticky session
48  * during load balancing.
49  *
50  * @author Ben Wang
51  * @author Marco Antonioni
52  * @version $Revision: 45726 $
53  * @deprecated 4.0.3
54  */

55 public class JvmRouteFilter
56    implements Filter JavaDoc
57 {
58    protected AbstractJBossManager manager_;
59    protected static Logger log_ = Logger.getLogger(JvmRouteFilter.class);
60
61    public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
62    {
63       if (log_.isDebugEnabled())
64       {
65          ServletContext JavaDoc sc = filterConfig.getServletContext();
66          Enumeration JavaDoc names = sc.getAttributeNames();
67          while (names.hasMoreElements())
68          {
69             String JavaDoc name = (String JavaDoc) names.nextElement();
70             Object JavaDoc 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 JavaDoc("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 JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain)
85       throws IOException JavaDoc, ServletException JavaDoc
86    {
87       // Check if request and response is valid
88
if (!(request instanceof HttpServletRequest JavaDoc && response instanceof HttpServletResponse JavaDoc))
89       {
90          // Don't know how to handle. There is a mistake.
91
throw new RuntimeException JavaDoc("JvmRouteFilter.doFilter(): Not a http request and response type.");
92       }
93
94       // get session id
95
HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
96       HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
97       HttpSession JavaDoc session = req.getSession(false);
98       if (session != null)
99       {
100          String JavaDoc sessionId = session.getId();
101    
102          // Obtain JvmRoute
103
String JavaDoc 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 JavaDoc("JvmRouteFilter.doFilter(): Tomcat JvmRoute is null. " +
113                "Need to assign a value in Tomcat server.xml for load balancing.");
114          }
115    
116          // Check if incoming session id has JvmRoute appended. If not, append it.
117
// TODO. We handle only get session id by cookie only
118
if (req.isRequestedSessionIdFromURL())
119          {
120             // Warning but do nothing
121
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 JavaDoc sessionId, String JavaDoc jvmRoute, HttpServletResponse JavaDoc response)
132    {
133       // get requested jvmRoute.
134
// TODO. The current format is assumed to be id.jvmRoute. Can be generalized later.
135
String JavaDoc 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 JavaDoc sid = sessionId;
143       if (requestedJvmRoute == null)
144       {
145          // If this filter is turned on, we assume we have an appendix of jvmRoute. So this request is new.
146
sid = sessionId + "." + jvmRoute;
147          manager_.setNewSessionCookie(sid, response);
148       }
149       else if (requestedJvmRoute.equals(jvmRoute))
150       {
151          return; // Nothing more needs to be done.
152
}
153       else
154       {
155          // We just have a failover since jvmRoute does not match. We will replace the old one with the new one.
156
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 JavaDoc base = sessionId.substring(0, sessionId.indexOf("."));
165             sid = base + "." + jvmRoute;
166          }
167          else
168          {
169             throw new RuntimeException JavaDoc("JvmRouteFilter.handleJvmRoute(): session id doesn't JvmRoute.");
170          }
171
172          manager_.setNewSessionCookie(sid, response);
173          // Change the sessionId with the new one using local jvmRoute
174
Session JavaDoc catalinaSession = null;
175          try
176          {
177             catalinaSession = manager_.findSession(sessionId);
178             // change session id with the new one using local jvmRoute and update cluster if needed.
179
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 JavaDoc 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