Coverage Report - org.mule.example.errorhandler.ExceptionBean
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionBean
0%
0/46
0%
0/14
1.692
 
 1  
 /*
 2  
  * $Id: ExceptionBean.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.example.errorhandler;
 12  
 
 13  
 import org.mule.util.ClassUtils;
 14  
 
 15  
 /**
 16  
  * The <code>ExceptionBean</code> is a POJO representing the details of a Throwable
 17  
  */
 18  
 public class ExceptionBean
 19  
 {
 20  0
     private static boolean showRootStackOnly = true;
 21  
 
 22  
     /**
 23  
      * Specific details about the Throwable. For example, for
 24  
      * <tt>FileNotFoundException</tt>, this contains the name of the file that
 25  
      * could not be found.
 26  
      */
 27  
     private String detailMessage;
 28  
 
 29  
     /**
 30  
      * The throwable that caused this throwable to get thrown, or null if this
 31  
      * throwable was not caused by another throwable, or if the causative throwable
 32  
      * is unknown.
 33  
      */
 34  
 
 35  0
     private ExceptionBean cause = null;
 36  
 
 37  
     /**
 38  
      * The stack trace, as returned by getStackTrace().
 39  
      */
 40  
     private String[] stackTrace;
 41  
 
 42  0
     private String exceptionClass = null;
 43  
 
 44  0
     private Throwable originalException = null;
 45  
 
 46  
     public ExceptionBean()
 47  
     {
 48  0
         super();
 49  0
     }
 50  
 
 51  
     public ExceptionBean(Throwable exception)
 52  0
     {
 53  0
         if (exception == null) throw new IllegalArgumentException("The exception cannot be null");
 54  0
         originalException = exception;
 55  0
         exceptionClass = exception.getClass().getName();
 56  0
         setDetailMessage(exception.getMessage());
 57  0
         setStackTrace((showRootStackOnly ? null : getStackAsString(exception.getStackTrace())));
 58  0
         if (exception.getCause() != null)
 59  
         {
 60  0
             setCause(new ExceptionBean(exception.getCause()));
 61  
         }
 62  
         else
 63  
         {
 64  0
             setStackTrace(exception.getStackTrace());
 65  
         }
 66  0
     }
 67  
 
 68  
     public Throwable toException() throws InstantiationException
 69  
     {
 70  0
         if (originalException == null)
 71  
         {
 72  0
             Throwable t = null;
 73  
             try
 74  
             {
 75  0
                 Class aClass = ClassUtils.loadClass(exceptionClass, getClass());
 76  0
                 if (cause == null)
 77  
                 {
 78  0
                     t = (Throwable)ClassUtils.instanciateClass(aClass, getDetailMessage());
 79  
                 }
 80  
                 else
 81  
                 {
 82  0
                     t = (Throwable)ClassUtils.instanciateClass(aClass, getDetailMessage(), cause.toException());
 83  
                 }
 84  0
                 if (getStackTrace() != null)
 85  
                 {
 86  
                     // t.setStackTrace( getStackTrace());
 87  
                 }
 88  0
                 originalException = t;
 89  
             }
 90  0
             catch (Exception e)
 91  
             {
 92  0
                 throw new InstantiationException("Failed to create Exception from ExceptionBean: "
 93  
                                                  + e.getMessage());
 94  0
             }
 95  
         }
 96  0
         return originalException;
 97  
     }
 98  
 
 99  
     public String getDetailMessage()
 100  
     {
 101  0
         return detailMessage;
 102  
     }
 103  
 
 104  
     public void setDetailMessage(String detailMessage)
 105  
     {
 106  0
         this.detailMessage = detailMessage;
 107  0
     }
 108  
 
 109  
     public ExceptionBean getCause()
 110  
     {
 111  0
         return cause;
 112  
     }
 113  
 
 114  
     public void setCause(ExceptionBean cause)
 115  
     {
 116  0
         this.cause = cause;
 117  0
     }
 118  
 
 119  
     public String[] /* List */
 120  
     getStackTrace()
 121  
     {
 122  0
         return stackTrace;
 123  
     }
 124  
 
 125  
     // public void addStackTrace(String trace)
 126  
     // {
 127  
     // stackTrace.add(trace);
 128  
     // }
 129  
 
 130  
     public void setStackTrace(StackTraceElement[] stackTrace)
 131  
     {
 132  0
         this.stackTrace = getStackAsString(stackTrace);
 133  0
     }
 134  
 
 135  
     public void setStackTrace(String[] stackTrace)
 136  
     {
 137  0
         this.stackTrace = stackTrace;
 138  0
     }
 139  
 
 140  
     public String getExceptionClass()
 141  
     {
 142  0
         return exceptionClass;
 143  
     }
 144  
 
 145  
     public void setExceptionClass(String exceptionClass)
 146  
     {
 147  0
         this.exceptionClass = exceptionClass;
 148  0
     }
 149  
 
 150  
     protected String[] getStackAsString(java.lang.StackTraceElement[] elements)
 151  
     {
 152  0
         String[] trace = new String[elements.length];
 153  0
         for (int i = 0; i < elements.length; i++)
 154  
         {
 155  0
             trace[i] = elements[i].toString();
 156  
         }
 157  0
         return trace;
 158  
     }
 159  
 }