View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  
21  package org.apache.directory.server.changepw.exceptions;
22  
23  
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.List;
27  
28  
29  /**
30   * Type safe enumeration of Change Password error types
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev: 557096 $
34   */
35  public final class ErrorType implements Comparable<ErrorType>
36  {
37      /**
38       * Constant for the "Request failed due to being malformed" error type.
39       */
40      public static final ErrorType KRB5_KPASSWD_MALFORMED = new ErrorType( 1, "Request failed due to being malformed." );
41  
42      /**
43       * Constant for the "Request failed due to a hard error in processing the request" error type.
44       */
45      public static final ErrorType KRB5_KPASSWD_HARDERROR = new ErrorType( 2,
46          "Request failed due to a hard error in processing the request." );
47  
48      /**
49       * Constant for the "Request failed due to an error in authentication processing" error type.
50       */
51      public static final ErrorType KRB5_KPASSWD_AUTHERROR = new ErrorType( 3,
52          "Request failed due to an error in authentication processing." );
53  
54      /**
55       * Constant for the "Request failed due to a soft error in processing the request" error type.
56       */
57      public static final ErrorType KRB5_KPASSWD_SOFTERROR = new ErrorType( 4,
58          "Request failed due to a soft error in processing the request." );
59  
60      /**
61       * Constant for the "Requestor not authorized" error type.
62       */
63      public static final ErrorType KRB5_KPASSWD_ACCESSDENIED = new ErrorType( 5, "Requestor not authorized." );
64  
65      /**
66       * Constant for the "Protocol version unsupported" error type.
67       */
68      public static final ErrorType KRB5_KPASSWD_BAD_VERSION = new ErrorType( 6, "Protocol version unsupported." );
69  
70      /**
71       * Constant for the "Initial flag required" error type.
72       */
73      public static final ErrorType KRB5_KPASSWD_INITIAL_FLAG_NEEDED = new ErrorType( 7, "Initial flag required." );
74  
75      /**
76       * Constant for the "Request failed for an unknown reason" error type.
77       */
78      public static final ErrorType KRB5_KPASSWD_UNKNOWN_ERROR = new ErrorType( 8,
79          "Request failed for an unknown reason." );
80  
81      /**
82       * Array for building a List of VALUES.
83       */
84      private static final ErrorType[] values =
85          { KRB5_KPASSWD_MALFORMED, KRB5_KPASSWD_HARDERROR, KRB5_KPASSWD_AUTHERROR, KRB5_KPASSWD_SOFTERROR,
86              KRB5_KPASSWD_ACCESSDENIED, KRB5_KPASSWD_BAD_VERSION, KRB5_KPASSWD_INITIAL_FLAG_NEEDED,
87              KRB5_KPASSWD_UNKNOWN_ERROR };
88  
89      /**
90       * A list of all the error type constants.
91       */
92      public static final List<ErrorType> VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
93  
94      /**
95       * The name of the error type.
96       */
97      private final String name;
98  
99      /**
100      * The value/code for the error type.
101      */
102     private final int ordinal;
103 
104 
105     /**
106      * Private constructor prevents construction outside of this class.
107      */
108     private ErrorType( int ordinal, String name )
109     {
110         this.ordinal = ordinal;
111         this.name = name;
112     }
113 
114 
115     /**
116      * Returns the message for this Change Password error.
117      *
118      * @return the message for this Change Password error.
119      */
120     public String getMessage()
121     {
122         return name;
123     }
124 
125 
126     /**
127      * Returns the message for this Change Password error.
128      *
129      * @return the message for this Change Password error.
130      */
131     public String toString()
132     {
133         return name;
134     }
135 
136 
137     /**
138      * Compares this type to another object hopefully one that is of the same
139      * type.
140      *
141      * @param that the object to compare this ErrorType to
142      * @return ordinal - that.ordinal;
143      */
144     public int compareTo( ErrorType that )
145     {
146         return this.ordinal - that.ordinal;
147     }
148 
149 
150     /**
151      * Gets the ordinal by its ordinal value.
152      *
153      * @param ordinal the ordinal value of the ordinal
154      * @return the type corresponding to the ordinal value
155      */
156     public static ErrorType getTypeByOrdinal( int ordinal )
157     {
158         for ( int ii = 0; ii < values.length; ii++ )
159         {
160             if ( values[ii].ordinal == ordinal )
161             {
162                 return values[ii];
163             }
164         }
165 
166         return KRB5_KPASSWD_UNKNOWN_ERROR;
167     }
168 
169 
170     /**
171      * Gets the ordinal value associated with this Change Password error.
172      *
173      * @return the ordinal value associated with this Change Password error
174      */
175     public int getOrdinal()
176     {
177         return ordinal;
178     }
179 }