1 10 11 package org.mule.samples.errorhandler; 12 13 import org.mule.util.ClassUtils; 14 15 21 public class ExceptionBean 22 { 23 28 private String detailMessage; 29 30 35 36 private ExceptionBean cause = null; 37 38 41 private String [] stackTrace; 42 43 static transient boolean showRootStackOnly = true; 44 45 private String exceptionClass = null; 46 47 private Throwable originalException = null; 48 49 public ExceptionBean() 50 { 51 super(); 52 } 53 54 public ExceptionBean(Throwable exception) 55 { 56 if (exception == null) throw new IllegalArgumentException ("The exception cannot be null"); 57 originalException = exception; 58 exceptionClass = exception.getClass().getName(); 59 setDetailMessage(exception.getMessage()); 60 setStackTrace((showRootStackOnly ? null : getStackAsString(exception.getStackTrace()))); 61 if (exception.getCause() != null) 62 { 63 setCause(new ExceptionBean(exception.getCause())); 64 } 65 else 66 { 67 setStackTrace(exception.getStackTrace()); 68 } 69 } 70 71 public Throwable toException() throws InstantiationException 72 { 73 if (originalException == null) 74 { 75 Throwable t = null; 76 try 77 { 78 Class aClass = ClassUtils.loadClass(exceptionClass, getClass()); 79 if (cause == null) 80 { 81 t = (Throwable )ClassUtils.instanciateClass(aClass, new Object []{getDetailMessage()}); 82 } 83 else 84 { 85 t = (Throwable )ClassUtils.instanciateClass(aClass, new Object []{getDetailMessage(), 86 cause.toException()}); 87 } 88 if (getStackTrace() != null) 89 { 90 } 92 originalException = t; 93 } 94 catch (Exception e) 95 { 96 throw new InstantiationException ("Failed to create Exception from ExceptionBean: " 97 + e.getMessage()); 98 } 99 } 100 return originalException; 101 } 102 103 public String getDetailMessage() 104 { 105 return detailMessage; 106 } 107 108 public void setDetailMessage(String detailMessage) 109 { 110 this.detailMessage = detailMessage; 111 } 112 113 public ExceptionBean getCause() 114 { 115 return cause; 116 } 117 118 public void setCause(ExceptionBean cause) 119 { 120 this.cause = cause; 121 } 122 123 public String [] 124 getStackTrace() 125 { 126 return stackTrace; 127 } 128 129 134 public void setStackTrace(StackTraceElement [] stackTrace) 135 { 136 this.stackTrace = getStackAsString(stackTrace); 137 } 138 139 public void setStackTrace(String [] stackTrace) 140 { 141 this.stackTrace = stackTrace; 142 } 143 144 public String getExceptionClass() 145 { 146 return exceptionClass; 147 } 148 149 public void setExceptionClass(String exceptionClass) 150 { 151 this.exceptionClass = exceptionClass; 152 } 153 154 protected String [] getStackAsString(java.lang.StackTraceElement [] elements) 155 { 156 String [] trace = new String [elements.length]; 157 for (int i = 0; i < elements.length; i++) 158 { 159 trace[i] = elements[i].toString(); 160 } 161 return trace; 162 } 163 } 164 | Popular Tags |