jfclere     2002/12/09 07:57:43

  Modified:    catalina/src/share/org/apache/catalina Manager.java
               catalina/src/share/org/apache/catalina/session
                        FileStore.java JDBCStore.java ManagerBase.java
                        PersistentManagerBase.java
  Added:       catalina/src/share/org/apache/catalina/valves
                        PersistentValve.java
  Log:
  Port the change I made in the 4.1 (5.0 is the place for new things 4.1 is not).
  
  Revision  Changes    Path
  1.3       +10 -4     
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/Manager.java
  
  Index: Manager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/Manager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Manager.java      20 Sep 2002 21:22:31 -0000      1.2
  +++ Manager.java      9 Dec 2002 15:57:43 -0000       1.3
  @@ -182,6 +182,12 @@
        */
       public void addPropertyChangeListener(PropertyChangeListener listener);
   
  +    /**
  +     * Get a session from the recycled ones or create a new empty one.
  +     * The PersistentManager manager does not need to create session data
  +     * because it reads it from the Store.
  +     */                                                                         
  +    public Session createEmptySession();
   
       /**
        * Construct and return a new session object, based on the default
  
  
  
  1.2       +9 -5      
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/FileStore.java
  
  Index: FileStore.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/FileStore.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FileStore.java    18 Jul 2002 16:47:51 -0000      1.1
  +++ FileStore.java    9 Dec 2002 15:57:43 -0000       1.2
  @@ -290,6 +290,10 @@
           if (file == null) {
               return (null);
           }
  +
  +        if (! file.exists()) {
  +            return (null);
  +        }
           if (debug >= 1) {
               log(sm.getString(getStoreName()+".loading",
                                id, file.getAbsolutePath()));
  @@ -329,7 +333,7 @@
   
           try {
               StandardSession session =
  -                (StandardSession) manager.createSession();
  +                (StandardSession) manager.createEmptySession();
               session.readObjectData(ois);
               session.setManager(manager);
               return (session);
  
  
  
  1.3       +5 -5      
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/JDBCStore.java
  
  Index: JDBCStore.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/JDBCStore.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JDBCStore.java    8 Dec 2002 13:42:10 -0000       1.2
  +++ JDBCStore.java    9 Dec 2002 15:57:43 -0000       1.3
  @@ -538,7 +538,7 @@
   
           if(ois != null) {
               try {
  -                _session = (StandardSession) manager.createSession();
  +                _session = (StandardSession) manager.createEmptySession();
                   _session.readObjectData(ois);
                   _session.setManager(manager);
               } finally {
  
  
  
  1.5       +27 -17    
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/ManagerBase.java
  
  Index: ManagerBase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/ManagerBase.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ManagerBase.java  5 Dec 2002 13:52:51 -0000       1.4
  +++ ManagerBase.java  9 Dec 2002 15:57:43 -0000       1.5
  @@ -600,18 +600,7 @@
       public Session createSession() {
   
           // Recycle or create a Session instance
  -        Session session = null;
  -        synchronized (recycled) {
  -            int size = recycled.size();
  -            if (size > 0) {
  -                session = (Session) recycled.get(size - 1);
  -                recycled.remove(size - 1);
  -            }
  -        }
  -        if (session != null)
  -            session.setManager(this);
  -        else
  -            session = new StandardSession(this);
  +        Session session = createEmptySession();
   
           // Initialize the properties of the new session and return it
           session.setNew(true);
  @@ -623,7 +612,6 @@
           // @todo Move appending of jvmRoute generateSessionId()???
           if (jvmRoute != null) {
               sessionId += '.' + jvmRoute;
  -            session.setId(sessionId);
           }
           /*
           synchronized (sessions) {
  @@ -635,6 +623,28 @@
   
           return (session);
   
  +    }
  +
  +
  +    /**
  +     * Get a session from the recycled ones or create a new empty one.
  +     * The PersistentManager manager does not need to create session data
  +     * because it reads it from the Store.
  +     */
  +    public Session createEmptySession() {
  +        Session session = null;
  +        synchronized (recycled) {
  +            int size = recycled.size();
  +            if (size > 0) {
  +                session = (Session) recycled.get(size - 1);
  +                recycled.remove(size - 1);
  +            }
  +        }
  +        if (session != null)
  +            session.setManager(this);
  +        else
  +            session = new StandardSession(this);
  +        return(session);
       }
   
   
  
  
  
  1.5       +14 -4     
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/PersistentManagerBase.java
  
  Index: PersistentManagerBase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/PersistentManagerBase.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PersistentManagerBase.java        25 Nov 2002 19:58:34 -0000      1.4
  +++ PersistentManagerBase.java        9 Dec 2002 15:57:43 -0000       1.5
  @@ -666,6 +666,15 @@
   
       }
   
  +    /**
  +     * Remove this Session from the active Sessions for this Manager,
  +     * but not from the Store. (Used by the PersistentValve)
  +     *
  +     * @param session Session to be removed
  +     */
  +    public void removeSuper(Session session) {
  +        super.remove (session);
  +    }
   
       /**
        * Load all sessions found in the persistence mechanism, assuming
  @@ -844,6 +853,7 @@
               log(sm.getString("persistentManager.swapIn", id));
   
           session.setManager(this);
  +        session.setId(id); // To make sure the listener knows about it.
           add(session);
           ((StandardSession)session).activate();
   
  
  
  
  1.1                  
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/valves/PersistentValve.java
  
  Index: PersistentValve.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/valves/PersistentValve.java,v
 1.1 2002/12/09 15:57:43 jfclere Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/09 15:57:43 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.valves;
  
  
  import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import org.apache.catalina.Container;
  import org.apache.catalina.Context;
  import org.apache.catalina.Manager;
  import org.apache.catalina.Request;
  import org.apache.catalina.Response;
  import org.apache.catalina.Session;
  import org.apache.catalina.ValveContext;
  import org.apache.catalina.util.StringManager;
  import org.apache.catalina.valves.ValveBase;
  import org.apache.catalina.Logger;
  import org.apache.catalina.core.StandardHost;
  import org.apache.catalina.Store;
  import org.apache.catalina.session.PersistentManager;
  import org.apache.catalina.session.ManagerBase;
  
  
  /**
   * Valve that implements the default basic behavior for the
   * <code>StandardHost</code> container implementation.
   * <p>
   * <b>USAGE CONSTRAINT</b>: To work correctly it requires a  PersistentManager.
   *
   * @author Jean-Frederic Clere
   * @version $Revision: 1.1 $ $Date: 2002/12/09 15:57:43 $
   */
  
  public class PersistentValve
      extends ValveBase {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The descriptive information related to this implementation.
       */
      private static final String info =
          "org.apache.catalina.valves.PersistentValve/1.0";
  
  
      /**
       * The string manager for this package.
       */
      private static final StringManager sm =
          StringManager.getManager(Constants.Package);
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * Return descriptive information about this Valve implementation.
       */
      public String getInfo() {
  
          return (info);
  
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Select the appropriate child Context to process this request,
       * based on the specified request URI.  If no matching Context can
       * be found, return an appropriate HTTP error.
       *
       * @param request Request to be processed
       * @param response Response to be produced
       * @param valveContext Valve context used to forward to the next Valve
       *
       * @exception IOException if an input/output error occurred
       * @exception ServletException if a servlet error occurred
       */
      public void invoke(Request request, Response response,
                         ValveContext valveContext)
          throws IOException, ServletException {
  
          // Validate the request and response object types
          if (!(request.getRequest() instanceof HttpServletRequest) ||
              !(response.getResponse() instanceof HttpServletResponse)) {
              return;     // NOTE - Not much else we can do generically
          }
  
          // Select the Context to be used for this Request
          StandardHost host = (StandardHost) getContainer();
          Context context = (Context) host.map(request, true);
          if (context == null) {
              ((HttpServletResponse) response.getResponse()).sendError
                  (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                   sm.getString("standardHost.noContext"));
              return;
          }
  
          // Bind the context CL to the current thread
          Thread.currentThread().setContextClassLoader
              (context.getLoader().getClassLoader());
  
          // Update the session last access time for our session (if any)
          HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
          String sessionId = hreq.getRequestedSessionId();
          Manager manager = context.getManager();
          if (sessionId != null && manager != null) {
              if (manager instanceof PersistentManager) {
                  Store store = ((PersistentManager) manager).getStore();
                  if (store != null) {
                      Session session = null;
                      try {
                          session = store.load(sessionId);
                      } catch (Exception e) {
                          log("deserializeError");
                      }
                      if (session != null) {
                          if (!session.isValid() ||
                              isSessionStale(session, System.currentTimeMillis())) {
                              log("session swapped in is invalid or expired");
                              session.expire();
                              store.remove(sessionId);
                          } else {
                              session.setManager(manager);
                              // session.setId(sessionId); Only if new ???
                              manager.add(session);
                              // ((StandardSession)session).activate();
                              session.access();
                          }
                      }
                  }
              }
          }
          log("sessionId: " + sessionId);
  
          // Ask this Context to process this request
          context.invoke(request, response);
          // ? valveContext.invokeNext(request, response);
  
          // Read the sessionid after the response.
          // HttpSession hsess = hreq.getSession(false);
          HttpSession hsess;
          try {
              hsess = hreq.getSession();
          } catch (Exception ex) {
              hsess = null;
          }
          String newsessionId = null;
          if (hsess!=null)
              newsessionId = hsess.getId();
  
          log("newsessionId: " + newsessionId);
          if (newsessionId!=null) {
              /* store the session in the store and remove it from the manager */
              if (manager instanceof PersistentManager) {
                  Session session = manager.findSession(newsessionId);
                  Store store = ((PersistentManager) manager).getStore();
                  if (store != null && session!=null &&
                      session.isValid() &&
                      !isSessionStale(session, System.currentTimeMillis())) {
                      // ((StandardSession)session).passivate();
                      store.save(session);
                      ((PersistentManager) manager).removeSuper(session);
                      session.recycle();
                  } else {
                      log("newsessionId store: " + store + " session: " +
                          session + " valid: " + session.isValid() +
                          " Staled: " +
                          isSessionStale(session, System.currentTimeMillis()));
  
                  }
              } else {
                  log("newsessionId Manager: " + manager);
              }
          }
      }
  
      /**
       * Log a message on the Logger associated with our Container (if any).
       *
       * @param message Message to be logged
       */
      protected void log(String message) {
   
          Logger logger = container.getLogger();
          if (logger != null)
              logger.log(this.toString() + ": " + message);
          else
              System.out.println(this.toString() + ": " + message);
   
      }
  
      /**
       * Indicate whether the session has been idle for longer
       * than its expiration date as of the supplied time.
       *
       * FIXME: Probably belongs in the Session class.
       */
      protected boolean isSessionStale(Session session, long timeNow) {
   
          int maxInactiveInterval = session.getMaxInactiveInterval();
          if (maxInactiveInterval >= 0) {
              int timeIdle = // Truncate, do not round up
                  (int) ((timeNow - session.getLastAccessedTime()) / 1000L);
              if (timeIdle >= maxInactiveInterval)
                  return true;
          }
   
          return false;
   
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to