View Javadoc

1   /* Copyright 2004, 2005 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.Properties;
19  
20  import org.acegisecurity.userdetails.UserDetails;
21  import org.acegisecurity.userdetails.UserDetailsService;
22  import org.acegisecurity.userdetails.UsernameNotFoundException;
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.dao.DataAccessException;
25  import org.springframework.util.Assert;
26  
27  
28  /***
29   * Retrieves user details from an in-memory list created by the bean context.
30   *
31   * @author Ben Alex
32   * @version $Id: InMemoryDaoImpl.java,v 1.9 2005/11/29 13:10:09 benalex Exp $
33   */
34  public class InMemoryDaoImpl implements UserDetailsService, InitializingBean {
35      //~ Instance fields ========================================================
36  
37      private UserMap userMap;
38  
39      //~ Methods ================================================================
40  
41      public void setUserMap(UserMap userMap) {
42          this.userMap = userMap;
43      }
44  
45      public UserMap getUserMap() {
46          return userMap;
47      }
48  
49      /***
50       * Modifies the internal <code>UserMap</code> to reflect the
51       * <code>Properties</code> instance passed. This helps externalise user
52       * information to another file etc.
53       *
54       * @param props the account information in a <code>Properties</code> object
55       *        format
56       */
57      public void setUserProperties(Properties props) {
58          UserMap userMap = new UserMap();
59          this.userMap = UserMapEditor.addUsersFromProperties(userMap, props);
60      }
61  
62      public void afterPropertiesSet() throws Exception {
63          Assert.notNull(this.userMap,
64              "A list of users, passwords, enabled/disabled status and their granted authorities must be set");
65      }
66  
67      public UserDetails loadUserByUsername(String username)
68          throws UsernameNotFoundException, DataAccessException {
69          return userMap.getUser(username);
70      }
71  }