01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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:
16: package org.acegisecurity.userdetails.memory;
17:
18: import java.util.HashMap;
19: import java.util.Map;
20:
21: import org.acegisecurity.userdetails.UserDetails;
22: import org.acegisecurity.userdetails.UsernameNotFoundException;
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25: import org.springframework.util.Assert;
26:
27: /**
28: * Used by {@link InMemoryDaoImpl} to store a list of users and their corresponding granted authorities.
29: *
30: * @author Ben Alex
31: * @version $Id: UserMap.java 1677 2006-09-15 03:47:17Z benalex $
32: */
33: public class UserMap {
34: //~ Static fields/initializers =====================================================================================
35:
36: private static final Log logger = LogFactory.getLog(UserMap.class);
37:
38: //~ Instance fields ================================================================================================
39:
40: private Map userMap = new HashMap();
41:
42: //~ Methods ========================================================================================================
43:
44: /**
45: * Adds a user to the in-memory map.
46: *
47: * @param user the user to be stored
48: *
49: * @throws IllegalArgumentException if a null User was passed
50: */
51: public void addUser(UserDetails user)
52: throws IllegalArgumentException {
53: Assert.notNull(user, "Must be a valid User");
54:
55: logger.info("Adding user [" + user + "]");
56: this .userMap.put(user.getUsername().toLowerCase(), user);
57: }
58:
59: /**
60: * Locates the specified user by performing a case insensitive search by username.
61: *
62: * @param username to find
63: *
64: * @return the located user
65: *
66: * @throws UsernameNotFoundException if the user could not be found
67: */
68: public UserDetails getUser(String username)
69: throws UsernameNotFoundException {
70: UserDetails result = (UserDetails) this .userMap.get(username
71: .toLowerCase());
72:
73: if (result == null) {
74: throw new UsernameNotFoundException("Could not find user: "
75: + username);
76: }
77:
78: return result;
79: }
80:
81: /**
82: * Indicates the size of the user map.
83: *
84: * @return the number of users in the map
85: */
86: public int getUserCount() {
87: return this .userMap.size();
88: }
89:
90: /**
91: * Set the users in this {@link UserMap}. Overrides previously added users.
92: *
93: * @param users {@link Map} <{@link String}, {@link UserDetails}> with pairs (username, userdetails)
94: * @since 1.1
95: */
96: public void setUsers(Map users) {
97: this.userMap = users;
98: }
99: }
|