1
2
3
4
5
6
7
8
9
10
11 package org.mule.api;
12
13 import org.mule.config.ExceptionHelper;
14 import org.mule.config.i18n.CoreMessages;
15 import org.mule.config.i18n.Message;
16 import org.mule.config.i18n.MessageFactory;
17 import org.mule.util.StringUtils;
18 import org.mule.util.SystemUtils;
19
20 import java.io.PrintWriter;
21 import java.io.StringWriter;
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27
28
29
30
31 public abstract class MuleException extends Exception
32 {
33 private 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((cause instanceof InvocationTargetException
54 ? ((InvocationTargetException) cause).getTargetException() : cause));
55 setMessage(message);
56 }
57
58 public MuleException(Throwable cause)
59 {
60 super((cause instanceof InvocationTargetException
61 ? ((InvocationTargetException) cause).getTargetException() : cause));
62 if (cause != null)
63 {
64 setMessage(MessageFactory.createStaticMessage(cause.getMessage()
65 + " (" + cause.getClass().getName() + ")"));
66 }
67 else
68 {
69 initialise();
70 }
71 }
72
73 protected MuleException()
74 {
75 super();
76 initialise();
77 }
78
79 protected void setMessage(Message message)
80 {
81 initialise();
82 this.message = message.getMessage();
83 i18nMessage = message;
84 }
85
86 protected void setMessage(String message)
87 {
88 initialise();
89 this.message = message;
90 if (i18nMessage == null)
91 {
92 i18nMessage = MessageFactory.createStaticMessage(message);
93 }
94 }
95
96 public int getExceptionCode()
97 {
98 return errorCode;
99 }
100
101 public Message getI18nMessage()
102 {
103 return i18nMessage;
104 }
105
106 public int getMessageCode()
107 {
108 return (i18nMessage == null ? 0 : i18nMessage.getCode());
109 }
110
111 public void addInfo(String name, Object info)
112 {
113 this.info.put(name, info);
114 }
115
116 protected void appendMessage(String s)
117 {
118 message += s;
119 }
120
121 protected void prependMessage(String s)
122 {
123 message = message + ". " + s;
124 }
125
126 protected void setExceptionCode(int code)
127 {
128 errorCode = code;
129 }
130
131 public final String getMessage()
132 {
133 return message;
134 }
135
136 protected void initialise()
137 {
138 setExceptionCode(ExceptionHelper.getErrorCode(getClass()));
139 String javadoc = ExceptionHelper.getJavaDocUrl(getClass());
140 String doc = ExceptionHelper.getDocUrl(getClass());
141 if (javadoc != null)
142 {
143
144 info.put("JavaDoc", javadoc);
145 }
146 if (doc != null)
147 {
148
149 info.put("Other Doc", doc);
150 }
151 }
152
153 public String getDetailedMessage()
154 {
155 MuleException e = ExceptionHelper.getRootMuleException(this);
156 if (!e.equals(this))
157 {
158 return getMessage();
159 }
160 StringBuffer buf = new StringBuffer(1024);
161 buf.append(SystemUtils.LINE_SEPARATOR).append(StringUtils.repeat('*', 80)).append(
162 SystemUtils.LINE_SEPARATOR);
163 buf.append("Message : ").append(message).append(SystemUtils.LINE_SEPARATOR);
164 buf.append("Type : ")
165 .append(getClass().getName())
166 .append(SystemUtils.LINE_SEPARATOR);
167 buf.append("Code : ").append("MULE_ERROR-").append(
168 getExceptionCode() + getMessageCode()).append(SystemUtils.LINE_SEPARATOR);
169
170
171
172 Map info = ExceptionHelper.getExceptionInfo(this);
173 for (Iterator iterator = info.keySet().iterator(); iterator.hasNext();)
174 {
175 String s = (String) iterator.next();
176 int pad = 22 - s.length();
177 buf.append(s);
178 if (pad > 0)
179 {
180 buf.append(StringUtils.repeat(' ', pad));
181 }
182 buf.append(": ");
183 buf.append(info.get(s)).append(SystemUtils.LINE_SEPARATOR);
184 }
185
186
187 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
188 buf.append(CoreMessages.exceptionStackIs()).append(SystemUtils.LINE_SEPARATOR);
189 buf.append(ExceptionHelper.getExceptionStack(this));
190
191 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
192 buf.append(CoreMessages.rootStackTrace()).append(SystemUtils.LINE_SEPARATOR);
193 Throwable root = ExceptionHelper.getRootException(this);
194 StringWriter w = new StringWriter();
195 PrintWriter p = new PrintWriter(w);
196 root.printStackTrace(p);
197 buf.append(w.toString()).append(SystemUtils.LINE_SEPARATOR);
198 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
199
200 return buf.toString();
201 }
202
203 public boolean equals(Object o)
204 {
205 if (this == o)
206 {
207 return true;
208 }
209 if (!(o instanceof MuleException))
210 {
211 return false;
212 }
213
214 final MuleException umoException = (MuleException) o;
215
216 if (errorCode != umoException.errorCode)
217 {
218 return false;
219 }
220 if (i18nMessage != null
221 ? !i18nMessage.equals(umoException.i18nMessage) : umoException.i18nMessage != null)
222 {
223 return false;
224 }
225 if (message != null ? !message.equals(umoException.message) : umoException.message != null)
226 {
227 return false;
228 }
229
230 return true;
231 }
232
233 public int hashCode()
234 {
235 int result;
236 result = errorCode;
237 result = 29 * result + (message != null ? message.hashCode() : 0);
238 result = 29 * result + (i18nMessage != null ? i18nMessage.hashCode() : 0);
239 return result;
240 }
241
242 public Map getInfo()
243 {
244 return info;
245 }
246 }