1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.objectledge.authentication;
29
30 import java.security.Principal;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.Map;
35 import java.util.Set;
36
37 import javax.servlet.http.HttpSession;
38 import javax.servlet.http.HttpSessionBindingEvent;
39 import javax.servlet.http.HttpSessionBindingListener;
40
41 import org.objectledge.context.Context;
42 import org.objectledge.pipeline.ProcessingException;
43 import org.objectledge.pipeline.Valve;
44 import org.objectledge.web.HttpContext;
45
46 /***
47 * A valve that
48 *
49 * @version $Id: UserTrackingValve.java,v 1.1 2005/07/26 09:25:47 rafal Exp $
50 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
51 */
52 public class UserTrackingValve
53 implements Valve
54 {
55 private final Map<Principal,PunchCard> cardRack = new HashMap<Principal,PunchCard>();
56
57 /***
58 * {@inheritDoc}
59 */
60 public void process(Context context)
61 throws ProcessingException
62 {
63 AuthenticationContext authContext = AuthenticationContext.getAuthenticationContext(context);
64 if(authContext != null && authContext.isUserAuthenticated())
65 {
66 synchronized(cardRack)
67 {
68 Principal principal = authContext.getUserPrincipal();
69 PunchCard card = cardRack.get(principal);
70 if(card == null)
71 {
72 card = new PunchCard(principal);
73 cardRack.put(principal, card);
74 HttpContext httpContext = HttpContext.getHttpContext(context);
75 HttpSession session = httpContext.getRequest().getSession();
76 session.setAttribute(PunchCard.class.getName(), card);
77 }
78 else
79 {
80 card.touch();
81 }
82 }
83 }
84 }
85
86 /***
87 * Returns the principals of all users that have an active session.
88 *
89 * @return a set of principal objects.
90 */
91 public Set<Principal> getLoggedInUsers()
92 {
93 synchronized(cardRack)
94 {
95 return new HashSet<Principal>(cardRack.keySet());
96 }
97 }
98
99 /***
100 * Returns the timestamp of user's most recent click.
101 *
102 * @param principal the users's principal.
103 * @return the timestamp of user's most recent click, or null if user is not logged in.
104 */
105 public Date getLastClickTime(Principal principal)
106 {
107 synchronized(cardRack)
108 {
109 PunchCard card = cardRack.get(principal);
110 if(card != null)
111 {
112 return card.getLastClickTime();
113 }
114 else
115 {
116 return null;
117 }
118 }
119 }
120
121 void checkOut(PunchCard card)
122 {
123 synchronized(cardRack)
124 {
125 cardRack.remove(card.getPrincipal());
126 }
127 }
128
129 /***
130 * A user's puch card.
131 */
132 private class PunchCard
133 implements HttpSessionBindingListener
134 {
135 private final Principal principal;
136
137 private Date lastClickTime;
138
139 public PunchCard(Principal principal) {
140 this.principal = principal;
141 this.lastClickTime = new Date();
142 }
143
144 public Principal getPrincipal() {
145 return principal;
146 }
147
148 public void touch() {
149 lastClickTime = new Date();
150 }
151
152 public Date getLastClickTime() {
153 return lastClickTime;
154 }
155
156 /***
157 * {@inheritDoc}
158 */
159 public void valueBound(HttpSessionBindingEvent arg0)
160 {
161
162 }
163
164 /***
165 * {@inheritDoc}
166 */
167 public void valueUnbound(HttpSessionBindingEvent event)
168 {
169 if(event.getValue() instanceof PunchCard)
170 {
171 checkOut((PunchCard)event.getValue());
172 }
173 }
174 }
175 }