1 16 package org.apache.catalina.cluster.session; 17 18 import java.io.IOException ; 19 20 import javax.servlet.ServletException ; 21 import javax.servlet.http.Cookie ; 22 import javax.servlet.http.HttpSession ; 23 24 import org.apache.catalina.Container; 25 import org.apache.catalina.Context; 26 import org.apache.catalina.Globals; 27 import org.apache.catalina.Host; 28 import org.apache.catalina.Lifecycle; 29 import org.apache.catalina.LifecycleException; 30 import org.apache.catalina.LifecycleListener; 31 import org.apache.catalina.Manager; 32 import org.apache.catalina.Session; 33 import org.apache.catalina.cluster.CatalinaCluster; 34 import org.apache.catalina.cluster.ClusterMessage; 35 import org.apache.catalina.connector.Request; 36 import org.apache.catalina.connector.Response; 37 import org.apache.catalina.session.ManagerBase; 38 import org.apache.catalina.util.LifecycleSupport; 39 import org.apache.catalina.util.StringManager; 40 import org.apache.catalina.valves.ValveBase; 41 42 71 public class JvmRouteBinderValve extends ValveBase implements Lifecycle { 72 73 74 public static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 75 .getLog(JvmRouteBinderValve.class); 76 77 80 protected static final String info = "org.apache.catalina.cluster.session.JvmRouteBinderValve/1.1"; 81 82 83 84 87 protected CatalinaCluster cluster; 88 89 92 protected Manager manager; 93 94 97 protected boolean managerCheck = false; 98 99 102 protected StringManager sm = StringManager.getManager(Constants.Package); 103 104 107 protected boolean started = false; 108 109 112 protected boolean enabled = true; 113 114 117 protected long numberOfSessions = 0; 118 119 protected String sessionIdAttribute = "org.apache.catalina.cluster.session.JvmRouteOrignalSessionID"; 120 121 124 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 125 126 127 128 131 public String getInfo() { 132 133 return (info); 134 135 } 136 137 142 public String getSessionIdAttribute() { 143 return sessionIdAttribute; 144 } 145 146 152 public void setSessionIdAttribute(String sessionIdAttribute) { 153 this.sessionIdAttribute = sessionIdAttribute; 154 } 155 156 159 public long getNumberOfSessions() { 160 return numberOfSessions; 161 } 162 163 166 public boolean getEnabled() { 167 return enabled; 168 } 169 170 174 public void setEnabled(boolean enabled) { 175 this.enabled = enabled; 176 } 177 178 190 public void invoke(Request request, Response response) throws IOException , 191 ServletException { 192 193 getManager(); 195 if (getEnabled() && manager != null) { 196 handlePossibleTurnover(request, response); 197 } 198 getNext().invoke(request, response); 200 } 201 202 210 protected void handlePossibleTurnover(Request request, Response response) { 211 HttpSession session = request.getSession(false); 212 if (session != null) { 213 long t1 = System.currentTimeMillis(); 214 String jvmRoute = getLocalJvmRoute(); 215 if (jvmRoute == null) { 216 if (log.isWarnEnabled()) 217 log.warn(sm.getString("jvmRoute.missingJvmRouteAttribute")); 218 return; 219 } 220 if (request.isRequestedSessionIdFromURL()) { 221 if (log.isDebugEnabled()) 222 log.debug(sm.getString("jvmRoute.skipURLSessionIDs")); 223 } else { 224 handleJvmRoute(session.getId(), jvmRoute, request, response); 225 } 226 if (log.isInfoEnabled()) { 227 long t2 = System.currentTimeMillis(); 228 long time = t2 - t1; 229 log.info(sm.getString("jvmRoute.turnoverInfo", new Long (time))); 230 } 231 } 232 } 233 234 239 protected String getLocalJvmRoute() { 240 return ((ManagerBase) manager).getJvmRoute(); 241 } 242 243 248 protected Manager getManager() { 249 if (!managerCheck) { 250 managerCheck = true; 251 if (container.getManager() instanceof DeltaManager) { 252 manager = container.getManager(); 253 if (log.isDebugEnabled()) 254 log.debug(sm.getString("jvmRoute.foundManager", container 255 .getManager(), getContainer().getName())); 256 } else if (log.isDebugEnabled()) 257 log.debug(sm.getString("jvmRoute.notFoundManager", container 258 .getManager(), getContainer().getName())); 259 } 260 return manager; 261 } 262 263 275 protected void handleJvmRoute(String sessionId, String localJvmRoute, 276 Request request, Response response) { 277 String requestJvmRoute = null; 279 int index = sessionId.indexOf("."); 280 if (index > 0) { 281 requestJvmRoute = sessionId 282 .substring(index + 1, sessionId.length()); 283 } 284 if (requestJvmRoute != null && !requestJvmRoute.equals(localJvmRoute)) { 285 if (log.isDebugEnabled()) { 286 log.debug(sm.getString("jvmRoute.failover", requestJvmRoute, 287 localJvmRoute, sessionId)); 288 } 289 String newSessionID = sessionId.substring(0, index) + "." 291 + localJvmRoute; 292 Session catalinaSession = null; 293 try { 294 catalinaSession = manager.findSession(sessionId); 295 } catch (IOException e) { 296 } 298 if (catalinaSession != null) { 299 changeSessionID(sessionId, request, response, newSessionID, 300 catalinaSession); 301 numberOfSessions++; 302 } else { 303 if (log.isDebugEnabled()) { 304 log.debug(sm.getString("jvmRoute.cannotFindSession", 305 sessionId)); 306 } 307 } 308 } 309 } 310 311 323 protected void changeSessionID(String sessionId, Request request, 324 Response response, String newSessionID, Session catalinaSession) { 325 lifecycle.fireLifecycleEvent("Before session migration", 326 catalinaSession); 327 request.setRequestedSessionId(newSessionID); 328 catalinaSession.setId(newSessionID); 329 if (catalinaSession instanceof DeltaSession) 330 ((DeltaSession) catalinaSession).resetDeltaRequest(); 331 setNewSessionCookie(newSessionID, request, response); 332 if (sessionIdAttribute != null && !"".equals(sessionIdAttribute)) { 335 if (log.isDebugEnabled()) { 336 log.debug("Set Orginal Session id at request attriute " + sessionIdAttribute+ " value: " + sessionId); 337 } 338 request.setAttribute(sessionIdAttribute, sessionId); 339 } 340 sendSessionIDClusterBackup(sessionId, newSessionID); 342 lifecycle 343 .fireLifecycleEvent("After session migration", catalinaSession); 344 if (log.isDebugEnabled()) { 345 log.debug(sm.getString("jvmRoute.changeSession", sessionId, 346 newSessionID)); 347 } 348 } 349 350 359 protected void sendSessionIDClusterBackup(String sessionId, 360 String newSessionID) { 361 SessionIDMessage msg = new SessionIDMessage(); 362 msg.setOrignalSessionID(sessionId); 363 msg.setBackupSessionID(newSessionID); 364 Context context = (Context) getContainer(); 365 msg.setContextPath(context.getPath()); 366 cluster.send(msg); 367 } 368 369 378 379 protected void setNewSessionCookie(String sessionId, Request request, 380 Response response) { 381 if (response != null) { 382 Context context = (Context) getContainer(); 383 if (context.getCookies()) { 384 Cookie newCookie = new Cookie (Globals.SESSION_COOKIE_NAME, 386 sessionId); 387 newCookie.setMaxAge(-1); 388 String contextPath = null; 389 if (!response.getConnector().getEmptySessionPath() 390 && (context != null)) { 391 contextPath = context.getEncodedPath(); 392 } 393 if ((contextPath != null) && (contextPath.length() > 0)) { 394 newCookie.setPath(contextPath); 395 } else { 396 newCookie.setPath("/"); 397 } 398 if (request.isSecure()) { 399 newCookie.setSecure(true); 400 } 401 if (log.isDebugEnabled()) { 402 log.debug(sm.getString("jvmRoute.newSessionCookie", 403 sessionId, Globals.SESSION_COOKIE_NAME, newCookie 404 .getPath(), new Boolean (newCookie 405 .getSecure()))); 406 } 407 response.addCookie(newCookie); 408 } 409 } 410 } 411 412 414 420 public void addLifecycleListener(LifecycleListener listener) { 421 422 lifecycle.addLifecycleListener(listener); 423 424 } 425 426 430 public LifecycleListener[] findLifecycleListeners() { 431 432 return lifecycle.findLifecycleListeners(); 433 434 } 435 436 442 public void removeLifecycleListener(LifecycleListener listener) { 443 444 lifecycle.removeLifecycleListener(listener); 445 446 } 447 448 457 public void start() throws LifecycleException { 458 459 if (started) 461 throw new LifecycleException(sm 462 .getString("jvmRoute.valve.alreadyStarted")); 463 lifecycle.fireLifecycleEvent(START_EVENT, null); 464 started = true; 465 Container container = getContainer().getParent(); 466 if (container instanceof Host 467 && ((Host) container).getCluster() != null) 468 cluster = (CatalinaCluster) ((Host) container).getCluster(); 469 if (cluster == null) { 470 throw new RuntimeException ("No clustering support at container " 471 + container.getName()); 472 } 473 474 if (log.isInfoEnabled()) 475 log.info(sm.getString("jvmRoute.valve.started")); 476 477 } 478 479 488 public void stop() throws LifecycleException { 489 490 if (!started) 492 throw new LifecycleException(sm 493 .getString("jvmRoute.valve.notStarted")); 494 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 495 started = false; 496 497 cluster = null; 498 manager = null; 499 managerCheck = false; 500 501 numberOfSessions = 0; 502 if (log.isInfoEnabled()) 503 log.info(sm.getString("jvmRoute.valve.stopped")); 504 505 } 506 507 } | Popular Tags |