2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.usermgt;
19 import java.security.NoSuchAlgorithmException;
21 import org.apache.log4j.Logger;
24 * User administration initializer. It populates the user administration with a
25 * number of groups and users but only in case no users exist.
27 * @author Erik Brakkee
29 public class UserAdminInitializer {
31 private static final Logger LOGGER = Logger.getLogger(UserAdminInitializer.class);
34 * Initializes the user administration in case no users are present.
37 public UserAdminInitializer(UserAdministration aAdmin, String[] aUsers,
38 String[] aGroups, String[] aPasswords) throws UserMgtException, 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 + ","
47 if (aAdmin.getUserCount() == 0) {
48 initialize(aAdmin, aUsers, aGroups, aPasswords);
54 * Adds the specified users and groups to the user administration.
55 * @param aAdmin User administration.
56 * @param aUsers Users.
57 * @param aGroups Groups.
58 * @param aPasswords Passwords.
59 * @throws UserMgtException In case of a problem creating users or groups.
61 private void initialize(UserAdministration aAdmin, String[] aUsers,
62 String[] aGroups, String[] aPasswords) throws UserMgtException {
63 for (int i = 0; i < aUsers.length; i++) {
64 String user = aUsers[i];
65 String group = aGroups[i];
66 String password = aPasswords[i];
68 if (aAdmin.getUser(user) == null) {
70 Group groupObj = aAdmin.getGroup(group);
71 if (groupObj == null) {
73 LOGGER.info("Creating group: " + group);
74 groupObj = aAdmin.createGroup(group);
76 assert groupObj != null;
78 LOGGER.info("Creating user: " + user + " password: " + password);
79 aAdmin.createUser(user, password, groupObj);