Removed DOCUMENT ME comments that were generated and applied source code
[utils] / security / src / main / java / org / wamblee / usermgt / UserMgtException.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 java.util.EnumMap;
19
20 /**
21  * User management exception.
22  * 
23  * @author Erik Brakkee
24  */
25 public class UserMgtException extends Exception {
26     static final long serialVersionUID = 5585349754997507529L;
27
28     /**
29      * Mapping of enum to exception message text.
30      */
31     private static final EnumMap<Reason, String> MESSAGES = new EnumMap<Reason, String>(
32         Reason.class);
33
34     static {
35         MESSAGES.put(Reason.UNKNOWN_USER, "Unknown user");
36         MESSAGES.put(Reason.UNKNOWN_GROUP, "Unknown group");
37         MESSAGES.put(Reason.DUPLICATE_USER, "Duplicate user");
38         MESSAGES.put(Reason.DUPLICATE_GROUP, "Duplicate group");
39         MESSAGES.put(Reason.USER_ALREADY_IN_GROUP, "User already in group");
40         MESSAGES.put(Reason.USER_NOT_IN_GROUP, "User not in group");
41         MESSAGES.put(Reason.TRIVIAL_RENAME, "Trivial rename");
42         MESSAGES.put(Reason.INVALID_PASSWORD, "Invalid password");
43         MESSAGES.put(Reason.GROUP_STILL_OCCUPIED, "Group still occupied");
44         MESSAGES.put(Reason.USER_MUST_BE_IN_A_GROUP,
45             "User must be in at least one group");
46         MESSAGES.put(Reason.INVALID_USERNAME, "Invalid user name");
47         MESSAGES.put(Reason.INVALID_GROUPNAME, "Invalid group name");
48     }
49
50     /**
51      * Cause of the exception.
52      */
53     private Reason cause;
54
55     /**
56      * User or null if no user is relevant for the problem.
57      */
58     private User user;
59
60     /**
61      * Group or null if no group is relevant for the problem.
62      */
63     private Group group;
64
65     /**
66      * Creates a new UserMgtException object.
67      * 
68      */
69     public UserMgtException(Reason aCause, String aMessage) {
70         super(MESSAGES.get(aCause) + ": " + aMessage);
71         cause = aCause;
72     }
73
74     /**
75      * Creates a new UserMgtException object.
76      * 
77      */
78     public UserMgtException(Reason aCause, User aUser) {
79         this(aCause, "for user '" + aUser.getName() + "'");
80         user = aUser;
81     }
82
83     /**
84      * Creates a new UserMgtException object.
85      * 
86      */
87     public UserMgtException(Reason aCause, Group aGroup) {
88         this(aCause, "for group '" + aGroup.getName() + "'");
89         group = aGroup;
90     }
91
92     /**
93      * Creates a new UserMgtException object.
94      * 
95      */
96     public UserMgtException(Reason aCause, User aUser, Group aGroup) {
97         this(aCause, "for user '" + aUser.getName() + "' and group '" +
98             aGroup.getName() + "'");
99         user = aUser;
100         group = aGroup;
101     }
102
103     /**
104      * Gets the cause of the problem.
105      * 
106      * @return Cause.
107      */
108     public Reason getReason() {
109         return cause;
110     }
111
112     /**
113      * Gets the user for which the problem occurred.
114      * 
115      * @return User or null if not applicable.
116      */
117     public User getUser() {
118         return user;
119     }
120
121     /**
122      * Gets the group for which the problem occured.
123      * 
124      * @return Group or null if not applicable.
125      */
126     public Group getGroup() {
127         return group;
128     }
129
130     /**
131      * Possible causes for the exception.
132      * 
133      */
134     public enum Reason {
135         UNKNOWN_USER, UNKNOWN_GROUP, DUPLICATE_USER, DUPLICATE_GROUP, USER_ALREADY_IN_GROUP, USER_NOT_IN_GROUP, TRIVIAL_RENAME, INVALID_PASSWORD, GROUP_STILL_OCCUPIED, USER_MUST_BE_IN_A_GROUP, INVALID_USERNAME, INVALID_GROUPNAME;
136     }
137 }