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.util.EnumMap;
22 * User management exception.
24 * @author Erik Brakkee
26 public class UserMgtException extends Exception {
28 static final long serialVersionUID = 5585349754997507529L;
31 * Possible causes for the exception.
39 USER_ALREADY_IN_GROUP,
44 USER_MUST_BE_IN_A_GROUP,
50 * Mapping of enum to exception message text.
52 private static final EnumMap<Reason,String> MESSAGES = new EnumMap<Reason,String>(Reason.class);
55 MESSAGES.put(Reason.UNKNOWN_USER, "Unknown user");
56 MESSAGES.put(Reason.UNKNOWN_GROUP, "Unknown group");
57 MESSAGES.put(Reason.DUPLICATE_USER, "Duplicate user");
58 MESSAGES.put(Reason.DUPLICATE_GROUP, "Duplicate group");
59 MESSAGES.put(Reason.USER_ALREADY_IN_GROUP, "User already in group");
60 MESSAGES.put(Reason.USER_NOT_IN_GROUP, "User not in group");
61 MESSAGES.put(Reason.TRIVIAL_RENAME, "Trivial rename");
62 MESSAGES.put(Reason.INVALID_PASSWORD, "Invalid password");
63 MESSAGES.put(Reason.GROUP_STILL_OCCUPIED, "Group still occupied");
64 MESSAGES.put(Reason.USER_MUST_BE_IN_A_GROUP, "User must be in at least one group");
65 MESSAGES.put(Reason.INVALID_USERNAME, "Invalid user name");
66 MESSAGES.put(Reason.INVALID_GROUPNAME, "Invalid group name");
70 * Cause of the exception.
72 private Reason _cause;
75 * User or null if no user is relevant for the problem.
80 * Group or null if no group is relevant for the problem.
84 public UserMgtException(Reason aCause, String aMessage) {
85 super(MESSAGES.get(aCause) + ": " + aMessage);
89 public UserMgtException(Reason aCause, User aUser) {
90 this(aCause, "for user '" + aUser.getName() + "'");
94 public UserMgtException(Reason aCause, Group aGroup) {
95 this(aCause, "for group '" + aGroup.getName() + "'");
99 public UserMgtException(Reason aCause, User aUser, Group aGroup) {
100 this(aCause, "for user '" + aUser.getName() + "' and group '" + aGroup.getName() + "'");
106 * Gets the cause of the problem.
109 public Reason getReason() {
114 * Gets the user for which the problem occurred.
115 * @return User or null if not applicable.
117 public User getUser() {
122 * Gets the group for which the problem occured.
123 * @return Group or null if not applicable.
125 public Group getGroup() {