1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivelock; |
16 | |
17 | import java.security.Principal; |
18 | import java.util.ArrayList; |
19 | import java.util.HashMap; |
20 | import java.util.Iterator; |
21 | import java.util.List; |
22 | import java.util.Map; |
23 | import java.util.Timer; |
24 | import java.util.TimerTask; |
25 | |
26 | import org.apache.commons.logging.Log; |
27 | import org.apache.hivemind.events.RegistryShutdownListener; |
28 | import org.apache.hivemind.service.ThreadLocalStorage; |
29 | import org.apache.hivemind.util.EventListenerList; |
30 | |
31 | // singleton |
32 | public class SecurityServiceImpl |
33 | implements SecurityService, RegistryShutdownListener |
34 | { |
35 | static private final long ONE_SECOND = 1000L; |
36 | static private final long SCAVENGER_PERIOD = 10L; |
37 | |
38 | public SecurityServiceImpl( Log logger, |
39 | ThreadLocalStorage storage, |
40 | long autoDisconnectTimeout, |
41 | long scavengerPeriod) |
42 | { |
43 | _logger = logger; |
44 | _storage = storage; |
45 | _autoDisconnectTimeout = autoDisconnectTimeout * ONE_SECOND; |
46 | if (autoDisconnectTimeout > 0L) |
47 | { |
48 | long timerPeriod = scavengerPeriod; |
49 | if (timerPeriod <= 0L) |
50 | { |
51 | timerPeriod = SCAVENGER_PERIOD; |
52 | } |
53 | timerPeriod *= ONE_SECOND; |
54 | // Start timer |
55 | _connected = new HashMap<Principal, Long>(); |
56 | _timer = new Timer(); |
57 | TimerTask task = new TimerTask() |
58 | { |
59 | public void run() |
60 | { |
61 | scavenge(); |
62 | } |
63 | }; |
64 | _timer.schedule(task, timerPeriod, timerPeriod); |
65 | } |
66 | else |
67 | { |
68 | _timer = null; |
69 | _connected = null; |
70 | } |
71 | } |
72 | |
73 | public void registryDidShutdown() |
74 | { |
75 | _logger.debug("registryDidShutdown()"); |
76 | //#### Should disconnect everybody? |
77 | } |
78 | |
79 | protected void scavenge() |
80 | { |
81 | _logger.debug("scavenge()"); |
82 | for (Principal user: findUsersToScavenge()) |
83 | { |
84 | disconnect(user); |
85 | } |
86 | } |
87 | |
88 | public void login(Principal user) |
89 | { |
90 | if (user != null) |
91 | { |
92 | _logger.info(user.getName() + " logged in"); |
93 | setCurrentUser(user); |
94 | fireUserConnected(user); |
95 | } |
96 | } |
97 | |
98 | public void logout() |
99 | { |
100 | Principal user = getCurrentUser(); |
101 | if (user != null) |
102 | { |
103 | _logger.info(user.getName() + " logged out"); |
104 | clearUser(user); |
105 | fireUserDisconnected(user, false); |
106 | } |
107 | } |
108 | |
109 | public void disconnect(Principal user) |
110 | { |
111 | _logger.debug("disconnect()"); |
112 | if (user != null) |
113 | { |
114 | _logger.info(user.getName() + " has been disconnected"); |
115 | clearUser(user); |
116 | fireUserDisconnected(user, true); |
117 | } |
118 | } |
119 | |
120 | public Principal getCurrentUser() |
121 | { |
122 | return (Principal) _storage.get(PRINCIPAL); |
123 | } |
124 | |
125 | public void setCurrentUser(Principal user) |
126 | { |
127 | _storage.put(PRINCIPAL, user); |
128 | blockUser(user); |
129 | } |
130 | |
131 | public void clearCurrentUser() |
132 | { |
133 | unblockUser(getCurrentUser()); |
134 | _storage.put(PRINCIPAL, null); |
135 | } |
136 | |
137 | synchronized public void addUserEventListener(UserEventListener listener) |
138 | { |
139 | _listeners.addListener(listener); |
140 | } |
141 | |
142 | synchronized public void removeUserEventListener(UserEventListener listener) |
143 | { |
144 | _listeners.removeListener(listener); |
145 | } |
146 | |
147 | protected void fireUserConnected(Principal principal) |
148 | { |
149 | Iterator i = _listeners.getListeners(); |
150 | while (i.hasNext()) |
151 | { |
152 | UserEventListener l = (UserEventListener) i.next(); |
153 | l.userConnected(principal); |
154 | } |
155 | } |
156 | |
157 | protected void fireUserDisconnected(Principal principal, boolean forced) |
158 | { |
159 | Iterator i = _listeners.getListeners(); |
160 | while (i.hasNext()) |
161 | { |
162 | UserEventListener l = (UserEventListener) i.next(); |
163 | l.userDisconnected(principal, forced); |
164 | } |
165 | } |
166 | |
167 | synchronized protected void unblockUser(Principal user) |
168 | { |
169 | if (_connected != null && _connected.containsKey(user)) |
170 | { |
171 | _connected.put(user, System.currentTimeMillis() + _autoDisconnectTimeout); |
172 | } |
173 | } |
174 | |
175 | synchronized protected void blockUser(Principal user) |
176 | { |
177 | if (_connected != null) |
178 | { |
179 | _connected.put(user, 0L); |
180 | } |
181 | } |
182 | |
183 | synchronized protected void clearUser(Principal user) |
184 | { |
185 | if (_connected != null) |
186 | { |
187 | _connected.remove(user); |
188 | } |
189 | } |
190 | |
191 | synchronized protected List<Principal> findUsersToScavenge() |
192 | { |
193 | List<Principal> users = new ArrayList<Principal>(_connected.size()); |
194 | long now = System.currentTimeMillis(); |
195 | Iterator<Map.Entry<Principal, Long>> i = _connected.entrySet().iterator(); |
196 | // Run through each entry of _connected map |
197 | while (i.hasNext()) |
198 | { |
199 | Map.Entry<Principal, Long> entry = i.next(); |
200 | long timeout = entry.getValue(); |
201 | // Check if current time > scavenge time |
202 | if (timeout > 0 && timeout < now) |
203 | { |
204 | // Remove from map and trigger events (only after) |
205 | users.add(entry.getKey()); |
206 | i.remove(); |
207 | } |
208 | } |
209 | return users; |
210 | } |
211 | |
212 | final private Log _logger; |
213 | final private ThreadLocalStorage _storage; |
214 | final private long _autoDisconnectTimeout; |
215 | final private EventListenerList _listeners = new EventListenerList(); |
216 | final private Map<Principal, Long> _connected; |
217 | final private Timer _timer; |
218 | |
219 | private static final String PRINCIPAL = "hivelock.core.Principal"; |
220 | } |