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