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