Coverage Report - org.mule.api.MuleException
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleException
0%
0/117
0%
0/42
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.api;
 8  
 
 9  
 import java.io.PrintWriter;
 10  
 import java.io.StringWriter;
 11  
 import java.util.HashMap;
 12  
 import java.util.Iterator;
 13  
 import java.util.Map;
 14  
 
 15  
 import org.mule.config.DefaultMuleConfiguration;
 16  
 import org.mule.config.ExceptionHelper;
 17  
 import org.mule.config.i18n.CoreMessages;
 18  
 import org.mule.config.i18n.Message;
 19  
 import org.mule.config.i18n.MessageFactory;
 20  
 import org.mule.util.StringUtils;
 21  
 import org.mule.util.SystemUtils;
 22  
 
 23  
 /**
 24  
  * <code>MuleException</code> is the base exception type for the Mule server any
 25  
  * other exceptions thrown by Mule code will be based on this exception,
 26  
  */
 27  
 public abstract class MuleException extends Exception
 28  
 {
 29  0
     private final Map info = new HashMap();
 30  0
     private int errorCode = -1;
 31  0
     private String message = null;
 32  
     private Message i18nMessage;
 33  
 
 34  
     /**
 35  
      * @param message the exception message
 36  
      */
 37  
     public MuleException(Message message)
 38  
     {
 39  0
         super();
 40  0
         setMessage(message);
 41  0
     }
 42  
 
 43  
     /**
 44  
      * @param message the exception message
 45  
      * @param cause the exception that cause this exception to be thrown
 46  
      */
 47  
     public MuleException(Message message, Throwable cause)
 48  
     {
 49  0
         super(ExceptionHelper.unwrap(cause));
 50  0
         setMessage(message);
 51  0
     }
 52  
 
 53  
     public MuleException(Throwable cause)
 54  
     {
 55  0
         super(ExceptionHelper.unwrap(cause));
 56  0
         if (cause != null)
 57  
         {
 58  0
             setMessage(MessageFactory.createStaticMessage(cause.getMessage() + " ("
 59  
                                                           + cause.getClass().getName() + ")"));
 60  
         }
 61  
         else
 62  
         {
 63  0
             initialise();
 64  
         }
 65  0
     }
 66  
 
 67  
     protected MuleException()
 68  
     {
 69  0
         super();
 70  0
         initialise();
 71  0
     }
 72  
 
 73  
     protected void setMessage(Message message)
 74  
     {
 75  0
         initialise();
 76  0
         this.message = message.getMessage();
 77  0
         i18nMessage = message;
 78  0
     }
 79  
 
 80  
     protected void setMessage(String message)
 81  
     {
 82  0
         initialise();
 83  0
         this.message = message;
 84  0
         if (i18nMessage == null)
 85  
         {
 86  0
             i18nMessage = MessageFactory.createStaticMessage(message);
 87  
         }
 88  0
     }
 89  
 
 90  
     public int getExceptionCode()
 91  
     {
 92  0
         return errorCode;
 93  
     }
 94  
 
 95  
     public Message getI18nMessage()
 96  
     {
 97  0
         return i18nMessage;
 98  
     }
 99  
 
 100  
     public int getMessageCode()
 101  
     {
 102  0
         return (i18nMessage == null ? 0 : i18nMessage.getCode());
 103  
     }
 104  
 
 105  
     public void addInfo(String name, Object info)
 106  
     {
 107  0
         this.info.put(name, info);
 108  0
     }
 109  
 
 110  
     protected void appendMessage(String s)
 111  
     {
 112  0
         message += s;
 113  0
     }
 114  
 
 115  
     protected void prependMessage(String s)
 116  
     {
 117  0
         message = message + ". " + s;
 118  0
     }
 119  
 
 120  
     protected void setExceptionCode(int code)
 121  
     {
 122  0
         errorCode = code;
 123  0
     }
 124  
 
 125  
     @Override
 126  
     public final String getMessage()
 127  
     {
 128  0
         return message;
 129  
     }
 130  
 
 131  
     protected void initialise()
 132  
     {
 133  0
         setExceptionCode(ExceptionHelper.getErrorCode(getClass()));
 134  0
         String javadoc = ExceptionHelper.getJavaDocUrl(getClass());
 135  0
         String doc = ExceptionHelper.getDocUrl(getClass());
 136  0
         if (javadoc != null)
 137  
         {
 138  
             // info.put(ClassHelper.getClassName(getClass()) + " JavaDoc", javadoc);
 139  0
             info.put("JavaDoc", javadoc);
 140  
         }
 141  0
         if (doc != null)
 142  
         {
 143  
             // info.put(ClassHelper.getClassName(getClass()) + " Other Doc", doc);
 144  0
             info.put("Other Doc", doc);
 145  
         }
 146  0
     }
 147  
 
 148  
     public String getDetailedMessage()
 149  
     {
 150  0
         if (DefaultMuleConfiguration.verboseExceptions)
 151  
         {
 152  0
             return getVerboseMessage();
 153  
         }
 154  
         else
 155  
         {
 156  0
             return getSummaryMessage();
 157  
         }
 158  
     }
 159  
 
 160  
     public String getVerboseMessage()
 161  
     {
 162  0
         MuleException e = ExceptionHelper.getRootMuleException(this);
 163  0
         if (!e.equals(this))
 164  
         {
 165  0
             return getMessage();
 166  
         }
 167  0
         StringBuffer buf = new StringBuffer(1024);
 168  0
         buf.append(SystemUtils.LINE_SEPARATOR).append(StringUtils.repeat('*', 80)).append(
 169  
             SystemUtils.LINE_SEPARATOR);
 170  0
         buf.append("Message               : ").append(message).append(SystemUtils.LINE_SEPARATOR);
 171  0
         buf.append("Type                  : ")
 172  
             .append(getClass().getName())
 173  
             .append(SystemUtils.LINE_SEPARATOR);
 174  0
         buf.append("Code                  : ").append("MULE_ERROR-").append(
 175  
             getExceptionCode() + getMessageCode()).append(SystemUtils.LINE_SEPARATOR);
 176  
         // buf.append("Msg Code :
 177  
         // ").append(getMessageCode()).append(SystemUtils.LINE_SEPARATOR);
 178  
 
 179  0
         Map info = ExceptionHelper.getExceptionInfo(this);
 180  0
         for (Iterator iterator = info.keySet().iterator(); iterator.hasNext();)
 181  
         {
 182  0
             String s = (String) iterator.next();
 183  0
             int pad = 22 - s.length();
 184  0
             buf.append(s);
 185  0
             if (pad > 0)
 186  
             {
 187  0
                 buf.append(StringUtils.repeat(' ', pad));
 188  
             }
 189  0
             buf.append(": ");
 190  0
             buf.append(info.get(s)).append(SystemUtils.LINE_SEPARATOR);
 191  0
         }
 192  
 
 193  
         // print exception stack
 194  0
         buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
 195  0
         buf.append(CoreMessages.exceptionStackIs()).append(SystemUtils.LINE_SEPARATOR);
 196  0
         buf.append(StringUtils.abbreviate(ExceptionHelper.getExceptionStack(this), 5000));
 197  
 
 198  0
         buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
 199  0
         buf.append(CoreMessages.rootStackTrace()).append(SystemUtils.LINE_SEPARATOR);
 200  0
         Throwable root = ExceptionHelper.getRootException(this);
 201  0
         StringWriter w = new StringWriter();
 202  0
         PrintWriter p = new PrintWriter(w);
 203  0
         root.printStackTrace(p);
 204  0
         buf.append(StringUtils.abbreviate(w.toString(), 5000)).append(SystemUtils.LINE_SEPARATOR);
 205  0
         buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
 206  
 
 207  0
         return buf.toString();
 208  
     }
 209  
 
 210  
     public String getSummaryMessage()
 211  
     {
 212  0
         MuleException e = ExceptionHelper.getRootMuleException(this);
 213  0
         if (!e.equals(this))
 214  
         {
 215  0
             return getMessage();
 216  
         }
 217  0
         StringBuffer buf = new StringBuffer(1024);
 218  0
         buf.append(SystemUtils.LINE_SEPARATOR).append(StringUtils.repeat('*', 80)).append(
 219  
             SystemUtils.LINE_SEPARATOR);
 220  0
         buf.append("Message               : ").append(message).append(SystemUtils.LINE_SEPARATOR);
 221  0
         buf.append("Code                  : ").append("MULE_ERROR-").append(
 222  
             getExceptionCode() + getMessageCode()).append(SystemUtils.LINE_SEPARATOR);
 223  
         // print exception stack
 224  0
         buf.append(StringUtils.repeat('-', 80)).append(SystemUtils.LINE_SEPARATOR);
 225  0
         buf.append(CoreMessages.exceptionStackIs()).append(SystemUtils.LINE_SEPARATOR);
 226  0
         buf.append(StringUtils.abbreviate(ExceptionHelper.getExceptionStack(this), 5000));
 227  
 
 228  0
         buf.append(StringUtils.repeat('-', 80)).append(SystemUtils.LINE_SEPARATOR);
 229  0
         buf.append(CoreMessages.rootStackTrace()).append(SystemUtils.LINE_SEPARATOR);
 230  0
         Throwable root = ExceptionHelper.getRootException(this);
 231  0
         Throwable rootSummary = ExceptionHelper.summarise(root, 3);
 232  0
         StringWriter w = new StringWriter();
 233  0
         PrintWriter p = new PrintWriter(w);
 234  0
         rootSummary.printStackTrace(p);
 235  0
         buf.append(StringUtils.abbreviate(w.toString(), 5000));
 236  0
         buf.append(
 237  
             "    + "
 238  
                             + root.getStackTrace().length
 239  
                             + " more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)")
 240  
             .append(SystemUtils.LINE_SEPARATOR);
 241  0
         buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR);
 242  
 
 243  0
         return buf.toString();
 244  
     }
 245  
 
 246  
     @Override
 247  
     public boolean equals(Object o)
 248  
     {
 249  0
         if (this == o)
 250  
         {
 251  0
             return true;
 252  
         }
 253  0
         if (!(o instanceof MuleException))
 254  
         {
 255  0
             return false;
 256  
         }
 257  
 
 258  0
         final MuleException exception = (MuleException) o;
 259  
 
 260  0
         if (errorCode != exception.errorCode)
 261  
         {
 262  0
             return false;
 263  
         }
 264  0
         if (i18nMessage != null ? !i18nMessage.equals(exception.i18nMessage) : exception.i18nMessage != null)
 265  
         {
 266  0
             return false;
 267  
         }
 268  0
         if (message != null ? !message.equals(exception.message) : exception.message != null)
 269  
         {
 270  0
             return false;
 271  
         }
 272  
 
 273  0
         return true;
 274  
     }
 275  
 
 276  
     @Override
 277  
     public int hashCode()
 278  
     {
 279  
         int result;
 280  0
         result = errorCode;
 281  0
         result = 29 * result + (message != null ? message.hashCode() : 0);
 282  0
         result = 29 * result + (i18nMessage != null ? i18nMessage.hashCode() : 0);
 283  0
         return result;
 284  
     }
 285  
 
 286  
     public Map getInfo()
 287  
     {
 288  0
         return info;
 289  
     }
 290  
 }