Author: remy.maucherat(a)jboss.com
Date: 2009-10-18 11:12:57 -0400 (Sun, 18 Oct 2009)
New Revision: 1206
Modified:
trunk/java/org/apache/catalina/Session.java
trunk/java/org/apache/catalina/core/ApplicationDispatcher.java
trunk/java/org/apache/catalina/core/ApplicationHttpRequest.java
trunk/java/org/apache/catalina/session/StandardSession.java
Log:
- Add code to associate cross context sessions together. Bleh.
- Improve types.
Modified: trunk/java/org/apache/catalina/Session.java
===================================================================
--- trunk/java/org/apache/catalina/Session.java 2009-10-17 10:50:39 UTC (rev 1205)
+++ trunk/java/org/apache/catalina/Session.java 2009-10-18 15:12:57 UTC (rev 1206)
@@ -251,6 +251,12 @@
*/
public void expire();
+
+ /**
+ * Add context name in which there is an associated session.
+ */
+ public void addAssociatedSession(String path);
+
/**
* Return the object bound with the specified name to the internal notes
Modified: trunk/java/org/apache/catalina/core/ApplicationDispatcher.java
===================================================================
--- trunk/java/org/apache/catalina/core/ApplicationDispatcher.java 2009-10-17 10:50:39 UTC
(rev 1205)
+++ trunk/java/org/apache/catalina/core/ApplicationDispatcher.java 2009-10-18 15:12:57 UTC
(rev 1206)
@@ -928,6 +928,7 @@
// Compute a crossContext flag
HttpServletRequest hcurrent = (HttpServletRequest) current;
boolean crossContext = false;
+ String originalContextPath = null;
if (state.outerRequest instanceof HttpServletRequest) {
HttpServletRequest houterRequest =
(HttpServletRequest) state.outerRequest;
@@ -938,9 +939,12 @@
contextPath = houterRequest.getContextPath();
}
crossContext = !(context.getPath().equals(contextPath));
+ if (crossContext && contextPath != null) {
+ originalContextPath = (String) contextPath;
+ }
}
wrapper = new ApplicationHttpRequest
- (hcurrent, context, crossContext);
+ (hcurrent, context, crossContext, originalContextPath);
} else {
wrapper = new ApplicationRequest(current);
}
Modified: trunk/java/org/apache/catalina/core/ApplicationHttpRequest.java
===================================================================
--- trunk/java/org/apache/catalina/core/ApplicationHttpRequest.java 2009-10-17 10:50:39
UTC (rev 1205)
+++ trunk/java/org/apache/catalina/core/ApplicationHttpRequest.java 2009-10-18 15:12:57
UTC (rev 1206)
@@ -33,6 +33,7 @@
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
+import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Globals;
import org.apache.catalina.Session;
@@ -95,11 +96,12 @@
* @param request The servlet request being wrapped
*/
public ApplicationHttpRequest(HttpServletRequest request, Context context,
- boolean crossContext) {
+ boolean crossContext, String originalContextPath) {
super(request);
this.context = context;
this.crossContext = crossContext;
+ this.originalContextPath = originalContextPath;
setRequest(request);
}
@@ -112,6 +114,12 @@
* The context for this request.
*/
protected Context context = null;
+
+
+ /**
+ * If cross context, the original context path.
+ */
+ protected String originalContextPath = null;
/**
@@ -535,6 +543,22 @@
if (localSession == null && create) {
localSession =
context.getManager().createSession(other.getId());
+ // Associate the two sessions if possible
+ if (originalContextPath != null) {
+ Container otherContext =
context.getParent().findChild(originalContextPath);
+ if (otherContext != null) {
+ try {
+ Session otherSession =
otherContext.getManager().findSession(other.getId());
+ if (otherSession != null) {
+
localSession.setMaxInactiveInterval(otherSession.getMaxInactiveInterval());
+
otherSession.addAssociatedSession(context.getPath());
+
localSession.addAssociatedSession(originalContextPath);
+ }
+ } catch (Exception e) {
+ // Ignore
+ }
+ }
+ }
}
if (localSession != null) {
localSession.access();
Modified: trunk/java/org/apache/catalina/session/StandardSession.java
===================================================================
--- trunk/java/org/apache/catalina/session/StandardSession.java 2009-10-17 10:50:39 UTC
(rev 1205)
+++ trunk/java/org/apache/catalina/session/StandardSession.java 2009-10-18 15:12:57 UTC
(rev 1206)
@@ -30,7 +30,6 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
-import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -46,6 +45,7 @@
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
+import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Globals;
import org.apache.catalina.Manager;
@@ -82,6 +82,9 @@
implements HttpSession, Session, Serializable {
+ private static final long serialVersionUID = -4294597583262209053L;
+
+
protected static final boolean ACTIVITY_CHECK =
Globals.STRICT_SERVLET_COMPLIANCE
||
Boolean.valueOf(System.getProperty("org.apache.catalina.session.StandardSession.ACTIVITY_CHECK",
"false")).booleanValue();
@@ -128,7 +131,7 @@
/**
* The collection of user data attributes associated with this Session.
*/
- protected Map attributes = new ConcurrentHashMap();
+ protected Map<String, Object> attributes = new ConcurrentHashMap<String,
Object>();
/**
@@ -190,7 +193,7 @@
/**
* The session event listeners for this Session.
*/
- protected transient ArrayList listeners = new ArrayList();
+ protected transient ArrayList<SessionListener> listeners = new
ArrayList<SessionListener>();
/**
@@ -224,7 +227,7 @@
* and event listeners. <b>IMPLEMENTATION NOTE:</b> This object is
* <em>not</em> saved and restored across session serializations!
*/
- protected transient Map notes = new Hashtable();
+ protected transient Map<String, Object> notes = new
ConcurrentHashMap<String, Object>();
/**
@@ -260,6 +263,13 @@
protected transient AtomicInteger accessCount = null;
+ /**
+ * Associated sessions in other contexts.
+ */
+ protected transient Map<String, Object> associatedSessions = null;
+ protected static final Object TOKEN = new Object();
+
+
// ----------------------------------------------------- Session Properties
@@ -487,6 +497,24 @@
if (isValid && interval == 0) {
expire();
}
+
+ // Expire any associated session
+ if (associatedSessions != null) {
+ for (String path : associatedSessions.keySet()) {
+ Container crossContainer =
manager.getContainer().getParent().findChild(path);
+ if (crossContainer != null) {
+ Manager crossManager = crossContainer.getManager();
+ try {
+ Session associatedSession = crossManager.findSession(id);
+ if (associatedSession instanceof StandardSession) {
+ ((StandardSession) associatedSession).maxInactiveInterval =
interval;
+ }
+ } catch (Exception e) {
+ // Ignore ...
+ }
+ }
+ }
+ }
}
@@ -648,7 +676,18 @@
}
+
+ /**
+ * Add context name in which there is an associated session.
+ */
+ public void addAssociatedSession(String path) {
+ if (associatedSessions == null) {
+ associatedSessions = new ConcurrentHashMap<String, Object>();
+ }
+ associatedSessions.put(path, TOKEN);
+ }
+
/**
* Perform the internal processing required to invalidate this session,
* without triggering an exception if the session has already expired.
@@ -737,6 +776,22 @@
}
}
+ // Expire any associated session
+ if (associatedSessions != null) {
+ for (String path : associatedSessions.keySet()) {
+ Container crossContainer =
manager.getContainer().getParent().findChild(path);
+ if (crossContainer != null) {
+ Manager crossManager = crossContainer.getManager();
+ try {
+ Session associatedSession = crossManager.findSession(id);
+ associatedSession.expire();
+ } catch (Exception e) {
+ // Ignore ...
+ }
+ }
+ }
+ }
+
// We have completed expire of this session
expiring = false;
@@ -857,6 +912,7 @@
setPrincipal(null);
isNew = false;
isValid = false;
+ associatedSessions = null;
manager = null;
}
@@ -1418,7 +1474,7 @@
// Deserialize the attribute count and attribute values
if (attributes == null)
- attributes = new Hashtable();
+ attributes = new ConcurrentHashMap<String, Object>();
int n = ((Integer) stream.readObject()).intValue();
boolean isValidSave = isValid;
isValid = true;
@@ -1432,11 +1488,11 @@
isValid = isValidSave;
if (listeners == null) {
- listeners = new ArrayList();
+ listeners = new ArrayList<SessionListener>();
}
if (notes == null) {
- notes = new Hashtable();
+ notes = new ConcurrentHashMap<String, Object>();
}
}