1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package nl.toolforge.karma.core;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import java.text.MessageFormat;
25 import java.util.Locale;
26 import java.util.MissingResourceException;
27 import java.util.ResourceBundle;
28
29 /***
30 * <p>Class representing a Karma errorcode. These errorcodes are localized to support different languages. Errorcodes
31 * are defined in ranges. Exceptions that are supported by Karma, define these ranges.</p>
32 *
33 * @author D.A. Smedes
34 * @version $Id: ErrorCode.java,v 1.17 2004/11/10 22:06:33 asmedes Exp $
35 */
36 public final class ErrorCode {
37
38 private static Log logger = LogFactory.getLog(ErrorCode.class);
39
40 private static Locale currentLocale = null;
41
42 private Object[] messageArguments = new Object[]{};
43
44 private ResourceBundle messageBundle = null;
45
46 static {
47
48
49
50 currentLocale = Locale.ENGLISH;
51 }
52
53 private String errorCode = null;
54
55 /***
56 * Creates an error code. Error codes must comply to the following pattern : <code>[A-Z]{3}-\d{5}</code>. Examples are:
57 * <code>MAN-00001</code>, <code>CMD-10020</code>.
58 *
59 * @param errorCode
60 */
61 public ErrorCode(String errorCode) {
62
63 if (!errorCode.matches("[A-Z]{3}-//d{5}")) {
64 throw new IllegalArgumentException("Illegal error code format.");
65 }
66 this.errorCode = errorCode;
67 }
68
69 public void setMessageBundle(ResourceBundle messageBundle) {
70 this.messageBundle = messageBundle;
71 }
72
73 /***
74 * Assigns message arguments to this error code as per the <code>MessageFormat</code> definition.
75 *
76 * @param messageArguments An Object array (currently only <code>String</code> instances are supported).
77 */
78 public final void setMessageArguments(Object[] messageArguments) {
79 this.messageArguments = messageArguments;
80 }
81
82 /***
83 * <p>Gets a localized error message for the <code>ErrorCode</code> instance. Error messages are defined in a
84 * <code>error-messages-<locale>.properties</code> (e.g. <code>error-messages-NL.properties</code>). A message
85 * text is identified by a key <code>message.</code> concatenated with {@link #getErrorCodeString}.
86 * <p/>
87 * </p>When no resource bundle can be found for <code>locale</code>, the default locale <code>Locale.ENGLISH</code> is
88 * used.
89 *
90 * @param locale A locale object (e.g. representing the current locale of the user environment).
91 * @return A localized error message or {@link #getErrorCodeString} when no message was found for this errorcode or the
92 * resourcebundle could not be found for <code>locale</code>.
93 */
94 public String getErrorMessage(Locale locale) {
95
96 if (locale == null) {
97 locale = currentLocale;
98 }
99
100 String message = "";
101
102 if (messageBundle == null) {
103 try {
104 locale = Locale.ENGLISH;
105 messageBundle = ResourceBundle.getBundle("error-messages", locale);
106 } catch (MissingResourceException m) {
107 logger.error("No default resource bundle available for locale " + locale);
108 return getErrorCodeString();
109 }
110 }
111
112 try {
113 message = messageBundle.getString("message." + getErrorCodeString());
114
115 if (getMessageArguments().length != 0) {
116 MessageFormat messageFormat = new MessageFormat(message);
117 message = messageFormat.format(getMessageArguments());
118 }
119
120 } catch (RuntimeException r) {
121 logger.error("No message found for errorcode : " + getErrorCodeString());
122 message = getErrorCodeString();
123 }
124
125 if (message.startsWith(getErrorCodeString())) {
126 return message;
127 }
128
129 return getErrorCodeString() + " : " + message;
130 }
131
132 /***
133 * Gets the error message for the current locale.
134 *
135 * @return The error message for the error code.
136 */
137 public String getErrorMessage() {
138 return getErrorMessage(currentLocale);
139 }
140
141 /***
142 * Gets this instance' error code.
143 *
144 * @return This instance' error code.
145 */
146 public String getErrorCodeString() {
147 return errorCode;
148 }
149
150 private Object[] getMessageArguments() {
151 return (messageArguments == null ? new Object[0] : messageArguments);
152 }
153
154 public boolean equals(Object o) {
155 if (this == o) return true;
156 if (!(o instanceof ErrorCode)) return false;
157
158 final ErrorCode errorCode1 = (ErrorCode) o;
159
160 if (errorCode != null ? !errorCode.equals(errorCode1.errorCode) : errorCode1.errorCode != null) return false;
161
162 return true;
163 }
164
165 public int hashCode() {
166 return (errorCode != null ? errorCode.hashCode() : 0);
167 }
168
169 public String toString() {
170 return errorCode;
171 }
172
173 }
174