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