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