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