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