diff --git a/core/src/main/java/org/acegisecurity/userdetails/memory/UserMap.java b/core/src/main/java/org/acegisecurity/userdetails/memory/UserMap.java index 6e1bb13992..5766db714c 100644 --- a/core/src/main/java/org/acegisecurity/userdetails/memory/UserMap.java +++ b/core/src/main/java/org/acegisecurity/userdetails/memory/UserMap.java @@ -16,6 +16,7 @@ package net.sf.acegisecurity.providers.dao.memory; import net.sf.acegisecurity.providers.dao.User; +import net.sf.acegisecurity.providers.dao.UsernameNotFoundException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -42,16 +43,48 @@ public class UserMap { //~ Methods ================================================================ - public User getUser(String username) { - return (User) this.userMap.get(username.toLowerCase()); + /** + * Locates the specified user by performing a case insensitive search by + * username. + * + * @param username to find + * + * @return the located user + * + * @throws UsernameNotFoundException if the user could not be found + */ + public User getUser(String username) throws UsernameNotFoundException { + User result = (User) this.userMap.get(username.toLowerCase()); + + if (result == null) { + throw new UsernameNotFoundException("Could not find user: " + + username); + } + + return result; + } + + /** + * Indicates the size of the user map. + * + * @return the number of users in the map + */ + public int getUserCount() { + return this.userMap.size(); } /** * Adds a user to the in-memory map. * * @param user the user to be stored + * + * @throws IllegalArgumentException if a null User was passed */ - public void addUser(User user) { + public void addUser(User user) throws IllegalArgumentException { + if (user == null) { + throw new IllegalArgumentException("Must be a valid User"); + } + logger.info("Adding user [" + user + "]"); this.userMap.put(user.getUsername().toLowerCase(), user); }