You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by dl...@apache.org on 2003/01/31 01:20:54 UTC

cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/services/session SessionListener.java SessionService.java TurbineSessionService.java

dlr         2003/01/30 16:20:53

  Modified:    src/java/org/apache/turbine/services/session
                        SessionListener.java SessionService.java
                        TurbineSessionService.java
  Log:
  * src/java/org/apache/turbine/services/session/SessionListener.java
    Removed imports obviated by removal of static "log" field.
  
    Added <listener> and <listener-class> web.xml examples and @since
    tag to header JavaDoc.
  
    log: Removed unused member.
  
  * src/java/org/apache/turbine/services/session/SessionService.java
    Added @since tag to header JavaDoc.
  
    getUserFromSession(HttpSession), getSession(String): Added JavaDoc,
    and removed redundant public declarations (implicit in a Java
    interface) for consistancy.
  
  * src/java/org/apache/turbine/services/session/TurbineSessionService.java
    Improved header JavaDoc.
  
    activeSessions: Changed from HashMap to Hashtable for thread-safety.
  
    getActiveSessions(): Now returns a copy of the active sessions,
    rather than a handle to the Collection view of the values of the
    actual Map used to store the active sessions.
  
    getActiveUsers(): Changed Collection implementation used to record
    active users from the slow, synchronized Vector to the faster,context-param
    un-sync'd ArrayList (pre-allocated to a reasonable storage space).
    Quinton, I couldn't see how the synchronization would be beneficial
    when each calling thread gets its own copy.  If your caller needs a
    synchronized Collection, wrap the return value using
    Collections.synchronizedCollection().
  
    As the Iterators returned by the Collection-view methods of most Map
    implementations are fail-fast, a ConcurrentModificationException
    could've been thrown if activeSessions was modified during
    iteration!  This bug is likely to show up only under high
    concurrency.  Synchronized on activeSessions while iterating to
    prevent this possibility.
  
    getUserFromSession(HttpSession), getSession(String): Added JavaDoc.
  
    isUserLoggedIn(User), getActiveUsers(): Removed unnecessary local
    variables.
  
  Revision  Changes    Path
  1.2       +16 -7     jakarta-turbine-2/src/java/org/apache/turbine/services/session/SessionListener.java
  
  Index: SessionListener.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/session/SessionListener.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -u -r1.1 -r1.2
  --- SessionListener.java	6 Jan 2003 00:34:58 -0000	1.1
  +++ SessionListener.java	31 Jan 2003 00:20:53 -0000	1.2
  @@ -57,21 +57,30 @@
   import javax.servlet.http.HttpSessionEvent;
   import javax.servlet.http.HttpSessionListener;
   
  -import org.apache.commons.logging.Log;
  -import org.apache.commons.logging.LogFactory;
  -
   /**
    * This class is a listener for Session creation and destruction.  It
  - * must be configured in your deployment descriptor for the container
  - * to call it.
  + * must be configured via your web application's <code>web.xml</code>
  + * deployment descriptor as follows for the container to call it:
    *
  + * <blockquote><code><pre>
  + * <listener>
  + *   <listener-class>
  + *     org.apache.turbine.session.SessionListener
  + *   </listener-class>
  + * </listener>
  + * </pre></code></blockquote>
  + *
  + * <code>&lt;listener&gt;</code> elemements can occur between
  + * <code>&lt;context-param&gt;</code> and <code>&lt;servlet&gt;</code>
  + * elements in your deployment descriptor.
  + *
  + * @since 2.3
  + * @version $Id$
    * @see javax.servlet.http.HttpSessionListener
    */
   public class SessionListener
           implements HttpSessionListener
   {
  -    private static Log log = LogFactory.getLog(SessionListener.class);
  -
       /**
        * Called by the servlet container when a new session is created
        *
  
  
  
  1.5       +8 -8      jakarta-turbine-2/src/java/org/apache/turbine/services/session/SessionService.java
  
  Index: SessionService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/session/SessionService.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -u -r1.4 -r1.5
  --- SessionService.java	30 Jan 2003 20:01:18 -0000	1.4
  +++ SessionService.java	31 Jan 2003 00:20:53 -0000	1.5
  @@ -66,7 +66,7 @@
    * a listener.  The listener must be configured in your web.xml file.
    *
    * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
  - * @version $Id$
  + * @since 2.3
    * @see org.apache.turbine.services.session.SessionListener
    */
   public interface SessionService extends Service
  @@ -125,18 +125,18 @@
       /**
        * Gets the User object of the the specified HttpSession.
        *
  -     * @param session
  -     * @return
  +     * @param session The session from which to extract a user.
  +     * @return The Turbine User object.
        */
  -    public User getUserFromSession(HttpSession session);
  +    User getUserFromSession(HttpSession session);
   
       /**
        * Gets the HttpSession by the session identifier
        *
  -     * @param sessionId
  -     * @return
  +     * @param sessionId The unique session identifier.
  +     * @return The session keyed by the specified identifier.
        */
  -    public HttpSession getSession(String sessionId);
  +    HttpSession getSession(String sessionId);
   
       /**
        * Get a collection of all session on which the given user
  @@ -145,6 +145,6 @@
        * @param user the user
        * @return Collection of HtttSession objects
        */
  -    public Collection getSessionsForUser(User user);
  +    Collection getSessionsForUser(User user);
   
   }
  
  
  
  1.5       +43 -30    jakarta-turbine-2/src/java/org/apache/turbine/services/session/TurbineSessionService.java
  
  Index: TurbineSessionService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/session/TurbineSessionService.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -u -r1.4 -r1.5
  --- TurbineSessionService.java	30 Jan 2003 20:01:19 -0000	1.4
  +++ TurbineSessionService.java	31 Jan 2003 00:20:53 -0000	1.5
  @@ -54,26 +54,34 @@
    * <http://www.apache.org/>.
    */
   
  +import java.util.ArrayList;
   import java.util.Collection;
  -import java.util.HashMap;
  +import java.util.Hashtable;
   import java.util.Iterator;
   import java.util.Map;
  -import java.util.Vector;
   import javax.servlet.http.HttpSession;
   
   import org.apache.turbine.om.security.User;
   import org.apache.turbine.services.TurbineBaseService;
   
   /**
  - * The SessionService allows access to the current sessions of the current
  - * context.
  - * The session objects that are cached by this service are obtained through
  - * a listener.  The listener must be configured in your web.xml file.
  + * The SessionService allows thread-safe access to the current
  + * sessions of the current context.  The session objects that are
  + * cached by this service are obtained through a listener, which must
  + * be configured via your web application's <code>web.xml</code>
  + * deployement descriptor as follows:
    *
  - * Access to this service should be through
  - * org.apache.turbine.session.TurbineSession
  + * <blockquote><code><pre>
  + * <listener>
  + *   <listener-class>
  + *     org.apache.turbine.session.SessionListener
  + *   </listener-class>
  + * </listener>
  + * </pre></code></blockquote>
    *
    * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
  + * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
  + * @since 2.3
    * @version $Id$
    * @see org.apache.turbine.services.session.TurbineSession
    * @see org.apache.turbine.services.session.SessionListener
  @@ -86,15 +94,18 @@
       private Map activeSessions;
   
       /**
  -     * Gets a list of the active sessions
  +     * Gets a list of the active sessions.
        *
  -     * @return List of HttpSession objects
  +     * @return A copy of the list of <code>HttpSession</code> objects.
        */
       public Collection getActiveSessions()
       {
  -        Collection result = null;
  -        result = activeSessions.values();
  -        return result;
  +        // Sync externally to allow ArrayList's ctor to iterate
  +        // activeSessions' values in a thread-safe fashion.
  +        synchronized (activeSessions)
  +        {
  +            return new ArrayList(activeSessions.values());
  +        }
       }
   
       /**
  @@ -132,8 +143,7 @@
        */
       public boolean isUserLoggedIn(User user)
       {
  -        Collection col = getActiveUsers();
  -        return col.contains(user);
  +        return getActiveUsers().contains(user);
       }
   
       /**
  @@ -141,20 +151,23 @@
        * logged in.  This will exclude any instances of anonymous user that
        * Turbine will use before the user actually logs on.
        *
  -     * @return collection of org.apache.turbine.om.security.User objects
  +     * @return A set of {@link org.apache.turbine.om.security.User} objects.
        */
       public Collection getActiveUsers()
       {
  -        Vector users = new Vector();
  -
  -        // loop through the active sessions to find the user
  -        for(Iterator iter = activeSessions.values().iterator(); iter.hasNext();)
  +        Collection users;
  +        synchronized (activeSessions)
           {
  -            HttpSession session = (HttpSession) iter.next();
  -            User tmpUser = getUserFromSession(session);
  -            if(tmpUser != null && tmpUser.hasLoggedIn())
  +            // Pre-allocate a list which won't need expansion more
  +            // than once.
  +            users = new ArrayList((int) (activeSessions.size() * 0.7));
  +            for (Iterator i = activeSessions.values().iterator(); i.hasNext();)
               {
  -                users.add(tmpUser);
  +                User u = getUserFromSession((HttpSession) i.next());
  +                if (u != null && u.hasLoggedIn())
  +                {
  +                    users.add(u);
  +                }
               }
           }
   
  @@ -164,8 +177,8 @@
       /**
        * Gets the User object of the the specified HttpSession.
        *
  -     * @param session
  -     * @return
  +     * @param session The session from which to extract a user.
  +     * @return The Turbine User object.
        */
       public User getUserFromSession(HttpSession session)
       {
  @@ -175,8 +188,8 @@
       /**
        * Gets the HttpSession by the session identifier
        *
  -     * @param sessionId
  -     * @return
  +     * @param sessionId The unique session identifier.
  +     * @return The session keyed by the specified identifier.
        */
       public HttpSession getSession(String sessionId)
       {
  @@ -217,7 +230,7 @@
        */
       public void init()
       {
  -        this.activeSessions = new HashMap();
  +        this.activeSessions = new Hashtable();
   
           setInit(true);
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


Re: cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/services/session SessionListener.java SessionService.java TurbineSessionService.java

Posted by Daniel Rall <dl...@collab.net>.
On 31 Jan 2003 dlr@apache.org wrote:

> dlr         2003/01/30 16:20:53
> 
>   Modified:    src/java/org/apache/turbine/services/session
>                         SessionListener.java SessionService.java
>                         TurbineSessionService.java
>   Log:
>   * src/java/org/apache/turbine/services/session/SessionListener.java
>     Removed imports obviated by removal of static "log" field.
>   
>     Added <listener> and <listener-class> web.xml examples and @since
>     tag to header JavaDoc.
>   
>     log: Removed unused member.
>   
>   * src/java/org/apache/turbine/services/session/SessionService.java
>     Added @since tag to header JavaDoc.
>   
>     getUserFromSession(HttpSession), getSession(String): Added JavaDoc,
>     and removed redundant public declarations (implicit in a Java
>     interface) for consistancy.
>   
>   * src/java/org/apache/turbine/services/session/TurbineSessionService.java
>     Improved header JavaDoc.
>   
>     activeSessions: Changed from HashMap to Hashtable for thread-safety.
>   
>     getActiveSessions(): Now returns a copy of the active sessions,
>     rather than a handle to the Collection view of the values of the
>     actual Map used to store the active sessions.
>   
>     getActiveUsers(): Changed Collection implementation used to record
>     active users from the slow, synchronized Vector to the faster,context-param
                                                                    ^

This context-param bit was a stray paste.  I don't have cvs admin karma, but
I hacked the ,v files in the repository to fix it.  "Kids, don't try this at 
home."

- Dan



---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org