(no commit message)
[utils] / security / usermgt / src / main / java / org / wamblee / security / authentication / AbstractUserSet.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.security.authentication;
17
18 import static org.wamblee.security.authentication.UserMgtException.Reason.*;
19
20 /**
21  * User set base class.
22  */
23 public abstract class AbstractUserSet implements UserSet {
24     /**
25      * Password validator.
26      */
27     private NameValidator passwordValidator;
28
29     /**
30      * Password encoder.
31      */
32     private MessageDigester passwordEncoder;
33
34     /**
35      * Creates a new AbstractUserSet object.
36      * 
37      */
38     protected AbstractUserSet(NameValidator aPasswordValidator,
39         MessageDigester aPasswordEncoder) {
40         passwordValidator = aPasswordValidator;
41         passwordEncoder = aPasswordEncoder;
42     }
43
44     /**
45      * Sets the password validtor and encoder in the user.
46      * 
47      * @param aUser
48      *            User.
49      */
50     protected void setPasswordInfo(User aUser) {
51         aUser.setPasswordValidator(passwordValidator);
52         aUser.setPasswordEncoder(passwordEncoder);
53     }
54
55     @Override
56     public User createUser(String aUsername, String aPassword) {
57         User user = new User(aUsername, aPassword, passwordValidator,
58             passwordEncoder);
59
60         if (contains(user)) {
61             throw new UserMgtException(DUPLICATE_USER, user);
62         }
63
64         add(user);
65
66         return user;
67     }
68 }