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