View Javadoc

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