Coverage Report - org.mule.samples.errorhandler.ExceptionBean
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionBean
0%
0/46
0%
0/7
1.692
 
 1  
 /*
 2  
  * $Id: ExceptionBean.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.samples.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  
     /**
 21  
      * Specific details about the Throwable. For example, for
 22  
      * <tt>FileNotFoundException</tt>, this contains the name of the file that
 23  
      * could not be found.
 24  
      */
 25  
     private String detailMessage;
 26  
 
 27  
     /**
 28  
      * The throwable that caused this throwable to get thrown, or null if this
 29  
      * throwable was not caused by another throwable, or if the causative throwable
 30  
      * is unknown.
 31  
      */
 32  
 
 33  0
     private ExceptionBean cause = null;
 34  
 
 35  
     /**
 36  
      * The stack trace, as returned by getStackTrace().
 37  
      */
 38  
     private String[] stackTrace;
 39  
 
 40  0
     static transient boolean showRootStackOnly = true;
 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, new Object[]{getDetailMessage()});
 79  
                 }
 80  
                 else
 81  
                 {
 82  0
                     t = (Throwable)ClassUtils.instanciateClass(aClass, new Object[]{getDetailMessage(),
 83  
                         cause.toException()});
 84  
                 }
 85  0
                 if (getStackTrace() != null)
 86  
                 {
 87  
                     // t.setStackTrace( getStackTrace());
 88  
                 }
 89  0
                 originalException = t;
 90  
             }
 91  0
             catch (Exception e)
 92  
             {
 93  0
                 throw new InstantiationException("Failed to create Exception from ExceptionBean: "
 94  
                                                  + e.getMessage());
 95  0
             }
 96  
         }
 97  0
         return originalException;
 98  
     }
 99  
 
 100  
     public String getDetailMessage()
 101  
     {
 102  0
         return detailMessage;
 103  
     }
 104  
 
 105  
     public void setDetailMessage(String detailMessage)
 106  
     {
 107  0
         this.detailMessage = detailMessage;
 108  0
     }
 109  
 
 110  
     public ExceptionBean getCause()
 111  
     {
 112  0
         return cause;
 113  
     }
 114  
 
 115  
     public void setCause(ExceptionBean cause)
 116  
     {
 117  0
         this.cause = cause;
 118  0
     }
 119  
 
 120  
     public String[] /* List */
 121  
     getStackTrace()
 122  
     {
 123  0
         return stackTrace;
 124  
     }
 125  
 
 126  
     // public void addStackTrace(String trace)
 127  
     // {
 128  
     // stackTrace.add(trace);
 129  
     // }
 130  
 
 131  
     public void setStackTrace(StackTraceElement[] stackTrace)
 132  
     {
 133  0
         this.stackTrace = getStackAsString(stackTrace);
 134  0
     }
 135  
 
 136  
     public void setStackTrace(String[] stackTrace)
 137  
     {
 138  0
         this.stackTrace = stackTrace;
 139  0
     }
 140  
 
 141  
     public String getExceptionClass()
 142  
     {
 143  0
         return exceptionClass;
 144  
     }
 145  
 
 146  
     public void setExceptionClass(String exceptionClass)
 147  
     {
 148  0
         this.exceptionClass = exceptionClass;
 149  0
     }
 150  
 
 151  
     protected String[] getStackAsString(java.lang.StackTraceElement[] elements)
 152  
     {
 153  0
         String[] trace = new String[elements.length];
 154  0
         for (int i = 0; i < elements.length; i++)
 155  
         {
 156  0
             trace[i] = elements[i].toString();
 157  
         }
 158  0
         return trace;
 159  
     }
 160  
 }