dee345417ccdab5c7d460655eab8cdff00846abc
[utils] / security / src / main / java / org / wamblee / usermgt / UserAdminInitializer.java
1 /*
2  * Copyright 2005 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
17 package org.wamblee.usermgt;
18
19 import java.security.NoSuchAlgorithmException;
20
21 import org.apache.log4j.Logger;
22
23 /**
24  * User administration initializer. It populates the user administration with a
25  * number of groups and users but only in case no users exist.
26  */
27 public class UserAdminInitializer {
28     
29     private static final Logger LOGGER = Logger.getLogger(UserAdminInitializer.class);
30     
31     /**
32      * Initializes the user administration in case no users are present.
33      * 
34      */
35     public UserAdminInitializer(UserAdministration aAdmin, String[] aUsers,
36             String[] aGroups, String[] aPasswords) throws UserMgtException, NoSuchAlgorithmException {
37         if (aUsers.length != aGroups.length
38                 || aUsers.length != aPasswords.length) {
39             throw new IllegalArgumentException(
40                     "Array sizes for users, groups, and passwords differ: "
41                             + aUsers.length + "," + aGroups.length + ","
42                             + aPasswords.length);
43
44         }
45         if (aAdmin.getUserCount() == 0) {
46             initialize(aAdmin, aUsers, aGroups, aPasswords);
47         }
48        
49     }
50
51     /**
52      * Adds the specified users and groups to the user administration. 
53      * @param aAdmin User administration. 
54      * @param aUsers Users. 
55      * @param aGroups Groups. 
56      * @param aPasswords Passwords. 
57      * @throws UserMgtException In case of a problem creating users or groups. 
58      */
59     private void initialize(UserAdministration aAdmin, String[] aUsers,
60             String[] aGroups, String[] aPasswords) throws UserMgtException {
61         for (int i = 0; i < aUsers.length; i++) {
62             String user = aUsers[i];
63             String group = aGroups[i];
64             String password = aPasswords[i];
65             
66             if (aAdmin.getUser(user) == null) {
67                 // must create user.
68                 Group groupObj = aAdmin.getGroup(group);
69                 if (groupObj == null) {
70                     // must create group
71                     LOGGER.info("Creating group: " + group);
72                     groupObj = aAdmin.createGroup(group);
73                 }
74                 assert groupObj != null; 
75                 
76                 LOGGER.info("Creating user: " + user + " password: " + password);
77                 aAdmin.createUser(user, password, groupObj);
78             }
79         }
80     }
81 }