View Javadoc

1   /* Copyright 2004 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.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      //~ Static fields/initializers =============================================
38  
39      private static final Log logger = LogFactory.getLog(UserMap.class);
40  
41      //~ Instance fields ========================================================
42  
43      private Map userMap = new HashMap();
44  
45      //~ Methods ================================================================
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  }