View Javadoc

1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
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  
16  package org.springframework.security.userdetails.memory;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.springframework.security.userdetails.UserDetails;
22  import org.springframework.security.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  /**
29   * Used by {@link InMemoryDaoImpl} to store a list of users and their corresponding granted authorities.
30   *
31   * @author Ben Alex
32   * @version $Id: UserMap.java 2729 2008-03-13 16:49:19Z luke_t $
33   */
34  public class UserMap {
35      //~ Static fields/initializers =====================================================================================
36  
37      private static final Log logger = LogFactory.getLog(UserMap.class);
38  
39      //~ Instance fields ================================================================================================
40  
41      private Map userMap = new HashMap();
42  
43      //~ Methods ========================================================================================================
44  
45      /**
46       * Adds a user to the in-memory map.
47       *
48       * @param user the user to be stored
49       *
50       * @throws IllegalArgumentException if a null User was passed
51       */
52      public void addUser(UserDetails user) 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) throws UsernameNotFoundException {
69          UserDetails result = (UserDetails) this.userMap.get(username.toLowerCase());
70  
71          if (result == null) {
72              throw new UsernameNotFoundException("Could not find user: " + username, username);
73          }
74  
75          return result;
76      }
77  
78      /**
79       * Indicates the size of the user map.
80       *
81       * @return the number of users in the map
82       */
83      public int getUserCount() {
84          return this.userMap.size();
85      }
86  
87      /**
88       * Set the users in this {@link UserMap}. Overrides previously added users.
89       *
90       * @param users {@link Map} <{@link String}, {@link UserDetails}> with pairs (username, userdetails)
91       * @since 1.1
92       */
93      public void setUsers(Map users) {
94          this.userMap = users;
95      }
96  }