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