1
2
3
4
5
6
7
8
9
10
11
12
13
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.User;
22 import org.acegisecurity.userdetails.UserDetails;
23 import org.acegisecurity.userdetails.UsernameNotFoundException;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.springframework.util.Assert;
27
28
29 /***
30 * Used by {@link InMemoryDaoImpl} to store a list of users and their
31 * corresponding granted authorities.
32 *
33 * @author Ben Alex
34 * @version $Id: UserMap.java,v 1.8 2005/11/29 13:10:09 benalex Exp $
35 */
36 public class UserMap {
37
38
39 private static final Log logger = LogFactory.getLog(UserMap.class);
40
41
42
43 private Map userMap = new HashMap();
44
45
46
47 /***
48 * Locates the specified user by performing a case insensitive search by
49 * username.
50 *
51 * @param username to find
52 *
53 * @return the located user
54 *
55 * @throws UsernameNotFoundException if the user could not be found
56 */
57 public User getUser(String username) throws UsernameNotFoundException {
58 User result = (User) this.userMap.get(username.toLowerCase());
59
60 if (result == null) {
61 throw new UsernameNotFoundException("Could not find user: "
62 + username);
63 }
64
65 return result;
66 }
67
68 /***
69 * Indicates the size of the user map.
70 *
71 * @return the number of users in the map
72 */
73 public int getUserCount() {
74 return this.userMap.size();
75 }
76
77 /***
78 * Adds a user to the in-memory map.
79 *
80 * @param user the user to be stored
81 *
82 * @throws IllegalArgumentException if a null User was passed
83 */
84 public void addUser(UserDetails user) throws IllegalArgumentException {
85 Assert.notNull(user, "Must be a valid User");
86
87 logger.info("Adding user [" + user + "]");
88 this.userMap.put(user.getUsername().toLowerCase(), user);
89 }
90 }