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