[jboss-cvs] jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer ...

Chris Laprun chris.laprun at jboss.com
Sat Aug 19 03:08:12 EDT 2006


  User: claprun 
  Date: 06/08/19 03:08:11

  Modified:    wsrp/src/main/org/jboss/portal/wsrp/consumer  
                        SessionHandler.java ProducerSessionInformation.java
  Log:
  - Added management of session expiration.
  - Minor improvements.
  
  Revision  Changes    Path
  1.7       +3 -7      jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/SessionHandler.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: SessionHandler.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/SessionHandler.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -b -r1.6 -r1.7
  --- SessionHandler.java	19 Aug 2006 03:24:32 -0000	1.6
  +++ SessionHandler.java	19 Aug 2006 07:08:11 -0000	1.7
  @@ -44,7 +44,7 @@
    * Manages session informations on behalf of a consumer.
    *
    * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
  - * @version $Revision: 1.6 $
  + * @version $Revision: 1.7 $
    * @since 2.4 (May 31, 2006)
    */
   public class SessionHandler
  @@ -57,7 +57,6 @@
   
      /** The prefix used to isolate WSRP-related session information in the actual session object. */
      private static final String SESSION_ID_PREFIX = "org.jboss.portal.wsrp.session.";
  -   private static final String SESSION_ID = "session id";
   
      /**
       * Constructs a new SessionHandler.
  @@ -205,11 +204,8 @@
      {
         if (sessionContext != null)
         {
  -         String sessionId = sessionContext.getSessionID();
  -         ParameterValidation.throwIllegalArgExceptionIfNull(sessionId, SESSION_ID);
            ProducerSessionInformation sessionInfo = getProducerSessionInformation(invocation);
  -         sessionInfo.addSessionIdForPortlet(WSRPConsumerImpl.getPortletHandle(invocation), sessionId);
  -         // sessionContext.getExpires(); // todo: manage expiration
  +         sessionInfo.addSessionForPortlet(WSRPConsumerImpl.getPortletHandle(invocation), sessionContext);
         }
      }
   
  @@ -253,7 +249,7 @@
         // invalidate current session
         invalidateSession(invocation);
   
  -      // fix the request todo: there is more to do here, check the spec!
  +      // set the session id to null
         runtimeContext.setSessionID(null);
      }
   
  
  
  
  1.3       +78 -13    jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/ProducerSessionInformation.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ProducerSessionInformation.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/consumer/ProducerSessionInformation.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- ProducerSessionInformation.java	30 Jun 2006 21:05:03 -0000	1.2
  +++ ProducerSessionInformation.java	19 Aug 2006 07:08:11 -0000	1.3
  @@ -22,6 +22,11 @@
   
   package org.jboss.portal.wsrp.consumer;
   
  +import org.jboss.logging.Logger;
  +import org.jboss.portal.common.util.ParameterValidation;
  +import org.jboss.portal.wsrp.WSRPConstants;
  +import org.jboss.portal.wsrp.core.SessionContext;
  +
   import java.util.HashMap;
   import java.util.Map;
   
  @@ -29,15 +34,22 @@
    * Records session and cookie information for a producer.
    *
    * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    * @since 2.4 (May 30, 2006)
    */
   public class ProducerSessionInformation
   {
  +   private Logger log = Logger.getLogger(ProducerSessionInformation.class);
  +
      private boolean userCookiesInitialized = false;
      private boolean perGroupCookies = false;
  +
  +   /** group id -> cookie */
      private Map groupCookies;
  +
  +   /** portlet handle -> SessionInfo */
      private Map portletSessions;
  +
      private String userCookie;
   
      public String getUserCookie()
  @@ -125,35 +137,41 @@
         groupCookies = null;
      }
   
  -   public void addSessionIdForPortlet(String portletHandle, String sessionId)
  +   public void addSessionForPortlet(String portletHandle, SessionContext sessionContext)
      {
  -      if (portletHandle == null || sessionId == null)
  -      {
  -         throw new IllegalArgumentException("Cannot add sessionId: '" + sessionId + "' for portlet id: '"
  -            + portletHandle + "'");
  -      }
  +      ParameterValidation.throwIllegalArgExceptionIfNull(portletHandle, "portlet handle");
   
         if (portletSessions == null)
         {
            portletSessions = new HashMap();
         }
   
  -      portletSessions.put(portletHandle, sessionId);
  +      portletSessions.put(portletHandle, new SessionInfo(sessionContext));
      }
   
  +   /**
  +    * Retrieves the session id for the portlet with the specified handle. Note that this will "touch" the session, hence
  +    * resetting the time since the last use of the session.
  +    *
  +    * @param portletHandle the handle of the portlet for which the session id is to be retrieved
  +    * @return the session id for the specified portlet, <code>null</code> if there is no session associated with the
  +    *         portlet or if the session has expired.
  +    */
      public String getSessionIdForPortlet(String portletHandle)
      {
  -      if (portletHandle == null)
  -      {
  -         throw new IllegalArgumentException("Cannot get sessionId for null portlet handle");
  -      }
  +      ParameterValidation.throwIllegalArgExceptionIfNull(portletHandle, "portlet handle");
   
         if (portletSessions == null)
         {
            return null;
         }
   
  -      return (String)portletSessions.get(portletHandle);
  +      SessionInfo session = (SessionInfo)portletSessions.get(portletHandle);
  +      if (session != null)
  +      {
  +         return session.getSessionId();
  +      }
  +      return null;
      }
   
      public void removeSessionIdForPortlet(String portletHandle)
  @@ -163,4 +181,51 @@
            portletSessions.remove(portletHandle);
         }
      }
  +
  +   private class SessionInfo
  +   {
  +      private SessionContext sessionContext;
  +      private long lastInvocationTime;
  +
  +      public SessionInfo(SessionContext sessionContext)
  +      {
  +         ParameterValidation.throwIllegalArgExceptionIfNull(sessionContext, "SessionContext");
  +         ParameterValidation.throwIllegalArgExceptionIfNull(sessionContext.getSessionID(), "session id");
  +
  +         this.sessionContext = sessionContext;
  +         lastInvocationTime = System.currentTimeMillis();
  +      }
  +
  +      /**
  +       * Checks that the session associated with the session context hasn't expired and update the last invocation time
  +       *
  +       * @return
  +       */
  +      private boolean isStillValid()
  +      {
  +         int expires = sessionContext.getExpires();
  +         if (expires == WSRPConstants.SESSION_NEVER_EXPIRES)
  +         {
  +            return true;
  +         }
  +
  +         long now = System.currentTimeMillis();
  +         long secondsSinceLastInvocation = (now - lastInvocationTime) / 1000;
  +         lastInvocationTime = now;
  +
  +         long diff = expires - secondsSinceLastInvocation;
  +         log.debug("Session ID '" + sessionContext.getSessionID() + "' is " + ((diff > 0) ? "" : "not")
  +            + " valid (time since last invocation: " + diff + ")");
  +         return diff > 0;
  +      }
  +
  +      public String getSessionId()
  +      {
  +         if (isStillValid())
  +         {
  +            return sessionContext.getSessionID();
  +         }
  +         return null;
  +      }
  +   }
   }
  
  
  



More information about the jboss-cvs-commits mailing list