View Javadoc

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