You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@locus.apache.org on 2000/01/09 23:32:43 UTC
cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/session ServerSessionManager.java StandardSessionManager.java
costin 00/01/09 14:32:43
Modified: src/share/org/apache/tomcat/core Constants.java Request.java
SessionManager.java
src/share/org/apache/tomcat/request SessionInterceptor.java
src/share/org/apache/tomcat/session
ServerSessionManager.java
StandardSessionManager.java
Log:
One step towards Craig's design for sessions.
The session manager no longer needs to process the request, it
acts just as a repository.
The request processing ( extracting the session Id) is done by
an interceptor. The other way - setting the response after a new
session is created should also be in a post-service interceptor.
( this way you can configure SSL sessions or URL rewriting without
touching DbManager )
Revision Changes Path
1.10 +3 -1 jakarta-tomcat/src/share/org/apache/tomcat/core/Constants.java
Index: Constants.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Constants.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Constants.java 2000/01/08 22:55:45 1.9
+++ Constants.java 2000/01/09 22:32:42 1.10
@@ -80,7 +80,9 @@
public static final String ATTRIB_WORKDIR1 = "sun.servlet.workdir";
public static final String ATTRIB_WORKDIR = "javax.servlet.context.tempdir";
-
+ public static final String SESSION_COOKIE_NAME = "JSESSIONID";
+ public static final String SESSION_PARAMETER_NAME = "jsessionid";
+
public static final String Package = "org.apache.tomcat.core";
public static final int RequestURIMatchRecursion = 5;
public static final String WORK_DIR = "work";
1.15 +44 -42 jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java
Index: Request.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- Request.java 2000/01/08 21:31:39 1.14
+++ Request.java 2000/01/09 22:32:42 1.15
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java,v 1.14 2000/01/08 21:31:39 rubys Exp $
- * $Revision: 1.14 $
- * $Date: 2000/01/08 21:31:39 $
+ * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java,v 1.15 2000/01/09 22:32:42 costin Exp $
+ * $Revision: 1.15 $
+ * $Date: 2000/01/09 22:32:42 $
*
* ====================================================================
*
@@ -96,7 +96,6 @@
protected String pathInfo;
protected Hashtable parameters = new Hashtable();
- protected String reqSessionId;
protected int contentLength = -1;
protected String contentType = null;
protected String charEncoding = null;
@@ -109,13 +108,19 @@
protected HttpServletRequestFacade requestFacade;
protected Context context;
protected Hashtable attributes = new Hashtable();
- protected HttpSession serverSession;
protected boolean didReadFormData;
protected boolean didParameters;
protected boolean didCookies;
// end "Request" variables
+ // Session
+ // set by interceptors - the session id
+ protected String reqSessionId;
+ // cache- avoid calling SessionManager for each getSession()
+ protected HttpSession serverSession;
+
+
// LookupResult - used by sub-requests and
// set by interceptors
ServletWrapper wrapper = null;
@@ -300,6 +305,10 @@
return reqSessionId;
}
+ public void setRequestedSessionId(String reqSessionId) {
+ this.reqSessionId = reqSessionId;
+ }
+
public String getServletPath() {
return servletPath;
}
@@ -393,42 +402,39 @@
// }
public HttpSession getSession(boolean create) {
- if( serverSession==null ) {
- if( ! create )
- return null;
- else {
-// serverSession =
-// ServerSessionManager.getManager()
-// .getServerSession(this, response, create);
-// serverSession.accessed();
- SessionManager sM=getContext().getSessionManager();
- serverSession =sM.getSession(this, response, create);
-
- }
- }
+ // use the cached value
+ if( serverSession!=null )
+ return serverSession;
- // assert serverSession!=null
-// ApplicationSession appSession = null;
-// return serverSession.getApplicationSession(context, create);
- return serverSession;
+ SessionManager sM=context.getSessionManager();
+
+ // if the interceptors found a request id, use it
+ if( reqSessionId != null ) {
+ // we have a session !
+ serverSession=sM.findSession( context, reqSessionId );
+ if( serverSession!=null) return serverSession;
+ }
+
+ if( ! create )
+ return null;
+ // no session exists, create flag
+ serverSession =sM.createSession( context );
+ reqSessionId = serverSession.getId();
+
+ // XXX XXX will be changed - post-request Interceptors
+ // ( to be defined) will set the session id in response,
+ // SessionManager is just a repository and doesn't deal with
+ // request internals.
+ // hardcoded - will change!
+ Cookie cookie = new Cookie(Constants.SESSION_COOKIE_NAME,
+ reqSessionId);
+ cookie.setMaxAge(-1);
+ cookie.setPath("/");
+ cookie.setVersion(1);
+ response.addSystemCookie(cookie);
-
- // if (reqSessionId != null) {
-// //Session session = context.getSession(reqSessionId);
-// //if (session == null) {
-// //session = context.createSession(reqSessionId);
-// //}
-// //return session;
-// System.out.println("DANGER, SESSIONS ARE NOT WORKING");
-// } else {
-// if (create) {
-// Session session = serverSession.createSession(response);
-// return session;
-// } else {
-// return null;
-// }
-// }
+ return serverSession;
}
// -------------------- LookupResult
@@ -589,10 +595,6 @@
*/
public void replaceQueryString(String inQueryString) {
this.queryString = inQueryString;
- }
-
- public void setRequestedSessionId(String reqSessionId) {
- this.reqSessionId = reqSessionId;
}
public void setSession(HttpSession serverSession) {
1.2 +39 -6 jakarta-tomcat/src/share/org/apache/tomcat/core/SessionManager.java
Index: SessionManager.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/SessionManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SessionManager.java 2000/01/07 21:11:38 1.1
+++ SessionManager.java 2000/01/09 22:32:42 1.2
@@ -68,22 +68,55 @@
/**
*
+ * @author Craig R. McClanahan
* @author costin@dnt.ro
*/
public interface SessionManager {
/**
- * Called by Request.getSession() to create/get a new session object.
- * May also be called by RequestInterceptors to find the session that
- * needs to be marked as accessed
+ * Construct and return a new session object, based on the default
+ * settings specified by this Manager's properties. The session
+ * id will be assigned by this method, and available via the getId()
+ * method of the returned session. If a new session cannot be created
+ * for any reason, return <code>null</code>.
+ *
+ * @param ctx The session will be created for the specified context
+ * @exception IllegalStateException if a new session cannot be
+ * instantiated for any reason
*/
- public HttpSession getSession(Request request, Response response,boolean create);
+ public HttpSession createSession(Context ctx);
+
/** Will mark the session lastAccess time.
- * Will be called for each request by a SessionInterceptor
+ * Will be called for each request that has a valid sessionId
+ *
+ * A simple SessionManager will just use the id, a complex
+ * manager may use the ctx and req to do additional work.
+ *
+ * @param ctx the context where the request belong
+ * @param req the request having the id
+ * @param id
+ */
+ public void accessed(Context ctx, Request req, String id);
+ // we can pass only Request, as it contains both, but it's better to
+ // show explicitely what this method uses.
+
+
+ /**
+ * Return the active Session, associated with this Manager, with the
+ * specified session id (if any); otherwise return <code>null</code>.
+ *
+ * @param id The session id for the session to be returned
+ * @param ctx The session needs to belong to the context
+ *
+ * @exception ClassNotFoundException if a deserialization error occurs
+ * while processing this request
+ * @exception IllegalStateException if a new session cannot be
+ * instantiated for any reason
*/
- public void accessed(HttpSession session);
+ public HttpSession findSession(Context ctx, String id);
+
/** Used by context when stoped, need to remove all sessions used by that context
*/
public void removeSessions( Context ctx );
1.3 +25 -7 jakarta-tomcat/src/share/org/apache/tomcat/request/SessionInterceptor.java
Index: SessionInterceptor.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/request/SessionInterceptor.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SessionInterceptor.java 2000/01/07 21:11:40 1.2
+++ SessionInterceptor.java 2000/01/09 22:32:43 1.3
@@ -69,6 +69,12 @@
import javax.servlet.http.*;
/**
+ * Will process the request and determine the session Id, and set it
+ * in the Request.
+ * It also marks the session as accessed.
+ *
+ * This implementation only handles Cookies sessions, please extend or
+ * add new interceptors for other methods.
*
*/
public class SessionInterceptor implements RequestInterceptor {
@@ -78,18 +84,30 @@
public int handleRequest(Request request ) {
// look for session id -- cookies only right now
+ String sessionId = null;
- SessionManager sM=request.getContext().getSessionManager();
- HttpSession session= sM.getSession(request, request.getResponse(), false);
+ Cookie cookies[]=request.getCookies(); // assert !=null
+
+ for( int i=0; i<cookies.length; i++ ) {
+ Cookie cookie = cookies[i];
+
+ if (cookie.getName().equals(
+ org.apache.tomcat.core.Constants.SESSION_COOKIE_NAME)) {
+ sessionId = cookie.getValue();
+
+ if (sessionId != null) {
+ request.setRequestedSessionId(sessionId);
+ }
+ }
+ }
- // ServerSession session =
- // sessionManager.getServerSession(request, request.getResponse(), false);
+ if (sessionId != null) {
+ Context ctx=request.getContext();
+ SessionManager sM=ctx.getSessionManager();
- if (session != null) {
- sM.accessed( session );
+ sM.accessed( ctx, request, sessionId );
}
- request.setSession(session); // may be null
return 0;
}
}
1.2 +17 -58 jakarta-tomcat/src/share/org/apache/tomcat/session/ServerSessionManager.java
Index: ServerSessionManager.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/session/ServerSessionManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ServerSessionManager.java 2000/01/07 21:11:41 1.1
+++ ServerSessionManager.java 2000/01/09 22:32:43 1.2
@@ -97,73 +97,32 @@
reaper.start();
}
- /** Called from Request.getSession to create a new session
- */
- public HttpSession getSession(Request request, Response response,
- boolean create) {
- ServerSession sSession=getServerSession( request, response, create);
- if( sSession!=null ) sSession.accessed();
- if( sSession ==null) return null;
+ public void accessed( Context ctx, Request req, String id ) {
+ ApplicationSession apS=(ApplicationSession)findSession( ctx, id);
+ if( apS==null) return;
- return sSession.getApplicationSession(request.getContext(), create);
- }
-
- public void accessed( HttpSession session ) {
- ApplicationSession apS=(ApplicationSession)session;
ServerSession servS=apS.getServerSession();
servS.accessed();
apS.accessed();
-
+
+ // cache it - no need to compute it again
+ req.setSession( apS );
}
-
- ServerSession getServerSession(Request request, Response response,
- boolean create) {
- // Look for session id -- cookies only right now
- String sessionId = null;
- ServerSession session = null;
+ public HttpSession createSession(Context ctx) {
+ String sessionId = SessionIdGenerator.generateId();
+ ServerSession session = new ServerSession(sessionId);
+ sessions.put(sessionId, session);
+ return session.getApplicationSession( ctx, true );
+ }
- // XXX need to check if request.getRequestdSessionId() returns something,
- // since the connector might have set it.
+ public HttpSession findSession(Context ctx, String id) {
+ ServerSession sSession=(ServerSession)sessions.get(id);
+ if(sSession==null) return null;
- // Enumeration enum = request.getCookies().elements();
- Cookie cookies[]=request.getCookies(); // assert !=null
-
- //while (enum.hasMoreElements()) {
- for( int i=0; i<cookies.length; i++ ) {
- Cookie cookie = cookies[i]; // (Cookie)enum.nextElement();
-
- if (cookie.getName().equals(
- Constants.SESSION_COOKIE_NAME)) {
- sessionId = cookie.getValue();
-
- if (sessionId != null) {
- request.setRequestedSessionId(sessionId);
- session = (ServerSession)sessions.get(sessionId);
- }
- }
- }
-
- if (session == null && create) {
- if (sessionId == null) {
- sessionId = SessionIdGenerator.generateId();
- Cookie cookie = new Cookie(
- Constants.SESSION_COOKIE_NAME, sessionId);
-
- cookie.setMaxAge(-1);
- cookie.setPath("/");
- cookie.setVersion(1);
-
- response.addSystemCookie(cookie);
- }
-
- session = new ServerSession(sessionId);
- sessions.put(sessionId, session);
- }
-
- return session;
+ return sSession.getApplicationSession(ctx, false);
}
-
+
// XXX
// sync'd for safty -- no other thread should be getting something
// from this while we are reaping. This isn't the most optimal
1.2 +18 -47 jakarta-tomcat/src/share/org/apache/tomcat/session/StandardSessionManager.java
Index: StandardSessionManager.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/session/StandardSessionManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StandardSessionManager.java 2000/01/09 03:23:22 1.1
+++ StandardSessionManager.java 2000/01/09 22:32:43 1.2
@@ -134,63 +134,30 @@
*
* @param session The session to be marked
*/
- public void accessed(HttpSession session) {
-
+ public void accessed(Context ctx, Request req, String id) {
+ HttpSession session=findSession(ctx, id);
+ if( session == null) return;
if (session instanceof Session)
((Session) session).access();
+ // cache the HttpSession - avoid another find
+ req.setSession( session );
}
-
-
- /**
- * Create a new session object, or get an existing one, associated with
- * the current request. This method may also be called by
- * RequestInterceptors to find the session that needs to be marked
- * as accessed. If no such session can be found and create is
- * <code>false</code>, return <code>null</code>.
- *
- * @param request The request being processed
- * @param response The response being created
- * @param create Create the session if necessary
- */
- public HttpSession getSession(Request request, Response response,
- boolean create) {
- // Look up the requested session in this request
- // XXX - Looks at cookies only right now
- // XXX - Check if request.getRequestedSessionId() returns something
- // that might have been set by a connector
- Cookie cookies[] = request.getCookies();
- String sessionId = SessionUtil.parseSessionId(cookies);
- if (sessionId != null)
- request.setRequestedSessionId(sessionId);
- Session session = null;
+ // XXX should we throw exception or just return null ??
+ public HttpSession findSession( Context ctx, String id ) {
try {
- session = manager.findSession(sessionId);
+ Session session = manager.findSession(id);
+ if(session!=null)
+ return session.getSession();
} catch (IOException e) {
- return (null);
}
-
- // Create a new session if necessary and requested
- if ((session == null) && create) {
- session = manager.createSession();
- sessionId = session.getId();
- // XXX - Cannot use manager.createCookie() because that would
- // require a Tomcat.Next style Request argument
- Cookie cookie = new Cookie
- (org.apache.tomcat.util.Constants.SESSION.COOKIE_NAME,
- sessionId);
- // XXX - cookie.setDomain() ???
- cookie.setMaxAge(-1);
- cookie.setPath("/"); // XXX - request.getContextPath() ???
- cookie.setVersion(1);
- response.addSystemCookie(cookie);
- }
-
- return ((HttpSession) session);
-
+ return (null);
}
+ public HttpSession createSession(Context ctx) {
+ return manager.createSession().getSession();
+ }
/**
* Remove all sessions because our associated Context is being shut down.
@@ -199,6 +166,10 @@
*/
public void removeSessions(Context ctx) {
+ // XXX XXX a manager may be shared by multiple
+ // contexts, we just want to remove the sessions of ctx!
+ // The manager will still run after that ( i.e. keep database
+ // connection open
if (manager instanceof Lifecycle) {
try {
((Lifecycle) manager).stop();