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