[Jboss-cvs] JBossAS SVN: r55046 - trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/session

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Aug 2 17:13:59 EDT 2006


Author: bstansberry at jboss.com
Date: 2006-08-02 17:13:58 -0400 (Wed, 02 Aug 2006)
New Revision: 55046

Added:
   trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/session/SessionReplicationContext.java
Log:
[JBAS-3318] Replicate all sessions affected by a cross-context call

Added: trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/session/SessionReplicationContext.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/session/SessionReplicationContext.java	2006-08-02 21:13:27 UTC (rev 55045)
+++ trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/session/SessionReplicationContext.java	2006-08-02 21:13:58 UTC (rev 55046)
@@ -0,0 +1,155 @@
+package org.jboss.web.tomcat.tc6.session;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.Stack;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.catalina.connector.Request;
+import org.apache.catalina.connector.Response;
+
+public final class SessionReplicationContext
+{
+   private String sessionId;
+   private SnapshotManager snapshot;   
+   
+   private static final ThreadLocal contextStack = new ThreadLocal();
+   private static final ThreadLocal requestThreadLocal = new ThreadLocal();
+   private static final ThreadLocal responseThreadLocal = new ThreadLocal();
+   
+   public static void initContext(Request request, Response response)
+   {
+      Stack stack = (Stack) contextStack.get();
+      if (stack == null)
+      {
+         stack = new Stack();
+         contextStack.set(stack);
+      }
+      stack.push(new LinkedHashSet());
+      
+      // We only store the outermost request if there are nested calls
+      // to this method in the same request
+      if (stack.size() == 1)
+      {
+         requestThreadLocal.set(request);
+         responseThreadLocal.set(response);
+      }
+   }
+   
+   public static SessionReplicationContext[] getActiveSessions()
+   {
+      Stack stack = (Stack) contextStack.get();
+      Set set = (Set) stack.peek();
+      
+      SessionReplicationContext[] result = new SessionReplicationContext[set.size()];
+      return (SessionReplicationContext[]) set.toArray(result);
+   }
+   
+   public static void clearContext()
+   {
+      Stack stack = (Stack) contextStack.get();
+      stack.pop();
+
+      // If this is the last in the stack, clear the request and response
+      if (stack.size() == 0)
+      {
+         requestThreadLocal.set(null);
+         responseThreadLocal.set(null);
+         // TODO consider leaving the stack around
+         contextStack.set(null);
+      }
+   }
+   
+   public static void addActiveSession(String realId, SnapshotManager manager)
+   {
+      Stack stack = (Stack) contextStack.get();
+      if (stack != null)
+      {
+         Set set = (Set) stack.peek();
+         if (set != null)
+            set.add(new SessionReplicationContext(realId, manager));
+      }
+   }
+   
+   public static boolean isSessionActive(String realId, SnapshotManager manager)
+   {
+      boolean result = false;
+      Stack stack = (Stack) contextStack.get();
+      if (stack != null)
+      {
+         Set set = (Set) stack.peek();
+         result = set.contains(new SessionReplicationContext(realId, manager));
+      }
+      return result;
+   }
+   
+   public static Request getOriginalRequest()
+   {
+      return (Request) requestThreadLocal.get();
+   }
+   
+   public static Response getOriginalResponse()
+   {
+      return (Response) responseThreadLocal.get();
+   }
+   
+   // Prevent external instantiation
+   private SessionReplicationContext(String sessionId, SnapshotManager manager) 
+   {
+      this.sessionId = sessionId;
+      this.snapshot = manager;
+   }
+
+   public SnapshotManager getSnapshot()
+   {
+      return snapshot;
+   }
+
+   public String getSessionId()
+   {
+      return sessionId;
+   }
+
+   public boolean equals(Object obj)
+   {
+      if (this == obj)
+         return true;
+      
+      if (obj instanceof SessionReplicationContext)
+      {
+         SessionReplicationContext other = (SessionReplicationContext) obj;
+         // Test the snapshot first as it is more likely to be unique
+         return (this.snapshot.equals(other.snapshot) 
+                 && this.sessionId.equals(other.sessionId));
+      }
+      
+      return false;
+   }
+
+   /**
+    * Returns the {@link #getSnapshot snapshot}'s hashcode, as it should be sufficient 
+    * to minimize bucket collisions; for the same request there shouldn't be more 
+    * than one session per snapshot.
+    */
+   public int hashCode()
+   {
+      return snapshot.hashCode();
+   }
+
+   public String toString()
+   {
+      StringBuffer sb = new StringBuffer(getClass().getName());
+      sb.append("{sessionid=");
+      sb.append(sessionId);
+      sb.append("snapshot=");
+      sb.append(snapshot);
+      sb.append("}");
+      return sb.toString();
+   }
+   
+   
+   
+   
+   
+}




More information about the jboss-cvs-commits mailing list