1 /***************************************
2 * *
3 * JBoss: The OpenSource J2EE WebOS *
4 * *
5 * Distributable under LGPL license. *
6 * See terms of license at gnu.org. *
7 * *
8 ***************************************/
9
10 package org.jboss.util;
11
12 import java.io.PrintWriter;
13 import java.io.PrintStream;
14
15 import java.sql.SQLException;
16
17 /**
18 * A common superclass for <tt>SQLException</tt> classes that can contain
19 * a nested <tt>Throwable</tt> detail object.
20 *
21 * @version <tt>$Revision: 1.2 $</tt>
22 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
23 */
24 public class NestedSQLException
25 extends SQLException
26 implements NestedThrowable
27 {
28 /** The nested throwable */
29 protected final Throwable nested;
30
31 /**
32 * Construct a <tt>NestedSQLException</tt> with the specified detail
33 * message.
34 *
35 * @param msg Detail message.
36 */
37 public NestedSQLException(final String msg) {
38 super(msg);
39 this.nested = null;
40 }
41
42 /**
43 * Construct a <tt>NestedSQLException</tt> with the specified detail
44 * message and nested <tt>Throwable</tt>.
45 *
46 * @param msg Detail message.
47 * @param nested Nested <tt>Throwable</tt>.
48 */
49 public NestedSQLException(final String msg, final Throwable nested) {
50 super(msg);
51 this.nested = nested;
52 NestedThrowable.Util.checkNested(this, nested);
53 }
54
55 /**
56 * Construct a <tt>NestedSQLException</tt> with the specified
57 * nested <tt>Throwable</tt>.
58 *
59 * @param nested Nested <tt>Throwable</tt>.
60 */
61 public NestedSQLException(final Throwable nested) {
62 this(nested.getMessage(), nested);
63 }
64
65 /**
66 * Construct a <tt>NestedSQLException</tt>.
67 *
68 * @param msg Detail message.
69 * @param state SQL state message.
70 */
71 public NestedSQLException(final String msg, final String state) {
72 super(msg, state);
73 this.nested = null;
74 }
75
76 /**
77 * Construct a <tt>NestedSQLException</tt>.
78 *
79 * @param msg Detail message.
80 * @param state SQL state message.
81 * @param code SQL vendor code.
82 */
83 public NestedSQLException(final String msg, final String state, final int code) {
84 super(msg, state, code);
85 this.nested = null;
86 }
87
88 /**
89 * Return the nested <tt>Throwable</tt>.
90 *
91 * @return Nested <tt>Throwable</tt>.
92 */
93 public Throwable getNested() {
94 return nested;
95 }
96
97 /**
98 * Return the nested <tt>Throwable</tt>.
99 *
100 * <p>For JDK 1.4 compatibility.
101 *
102 * @return Nested <tt>Throwable</tt>.
103 */
104 public Throwable getCause() {
105 return nested;
106 }
107
108 /**
109 * Returns the composite throwable message.
110 *
111 * @return The composite throwable message.
112 */
113 public String getMessage() {
114 return NestedThrowable.Util.getMessage(super.getMessage(), nested);
115 }
116
117 /**
118 * Prints the composite message and the embedded stack trace to the
119 * specified print stream.
120 *
121 * @param stream Stream to print to.
122 */
123 public void printStackTrace(final PrintStream stream) {
124 if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED) {
125 super.printStackTrace(stream);
126 }
127 NestedThrowable.Util.print(nested, stream);
128 }
129
130 /**
131 * Prints the composite message and the embedded stack trace to the
132 * specified print writer.
133 *
134 * @param writer Writer to print to.
135 */
136 public void printStackTrace(final PrintWriter writer) {
137 if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED) {
138 super.printStackTrace(writer);
139 }
140 NestedThrowable.Util.print(nested, writer);
141 }
142
143 /**
144 * Prints the composite message and the embedded stack trace to
145 * <tt>System.err</tt>.
146 */
147 public void printStackTrace() {
148 printStackTrace(System.err);
149 }
150 }