KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > test > strong > TestException


1 package com.genimen.djeneric.test.strong;
2
3 import java.io.PrintStream JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import java.io.Serializable JavaDoc;
6 import java.io.StringWriter JavaDoc;
7
8 import com.genimen.djeneric.repository.exceptions.DjenericException;
9
10 // Place your custom code BETWEEN the following tags
11
//<DjenericManualCode id="1">
12
//</DjenericManualCode>
13
public class TestException extends DjenericException implements Serializable JavaDoc
14 {
15   private static final long serialVersionUID = 1L;
16
17   private static final String JavaDoc CHAINED_MSG = "Chained to: ";
18
19   private String JavaDoc _exceptionId = "ERRORTYPE_UNKNOWN";
20   private String JavaDoc _stackTrace = null;
21   public static String JavaDoc OBJECT_NOT_FOUND = "OBJECT_NOT_FOUND";
22   public static String JavaDoc MULTIPLE_OBJECTS_FOUND = "MULTIPLE_OBJECTS_FOUND";
23   transient private boolean _ignoreMyOwnStack = false; // transient flag to recognize remote (serialized) exceptions
24

25   public TestException()
26   {
27     storeStack(this);
28   }
29
30   public TestException(Exception JavaDoc chainThis)
31   {
32     super(chainThis.getMessage());
33     storeStack(chainThis);
34     chain(chainThis);
35   }
36
37   public TestException(String JavaDoc message)
38   {
39     super(message);
40     storeStack(this);
41   }
42
43   public TestException(String JavaDoc message, String JavaDoc exceptionId)
44   {
45     super(message);
46     _exceptionId = exceptionId;
47     storeStack(this);
48   }
49
50   public TestException(String JavaDoc message, String JavaDoc exceptionId, Exception JavaDoc chainThis)
51   {
52     super(message);
53     _exceptionId = exceptionId;
54     storeStack(chainThis);
55     chain(chainThis);
56   }
57
58   private void storeStack(Exception JavaDoc theEx)
59   {
60     _stackTrace = stack2String(theEx);
61     _ignoreMyOwnStack = true; // transient flag to recognize remote (serialized) exceptions
62
}
63
64   private void chain(Exception JavaDoc chainThis)
65   {
66     if (chainThis instanceof TestException)
67     {
68       TestException bex = (TestException) chainThis;
69       _exceptionId = bex._exceptionId;
70     }
71   }
72
73   public String JavaDoc getExceptionId()
74   {
75     return _exceptionId;
76   }
77
78   /**
79    * Prints the stack trace of the Thrown exception.
80    */

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   /**
92    * Prints the stack trace of the Thrown exception in a printStream.
93    *
94    * @param PrintStream printStream which should be used to print the stacktrace.
95    */

96   public void printStackTrace(PrintStream JavaDoc 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   /**
107    * Prints the stack trace of the thrown exception in a printWriter.
108    *
109    * @param PrintStream printWriter which should be used to print the stacktrace.
110    */

111   public void printStackTrace(PrintWriter JavaDoc 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 JavaDoc exceptionId)
122   {
123     _exceptionId = exceptionId;
124   }
125
126   public String JavaDoc toString()
127   {
128     String JavaDoc 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 JavaDoc stack2String(Exception JavaDoc x)
136   {
137     if (x == null) return "";
138
139     StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
140     java.io.PrintWriter JavaDoc stackTrace = new java.io.PrintWriter JavaDoc(stringWriter);
141     x.printStackTrace(stackTrace);
142     String JavaDoc result = stringWriter.toString().trim();
143     if (result.length() == 0) result = null;
144
145     return result;
146   }
147
148   // Place your custom code BETWEEN the following tags
149
//<DjenericManualCode>
150
//</DjenericManualCode>
151
}
Popular Tags