1 package com.genimen.djeneric.test.strong; 2 3 import java.io.PrintStream ; 4 import java.io.PrintWriter ; 5 import java.io.Serializable ; 6 import java.io.StringWriter ; 7 8 import com.genimen.djeneric.repository.exceptions.DjenericException; 9 10 public class TestException extends DjenericException implements Serializable 14 { 15 private static final long serialVersionUID = 1L; 16 17 private static final String CHAINED_MSG = "Chained to: "; 18 19 private String _exceptionId = "ERRORTYPE_UNKNOWN"; 20 private String _stackTrace = null; 21 public static String OBJECT_NOT_FOUND = "OBJECT_NOT_FOUND"; 22 public static String MULTIPLE_OBJECTS_FOUND = "MULTIPLE_OBJECTS_FOUND"; 23 transient private boolean _ignoreMyOwnStack = false; 25 public TestException() 26 { 27 storeStack(this); 28 } 29 30 public TestException(Exception chainThis) 31 { 32 super(chainThis.getMessage()); 33 storeStack(chainThis); 34 chain(chainThis); 35 } 36 37 public TestException(String message) 38 { 39 super(message); 40 storeStack(this); 41 } 42 43 public TestException(String message, String exceptionId) 44 { 45 super(message); 46 _exceptionId = exceptionId; 47 storeStack(this); 48 } 49 50 public TestException(String message, String exceptionId, Exception chainThis) 51 { 52 super(message); 53 _exceptionId = exceptionId; 54 storeStack(chainThis); 55 chain(chainThis); 56 } 57 58 private void storeStack(Exception theEx) 59 { 60 _stackTrace = stack2String(theEx); 61 _ignoreMyOwnStack = true; } 63 64 private void chain(Exception chainThis) 65 { 66 if (chainThis instanceof TestException) 67 { 68 TestException bex = (TestException) chainThis; 69 _exceptionId = bex._exceptionId; 70 } 71 } 72 73 public String getExceptionId() 74 { 75 return _exceptionId; 76 } 77 78 81 public void printStackTrace() 82 { 83 if (_stackTrace != null) 84 { 85 System.err.println(_stackTrace); 86 if (!_ignoreMyOwnStack) System.err.print(CHAINED_MSG); 87 } 88 if (!_ignoreMyOwnStack) super.printStackTrace(); 89 } 90 91 96 public void printStackTrace(PrintStream printStream) 97 { 98 if (_stackTrace != null) 99 { 100 printStream.println(_stackTrace); 101 if (!_ignoreMyOwnStack) printStream.print(CHAINED_MSG); 102 } 103 if (!_ignoreMyOwnStack) super.printStackTrace(printStream); 104 } 105 106 111 public void printStackTrace(PrintWriter writer) 112 { 113 if (_stackTrace != null) 114 { 115 writer.println(_stackTrace); 116 if (!_ignoreMyOwnStack) writer.print(CHAINED_MSG); 117 } 118 if (!_ignoreMyOwnStack) super.printStackTrace(writer); 119 } 120 121 public void setExceptionID(String exceptionId) 122 { 123 _exceptionId = exceptionId; 124 } 125 126 public String toString() 127 { 128 String exceptionMessage = getClass().getName() + ": "; 129 130 if (getMessage() != null) exceptionMessage += getMessage() + "\n"; 131 exceptionMessage += "Exception ID = " + _exceptionId + "\n"; 132 return exceptionMessage.trim(); 133 } 134 135 private String stack2String(Exception x) 136 { 137 if (x == null) return ""; 138 139 StringWriter stringWriter = new StringWriter (); 140 java.io.PrintWriter stackTrace = new java.io.PrintWriter (stringWriter); 141 x.printStackTrace(stackTrace); 142 String result = stringWriter.toString().trim(); 143 if (result.length() == 0) result = null; 144 145 return result; 146 } 147 148 } | Popular Tags |