| 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.HashMap; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import org.apache.commons.logging.Log; |
| 22 | import org.apache.hivemind.ApplicationRuntimeException; |
| 23 | |
| 24 | // singleton |
| 25 | public class UserContextStorageImpl |
| 26 | implements UserContextStorage, UserEventListener |
| 27 | { |
| 28 | public UserContextStorageImpl(Log logger, SecurityService security) |
| 29 | { |
| 30 | _logger = logger; |
| 31 | _security = security; |
| 32 | } |
| 33 | |
| 34 | public void userConnected(Principal user) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | public void userDisconnected(Principal user, boolean forced) |
| 39 | { |
| 40 | clearContext(user); |
| 41 | } |
| 42 | |
| 43 | public void clear() |
| 44 | { |
| 45 | clearContext(_security.getCurrentUser()); |
| 46 | } |
| 47 | |
| 48 | public Object get(String key) |
| 49 | { |
| 50 | return getContext().get(key); |
| 51 | } |
| 52 | |
| 53 | public void put(String key, Object value) |
| 54 | { |
| 55 | getContext().put(key, value); |
| 56 | } |
| 57 | |
| 58 | private Map<String, Object> getContext() |
| 59 | { |
| 60 | Principal principal = _security.getCurrentUser(); |
| 61 | if (principal == null) |
| 62 | { |
| 63 | _logger.warn("No Principal in current context"); |
| 64 | throw new ApplicationRuntimeException( |
| 65 | "Cannot use UserContextStorage with no Principal"); |
| 66 | } |
| 67 | return getContext(principal); |
| 68 | } |
| 69 | |
| 70 | private synchronized Map<String, Object> getContext(Principal principal) |
| 71 | { |
| 72 | Map<String, Object> context = _contexts.get(principal); |
| 73 | if (context == null) |
| 74 | { |
| 75 | context = new HashMap<String, Object>(); |
| 76 | _contexts.put(principal, context); |
| 77 | } |
| 78 | return context; |
| 79 | } |
| 80 | |
| 81 | private synchronized void clearContext(Principal principal) |
| 82 | { |
| 83 | if (principal != null) |
| 84 | { |
| 85 | _contexts.remove(principal); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | final private Log _logger; |
| 90 | final private SecurityService _security; |
| 91 | final private Map<Principal, Map<String, Object>> _contexts = |
| 92 | new HashMap<Principal, Map<String, Object>>(); |
| 93 | } |