Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 package org.apache.catalina.valves;
 
 
 import static org.jboss.web.CatalinaMessages.MESSAGES;
 
 
 
Valve that implements per-request session persistence. It is intended to be used with non-sticky load-balancers.

USAGE CONSTRAINT: To work correctly it requires a PersistentManager.

USAGE CONSTRAINT: To work correctly it assumes only one request exists per session at any one time.

Author(s):
Jean-Frederic Clere
Version:
$Revision: 1125 $ $Date: 2009-06-22 16:52:50 +0200 (Mon, 22 Jun 2009) $
 
 
 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";
 
 
     // ------------------------------------------------------------- Properties
 

    
Return descriptive information about this Valve implementation.
 
     public String getInfo() {
 
         return ();
 
     }
 
 
     // --------------------------------------------------------- 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.

Parameters:
request Request to be processed
response Response to be produced
Throws:
java.io.IOException if an input/output error occurred
javax.servlet.ServletException if a servlet error occurred
 
     public void invoke(Request requestResponse response)
         throws IOExceptionServletException {
 
         // Select the Context to be used for this Request
         Context context = request.getContext();
         if (context == null) {
             response.sendError(.,
                    .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)
        String sessionId = request.getRequestedSessionId();
        Manager manager = context.getManager();
        if (sessionId != null && manager != null) {
            if (manager instanceof PersistentManager) {
                Store store = ((PersistentManagermanager).getStore();
                if (store != null) {
                    Session session = null;
                    try {
                        session = store.load(sessionId);
                    } catch (Exception e) {
                        ..deserializationError(e);
                    }
                    if (session != null) {
                        if (!session.isValid() ||
                            isSessionStale(session, System.currentTimeMillis())) {
                            if (.getLogger().isDebugEnabled())
                                .getLogger().debug("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();
                            session.endAccess();
                        }
                    }
                }
            }
        }
        if (.getLogger().isDebugEnabled())
            .getLogger().debug("sessionId: " + sessionId);
        // Ask the next valve to process the request.
        getNext().invoke(requestresponse);
        // Read the sessionid after the response.
        // HttpSession hsess = hreq.getSession(false);
        Session hsess;
        try {
            hsess = request.getSessionInternal();
        } catch (Exception ex) {
            hsess = null;
        }
        String newsessionId = null;
        if (hsess!=null)
            newsessionId = hsess.getIdInternal();
        if (.getLogger().isDebugEnabled())
            .getLogger().debug("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 = ((PersistentManagermanager).getStore();
                if (store != null && session!=null &&
                    session.isValid() &&
                    !isSessionStale(session, System.currentTimeMillis())) {
                    // ((StandardSession)session).passivate();
                    store.save(session);
                    ((PersistentManagermanager).removeSuper(session);
                    session.recycle();
                } else {
                    if (.getLogger().isDebugEnabled())
                        .getLogger().debug("newsessionId store: " + store + " session: " +
                                session + " valid: " + session.isValid() +
                                " Staled: " +
                                isSessionStale(session, System.currentTimeMillis()));
                }
            } else {
                if (.getLogger().isDebugEnabled())
                    .getLogger().debug("newsessionId Manager: " + manager);
            }
        }
    }

    
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 sessionlong 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;
 
    }
New to GrepCode? Check out our FAQ X