KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > umo > UMOException


1 /*
2  * $Id: UMOException.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.umo;
12
13 import org.apache.commons.lang.SystemUtils;
14 import org.mule.config.ExceptionHelper;
15 import org.mule.config.i18n.Message;
16 import org.mule.config.i18n.Messages;
17 import org.mule.util.StringUtils;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * <code>UMOException</code> is the base exception type for the Mule server any
28  * other exceptions thrown by Mule code will be based on this exception
29  *
30  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
31  * @version $Revision: 3798 $
32  */

33 public abstract class UMOException extends Exception JavaDoc
34 {
35     private Map JavaDoc info = new HashMap JavaDoc();
36     private int errorCode = -1;
37     private String JavaDoc message = null;
38     private Message i18nMessage;
39
40     /**
41      * @param message the exception message
42      */

43     public UMOException(Message message)
44     {
45         super();
46         setMessage(message);
47     }
48
49     /**
50      * @param message the exception message
51      * @param cause the exception that cause this exception to be thrown
52      */

53     public UMOException(Message message, Throwable JavaDoc cause)
54     {
55         super((cause instanceof InvocationTargetException JavaDoc
56                         ? ((InvocationTargetException JavaDoc)cause).getTargetException() : cause));
57         setMessage(message);
58     }
59
60     public UMOException(Throwable JavaDoc cause)
61     {
62         super((cause instanceof InvocationTargetException JavaDoc
63                         ? ((InvocationTargetException JavaDoc)cause).getTargetException() : cause));
64         setMessage(Message.createStaticMessage(cause.getMessage() + " (" + cause.getClass().getName() + ")"));
65         initialise();
66     }
67
68     protected UMOException()
69     {
70         super();
71         initialise();
72     }
73
74     protected void setMessage(Message message)
75     {
76         initialise();
77         this.message = message.getMessage();
78         i18nMessage = message;
79     }
80
81     protected void setMessage(String JavaDoc message)
82     {
83         initialise();
84         this.message = message;
85         if (i18nMessage == null)
86         {
87             i18nMessage = Message.createStaticMessage(message);
88         }
89     }
90
91     public int getExceptionCode()
92     {
93         return errorCode;
94     }
95
96     public Message getI18nMessage()
97     {
98         return i18nMessage;
99     }
100
101     public int getMessageCode()
102     {
103         return (i18nMessage == null ? 0 : i18nMessage.getCode());
104     }
105
106     public void addInfo(String JavaDoc name, Object JavaDoc info)
107     {
108         this.info.put(name, info);
109     }
110
111     protected void appendMessage(String JavaDoc s)
112     {
113         message += s;
114     }
115
116     protected void prependMessage(String JavaDoc s)
117     {
118         message = message + ". " + s;
119     }
120
121     protected void setExceptionCode(int code)
122     {
123         errorCode = code;
124     }
125
126     public final String JavaDoc getMessage()
127     {
128         return message;
129     }
130
131     protected void initialise()
132     {
133         setExceptionCode(ExceptionHelper.getErrorCode(getClass()));
134         String JavaDoc javadoc = ExceptionHelper.getJavaDocUrl(getClass());
135         String JavaDoc doc = ExceptionHelper.getDocUrl(getClass());
136         if (javadoc != null)
137         {
138             // info.put(ClassHelper.getClassName(getClass()) + " JavaDoc", javadoc);
139
info.put("JavaDoc", javadoc);
140         }
141         if (doc != null)
142         {
143             // info.put(ClassHelper.getClassName(getClass()) + " Other Doc", doc);
144
info.put("Other Doc", doc);
145         }
146     }
147
148     public String JavaDoc getDetailedMessage()
149     {
150         UMOException e = ExceptionHelper.getRootMuleException(this);
151         if (!e.equals(this))
152         {
153             return getMessage();
154         }
155         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(1024);
156         buf.append(SystemUtils.LINE_SEPARATOR).append(StringUtils.repeat('*', 80)).append(
157             SystemUtils.LINE_SEPARATOR);
158         buf.append("Message : ").append(message).append(SystemUtils.LINE_SEPARATOR);
159         buf.append("Type : ")
160             .append(getClass().getName())
161             .append(SystemUtils.LINE_SEPARATOR);
162         buf.append("Code : ").append("MULE_ERROR-").append(
163             getExceptionCode() + getMessageCode()).append(SystemUtils.LINE_SEPARATOR);
164         // buf.append("Msg Code :
165
// ").append(getMessageCode()).append(SystemUtils.LINE_SEPARATOR);
166

167         Map JavaDoc info = ExceptionHelper.getExceptionInfo(this);
168         for (Iterator JavaDoc iterator = info.keySet().iterator(); iterator.hasNext();)
169         {
170             String JavaDoc s = (String JavaDoc)iterator.next();
171             int pad = 22 - s.length();
172             buf.append(s);
173             if (pad > 0)
174             {
175                 buf.append(StringUtils.repeat(' ', pad));
176             }
177             buf.append(": ");
178             buf.append(info.get(s)).append(SystemUtils.LINE_SEPARATOR);
179         }
180
181         // print exception stack
182
buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
183         buf.append(new Message(Messages.EXCEPTION_STACK_IS)).append(SystemUtils.LINE_SEPARATOR);
184         buf.append(ExceptionHelper.getExceptionStack(this));
185
186         buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
187         buf.append(new Message(Messages.ROOT_STACK_TRACE)).append(SystemUtils.LINE_SEPARATOR);
188         Throwable JavaDoc root = ExceptionHelper.getRootException(this);
189         StringWriter JavaDoc w = new StringWriter JavaDoc();
190         PrintWriter JavaDoc p = new PrintWriter JavaDoc(w);
191         root.printStackTrace(p);
192         buf.append(w.toString()).append(SystemUtils.LINE_SEPARATOR);
193         buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
194
195         return buf.toString();
196     }
197
198     public boolean equals(Object JavaDoc o)
199     {
200         if (this == o)
201         {
202             return true;
203         }
204         if (!(o instanceof UMOException))
205         {
206             return false;
207         }
208
209         final UMOException umoException = (UMOException)o;
210
211         if (errorCode != umoException.errorCode)
212         {
213             return false;
214         }
215         if (i18nMessage != null
216                         ? !i18nMessage.equals(umoException.i18nMessage) : umoException.i18nMessage != null)
217         {
218             return false;
219         }
220         if (message != null ? !message.equals(umoException.message) : umoException.message != null)
221         {
222             return false;
223         }
224
225         return true;
226     }
227
228     public int hashCode()
229     {
230         int result;
231         result = errorCode;
232         result = 29 * result + (message != null ? message.hashCode() : 0);
233         result = 29 * result + (i18nMessage != null ? i18nMessage.hashCode() : 0);
234         return result;
235     }
236
237     public Map JavaDoc getInfo()
238     {
239         return info;
240     }
241 }
242
Popular Tags