View Javadoc

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