View Javadoc

1   /*
2    * Copyright  2003-2004 The Apache Software Foundation.
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  
18  package org.apache.ws.security.components.crypto;
19  
20  import java.text.MessageFormat;
21  import java.util.MissingResourceException;
22  import java.util.ResourceBundle;
23  
24  /***
25   * CredentialException.
26   * <p/>
27   *
28   * @author Davanum Srinivas (dims@yahoo.com).
29   */
30  public class CredentialException extends Exception {
31      public static final int FAILURE = -1;
32      public static final int EXPIRED = 1;
33      public static final int DEFECTIVE = 2;
34      public static final int IO_ERROR = 3;
35      public static final int SEC_ERROR = 3;
36      private static ResourceBundle resources;
37      private int errorCode;
38  
39      static {
40          try {
41              resources = ResourceBundle.getBundle("org.apache.ws.security.components.crypto.errors");
42          } catch (MissingResourceException e) {
43              throw new RuntimeException(e.getMessage());
44          }
45      }
46  
47      /***
48       * Constructor.
49       * <p/>
50       *
51       * @param errorCode
52       * @param msgId
53       * @param root
54       */
55      public CredentialException(int errorCode, String msgId, Throwable root) {
56          this(errorCode, msgId, null, root);
57      }
58  
59      /***
60       * Constructor.
61       * <p/>
62       *
63       * @param errorCode
64       * @param msgId
65       * @param args
66       */
67      public CredentialException(int errorCode, String msgId, Object[] args) {
68          this(errorCode, msgId, args, null);
69      }
70  
71      /***
72       * Constructor.
73       * <p/>
74       *
75       * @param errorCode
76       * @param msgId
77       * @param args
78       * @param root
79       */
80      public CredentialException(int errorCode, String msgId, Object[] args, Throwable root) {
81          super(getMessage(msgId, args) + (root == null ? "[]" : " Inner Exception: [" + root.getMessage() + "]"));
82          this.errorCode = -1;
83          this.errorCode = errorCode;
84      }
85  
86      /***
87       * get the error code.
88       * <p/>
89       *
90       * @return error code of this exception See values above.
91       */
92      public int getErrorCode() {
93          return errorCode;
94      }
95  
96      /***
97       * get the actual message.
98       * <p/>
99       *
100      * @param msgId
101      * @param args
102      * @return the message translated from the property (message) file.
103      */
104     private static String getMessage(String msgId, Object[] args) {
105         try {
106             return MessageFormat.format(resources.getString(msgId), args);
107         } catch (MissingResourceException e) {
108             throw new RuntimeException("bad" + msgId);
109         }
110     }
111 }
112