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.om.security.User;
29 import org.apache.turbine.services.pull.ApplicationTool;
30
31 /**
32 * A pull tool for accessing the SessionService from a velocity template.
33 *
34 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
35 * @version $Id: SessionTool.java 1706239 2015-10-01 13:18:35Z tv $
36 */
37 public class SessionTool
38 implements ApplicationTool
39 {
40 @Override
41 public void init(Object o)
42 {
43 // empty
44 }
45
46 @Override
47 public void refresh()
48 {
49 // empty
50 }
51
52 /**
53 * Gets a list of the active sessions
54 *
55 * @return List of HttpSession objects
56 */
57 public Collection<HttpSession> getActiveSessions()
58 {
59 return TurbineSession.getActiveSessions();
60 }
61
62 /**
63 * Adds a session to the current list. This method should only be
64 * called by the listener.
65 *
66 * @param session Session to add
67 */
68 public void addSession(HttpSession session)
69 {
70 TurbineSession.addSession(session);
71 }
72
73 /**
74 * Removes a session from the current list. This method should only be
75 * called by the listener.
76 *
77 * @param session Session to remove
78 */
79 public void removeSession(HttpSession session)
80 {
81 TurbineSession.removeSession(session);
82 }
83
84 /**
85 * Determines if a given user is currently logged in. The actual
86 * implementation of the User object must implement the equals()
87 * method. By default, Torque based objects (liek TurbineUser)
88 * have an implementation of equals() that will compare the
89 * result of getPrimaryKey().
90 *
91 * @param user User to check for
92 * @return true if the user is logged in on one of the
93 * active sessions.
94 */
95 public boolean isUserLoggedIn(User user)
96 {
97 return TurbineSession.isUserLoggedIn(user);
98 }
99
100 /**
101 * Gets a collection of all user objects representing the users currently
102 * logged in. This will exclude any instances of anonymous user that
103 * Turbine will use before the user actually logs on.
104 *
105 * @return collection of org.apache.turbine.om.security.User objects
106 */
107 public Collection<User> getActiveUsers()
108 {
109 return TurbineSession.getActiveUsers();
110 }
111
112 /**
113 * Gets the User object of the the specified HttpSession.
114 *
115 * @param session
116 * @return the user from the session
117 */
118 public User getUserFromSession(HttpSession session)
119 {
120 return TurbineSession.getUserFromSession(session);
121 }
122
123 /**
124 * Get a collection of all session on which the given user
125 * is logged in.
126 *
127 * @param user the user
128 * @return Collection of HtttSession objects
129 */
130 public Collection<HttpSession> getSessionsForUser(User user)
131 {
132 return TurbineSession.getSessionsForUser(user);
133 }
134 }