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