1
2
3
4
5
6
7 package org.mule.api;
8
9 import java.io.PrintWriter;
10 import java.io.StringWriter;
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.Map;
14
15 import org.mule.config.DefaultMuleConfiguration;
16 import org.mule.config.ExceptionHelper;
17 import org.mule.config.i18n.CoreMessages;
18 import org.mule.config.i18n.Message;
19 import org.mule.config.i18n.MessageFactory;
20 import org.mule.util.StringUtils;
21 import org.mule.util.SystemUtils;
22
23
24
25
26
27 public abstract class MuleException extends Exception
28 {
29 private final Map info = new HashMap();
30 private int errorCode = -1;
31 private String message = null;
32 private Message i18nMessage;
33
34
35
36
37 public MuleException(Message message)
38 {
39 super();
40 setMessage(message);
41 }
42
43
44
45
46
47 public MuleException(Message message, Throwable cause)
48 {
49 super(ExceptionHelper.unwrap(cause));
50 setMessage(message);
51 }
52
53 public MuleException(Throwable cause)
54 {
55 super(ExceptionHelper.unwrap(cause));
56 if (cause != null)
57 {
58 setMessage(MessageFactory.createStaticMessage(cause.getMessage() + " ("
59 + cause.getClass().getName() + ")"));
60 }
61 else
62 {
63 initialise();
64 }
65 }
66
67 protected MuleException()
68 {
69 super();
70 initialise();
71 }
72
73 protected void setMessage(Message message)
74 {
75 initialise();
76 this.message = message.getMessage();
77 i18nMessage = message;
78 }
79
80 protected void setMessage(String message)
81 {
82 initialise();
83 this.message = message;
84 if (i18nMessage == null)
85 {
86 i18nMessage = MessageFactory.createStaticMessage(message);
87 }
88 }
89
90 public int getExceptionCode()
91 {
92 return errorCode;
93 }
94
95 public Message getI18nMessage()
96 {
97 return i18nMessage;
98 }
99
100 public int getMessageCode()
101 {
102 return (i18nMessage == null ? 0 : i18nMessage.getCode());
103 }
104
105 public void addInfo(String name, Object info)
106 {
107 this.info.put(name, info);
108 }
109
110 protected void appendMessage(String s)
111 {
112 message += s;
113 }
114
115 protected void prependMessage(String s)
116 {
117 message = message + ". " + s;
118 }
119
120 protected void setExceptionCode(int code)
121 {
122 errorCode = code;
123 }
124
125 @Override
126 public final String getMessage()
127 {
128 return message;
129 }
130
131 protected void initialise()
132 {
133 setExceptionCode(ExceptionHelper.getErrorCode(getClass()));
134 String javadoc = ExceptionHelper.getJavaDocUrl(getClass());
135 String doc = ExceptionHelper.getDocUrl(getClass());
136 if (javadoc != null)
137 {
138
139 info.put("JavaDoc", javadoc);
140 }
141 if (doc != null)
142 {
143
144 info.put("Other Doc", doc);
145 }
146 }
147
148 public String getDetailedMessage()
149 {
150 if (DefaultMuleConfiguration.verboseExceptions)
151 {
152 return getVerboseMessage();
153 }
154 else
155 {
156 return getSummaryMessage();
157 }
158 }
159
160 public String getVerboseMessage()
161 {
162 MuleException e = ExceptionHelper.getRootMuleException(this);
163 if (!e.equals(this))
164 {
165 return getMessage();
166 }
167 StringBuffer buf = new StringBuffer(1024);
168 buf.append(SystemUtils.LINE_SEPARATOR).append(StringUtils.repeat('*', 80)).append(
169 SystemUtils.LINE_SEPARATOR);
170 buf.append("Message : ").append(message).append(SystemUtils.LINE_SEPARATOR);
171 buf.append("Type : ")
172 .append(getClass().getName())
173 .append(SystemUtils.LINE_SEPARATOR);
174 buf.append("Code : ").append("MULE_ERROR-").append(
175 getExceptionCode() + getMessageCode()).append(SystemUtils.LINE_SEPARATOR);
176
177
178
179 Map info = ExceptionHelper.getExceptionInfo(this);
180 for (Iterator iterator = info.keySet().iterator(); iterator.hasNext();)
181 {
182 String s = (String) iterator.next();
183 int pad = 22 - s.length();
184 buf.append(s);
185 if (pad > 0)
186 {
187 buf.append(StringUtils.repeat(' ', pad));
188 }
189 buf.append(": ");
190 buf.append(info.get(s)).append(SystemUtils.LINE_SEPARATOR);
191 }
192
193
194 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
195 buf.append(CoreMessages.exceptionStackIs()).append(SystemUtils.LINE_SEPARATOR);
196 buf.append(StringUtils.abbreviate(ExceptionHelper.getExceptionStack(this), 5000));
197
198 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
199 buf.append(CoreMessages.rootStackTrace()).append(SystemUtils.LINE_SEPARATOR);
200 Throwable root = ExceptionHelper.getRootException(this);
201 StringWriter w = new StringWriter();
202 PrintWriter p = new PrintWriter(w);
203 root.printStackTrace(p);
204 buf.append(StringUtils.abbreviate(w.toString(), 5000)).append(SystemUtils.LINE_SEPARATOR);
205 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
206
207 return buf.toString();
208 }
209
210 public String getSummaryMessage()
211 {
212 MuleException e = ExceptionHelper.getRootMuleException(this);
213 if (!e.equals(this))
214 {
215 return getMessage();
216 }
217 StringBuffer buf = new StringBuffer(1024);
218 buf.append(SystemUtils.LINE_SEPARATOR).append(StringUtils.repeat('*', 80)).append(
219 SystemUtils.LINE_SEPARATOR);
220 buf.append("Message : ").append(message).append(SystemUtils.LINE_SEPARATOR);
221 buf.append("Code : ").append("MULE_ERROR-").append(
222 getExceptionCode() + getMessageCode()).append(SystemUtils.LINE_SEPARATOR);
223
224 buf.append(StringUtils.repeat('-', 80)).append(SystemUtils.LINE_SEPARATOR);
225 buf.append(CoreMessages.exceptionStackIs()).append(SystemUtils.LINE_SEPARATOR);
226 buf.append(StringUtils.abbreviate(ExceptionHelper.getExceptionStack(this), 5000));
227
228 buf.append(StringUtils.repeat('-', 80)).append(SystemUtils.LINE_SEPARATOR);
229 buf.append(CoreMessages.rootStackTrace()).append(SystemUtils.LINE_SEPARATOR);
230 Throwable root = ExceptionHelper.getRootException(this);
231 Throwable rootSummary = ExceptionHelper.summarise(root, 3);
232 StringWriter w = new StringWriter();
233 PrintWriter p = new PrintWriter(w);
234 rootSummary.printStackTrace(p);
235 buf.append(StringUtils.abbreviate(w.toString(), 5000));
236 buf.append(
237 " + "
238 + root.getStackTrace().length
239 + " more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)")
240 .append(SystemUtils.LINE_SEPARATOR);
241 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
242
243 return buf.toString();
244 }
245
246 @Override
247 public boolean equals(Object o)
248 {
249 if (this == o)
250 {
251 return true;
252 }
253 if (!(o instanceof MuleException))
254 {
255 return false;
256 }
257
258 final MuleException exception = (MuleException) o;
259
260 if (errorCode != exception.errorCode)
261 {
262 return false;
263 }
264 if (i18nMessage != null ? !i18nMessage.equals(exception.i18nMessage) : exception.i18nMessage != null)
265 {
266 return false;
267 }
268 if (message != null ? !message.equals(exception.message) : exception.message != null)
269 {
270 return false;
271 }
272
273 return true;
274 }
275
276 @Override
277 public int hashCode()
278 {
279 int result;
280 result = errorCode;
281 result = 29 * result + (message != null ? message.hashCode() : 0);
282 result = 29 * result + (i18nMessage != null ? i18nMessage.hashCode() : 0);
283 return result;
284 }
285
286 public Map getInfo()
287 {
288 return info;
289 }
290 }