View Javadoc

1   package org.apache.turbine.services.session;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Collection;
20  import javax.servlet.http.HttpSession;
21  
22  import org.apache.turbine.om.security.User;
23  import org.apache.turbine.services.TurbineServices;
24  
25  /***
26   * This is a conveience class provided to allow access to the SessionService
27   * through static methods.  The SessionService should ALWAYS be accessed
28   * through this class.
29   *
30   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
31   * @version $Id: TurbineSession.java 264148 2005-08-29 14:21:04Z henning $
32   * @see org.apache.turbine.services.session.SessionService
33   */
34  public abstract class TurbineSession
35  {
36      /***
37       * Gets a list of the active sessions
38       *
39       * @return List of HttpSession objects
40       */
41      public static Collection getActiveSessions()
42      {
43          return getService().getActiveSessions();
44      }
45  
46      /***
47       * Adds a session to the current list.  This method should only be
48       * called by the listener.
49       *
50       * @param session Session to add
51       */
52      public static void addSession(HttpSession session)
53      {
54          getService().addSession(session);
55      }
56  
57      /***
58       * Removes a session from the current list.  This method should only be
59       * called by the listener.
60       *
61       * @param session Session to remove
62       */
63      public static void removeSession(HttpSession session)
64      {
65          getService().removeSession(session);
66      }
67  
68      /***
69       * Determines if a given user is currently logged in.  The actual
70       * implementation of the User object must implement the equals()
71       * method.  By default, Torque based objects (liek TurbineUser)
72       * have an implementation of equals() that will compare the
73       * result of getPrimaryKey().
74       *
75       * @param user User to check for
76       * @return true if the user is logged in on one of the
77       * active sessions.
78       */
79      public static boolean isUserLoggedIn(User user)
80      {
81          return getService().isUserLoggedIn(user);
82      }
83  
84      /***
85       * Gets a collection of all user objects representing the users currently
86       * logged in.  This will exclude any instances of anonymous user that
87       * Turbine will use before the user actually logs on.
88       *
89       * @return collection of org.apache.turbine.om.security.User objects
90       */
91      public static Collection getActiveUsers()
92      {
93          return getService().getActiveUsers();
94      }
95  
96      /***
97       * Utility method for accessing the service
98       * implementation
99       *
100      * @return a IntakeService implementation instance
101      */
102     private static SessionService getService()
103     {
104         return (SessionService) TurbineServices
105                 .getInstance().getService(SessionService.SERVICE_NAME);
106     }
107 
108     /***
109      * Gets the User object of the the specified HttpSession.
110      *
111      * @param session
112      * @return
113      */
114     public static User getUserFromSession(HttpSession session)
115     {
116         return getService().getUserFromSession(session);
117     }
118 
119     /***
120      * Gets the HttpSession by the session identifier
121      *
122      * @param sessionId
123      * @return
124      */
125     public static HttpSession getSession(String sessionId)
126     {
127         return getService().getSession(sessionId);
128     }
129 
130     /***
131      * Get a collection of all session on which the given user
132      * is logged in.
133      *
134      * @param user the user
135      * @return Collection of HtttSession objects
136      */
137     public static Collection getSessionsForUser(User user)
138     {
139         return getService().getSessionsForUser(user);
140     }
141 }