001: /*
002: * @(#) ConnectionPoolException.java 1.0 02/08/01
003: */
004: package org.smartlib.pool.core;
005:
006: import java.sql.SQLException;
007:
008: /**
009: * This class extends the Exception class and encapsulates any other exception.
010: *
011: * @author Sachin Shekar Shetty
012: * @version 1.0, 02/08/01
013: *
014: */
015:
016: public class ConnectionPoolException extends SQLException {
017:
018: private Throwable previousException = null;
019: protected String message = null;
020: private Throwable nextException;
021: Debugger debug = new Debugger(
022: "org.smartlib.pool.core.ConnectionPoolException", true);
023:
024: ConnectionPoolException() {
025:
026: super ();
027:
028: }
029:
030: ConnectionPoolException(String messageId) {
031:
032: super (messageId);
033: message = messageId;
034: debug.print("Exception Created:" + messageId);
035:
036: }
037:
038: ConnectionPoolException(String messageId, Throwable prevException) {
039:
040: super (messageId);
041: if ((prevException instanceof ConnectionPoolException)
042: || (message == null))
043: message = messageId;
044: this .previousException = prevException;
045: debug.print("Exception Created: " + messageId);
046: debug.writeException(this );
047:
048: }
049:
050: Throwable getPreviousException() {
051:
052: return previousException;
053:
054: }
055:
056: /**
057: * This method prints the stack trace to System.err. It prints the
058: * entire stackTrace of all the previous exceptions that it has
059: * encapsulated.
060: */
061: public void printStackTrace() {
062:
063: super .printStackTrace();
064: if (previousException != null) {
065: System.err.println("Caused by Prev: ");
066: previousException.printStackTrace();
067: }
068: if (nextException != null) {
069: System.err.println("Caused by Next: ");
070: nextException.printStackTrace();
071: }
072:
073: }
074:
075: /**
076: * This method prints the stack trace to <code>ps</code>. It prints the
077: * entire stackTrace of all the previous exceptions that it has
078: * encapsulated.
079: *
080: * @param ps PrintStream into which the stack trace will be written.
081: */
082: public void printStackTrace(java.io.PrintStream ps) {
083:
084: if (ps == null) {
085: System.err
086: .println("PrintStream passed to printStackTrace method is null.");
087: printStackTrace();
088: }
089: super .printStackTrace(ps);
090: if (previousException != null) {
091: ps.println("Caused by:");
092: previousException.printStackTrace(ps);
093: }
094:
095: }
096:
097: /**
098: * This method returns the message id.
099: */
100: public String getMessageId() {
101:
102: if (message == null)
103: return null;
104: int index = message.indexOf(":");
105: int len = message.length();
106:
107: if (index <= 0)
108: return null;
109: return message.substring(0, index);
110:
111: }
112:
113: String getRootMessageId() {
114: ConnectionPoolException exp = null;
115: Throwable prevExp = this ;
116: while ((prevExp != null)
117: && (prevExp instanceof ConnectionPoolException)) {
118: exp = (ConnectionPoolException) prevExp;
119: prevExp = exp.previousException;
120: }
121: if (exp == null)
122: return null;
123: String message = exp.message;
124: if (message == null)
125: return null;
126: int index = message.indexOf(":");
127: int len = message.length();
128:
129: if (index <= 0)
130: return null;
131: return message.substring(0, index);
132:
133: }
134:
135: /**
136: * This method prints the stack trace to <code>pw</code>. It prints the
137: * entire stackTrace of all the previous exceptions that it has
138: * encapsulated.
139: *
140: * @param pw PrintWriter into which the stack trace will be written.
141: */
142: public void printStackTrace(java.io.PrintWriter pw) {
143:
144: if (pw == null) {
145: System.err
146: .println("PrintWriter passed to printStackTrace method is null.");
147: printStackTrace();
148: }
149: super .printStackTrace(pw);
150: if (previousException != null) {
151: pw.println("Caused by:");
152: previousException.printStackTrace(pw);
153: }
154:
155: }
156:
157: }
|