KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > NestedSQLException


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.PrintStream JavaDoc;
26
27 import java.sql.SQLException JavaDoc;
28
29 /**
30  * A common superclass for <tt>SQLException</tt> classes that can contain
31  * a nested <tt>Throwable</tt> detail object.
32  *
33  * @version <tt>$Revision: 1958 $</tt>
34  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
35  */

36 public class NestedSQLException
37    extends SQLException JavaDoc
38    implements NestedThrowable
39 {
40    /** The nested throwable */
41    protected final Throwable JavaDoc nested;
42
43    /**
44     * Construct a <tt>NestedSQLException</tt> with the specified detail
45     * message.
46     *
47     * @param msg Detail message.
48     */

49    public NestedSQLException(final String JavaDoc msg) {
50       super(msg);
51       this.nested = null;
52    }
53
54    /**
55     * Construct a <tt>NestedSQLException</tt> with the specified detail
56     * message and nested <tt>Throwable</tt>.
57     *
58     * @param msg Detail message.
59     * @param nested Nested <tt>Throwable</tt>.
60     */

61    public NestedSQLException(final String JavaDoc msg, final Throwable JavaDoc nested) {
62       super(msg);
63       this.nested = nested;
64       NestedThrowable.Util.checkNested(this, nested);
65    }
66
67    /**
68     * Construct a <tt>NestedSQLException</tt> with the specified
69     * nested <tt>Throwable</tt>.
70     *
71     * @param nested Nested <tt>Throwable</tt>.
72     */

73    public NestedSQLException(final Throwable JavaDoc nested) {
74       this(nested.getMessage(), nested);
75    }
76
77    /**
78     * Construct a <tt>NestedSQLException</tt>.
79     *
80     * @param msg Detail message.
81     * @param state SQL state message.
82     */

83    public NestedSQLException(final String JavaDoc msg, final String JavaDoc state) {
84       super(msg, state);
85       this.nested = null;
86    }
87
88    /**
89     * Construct a <tt>NestedSQLException</tt>.
90     *
91     * @param msg Detail message.
92     * @param state SQL state message.
93     * @param code SQL vendor code.
94     */

95    public NestedSQLException(final String JavaDoc msg, final String JavaDoc state, final int code) {
96       super(msg, state, code);
97       this.nested = null;
98    }
99    
100    /**
101     * Return the nested <tt>Throwable</tt>.
102     *
103     * @return Nested <tt>Throwable</tt>.
104     */

105    public Throwable JavaDoc getNested() {
106       return nested;
107    }
108
109    /**
110     * Return the nested <tt>Throwable</tt>.
111     *
112     * <p>For JDK 1.4 compatibility.
113     *
114     * @return Nested <tt>Throwable</tt>.
115     */

116    public Throwable JavaDoc getCause() {
117       return nested;
118    }
119       
120    /**
121     * Returns the composite throwable message.
122     *
123     * @return The composite throwable message.
124     */

125    public String JavaDoc getMessage() {
126       return NestedThrowable.Util.getMessage(super.getMessage(), nested);
127    }
128
129    /**
130     * Prints the composite message and the embedded stack trace to the
131     * specified print stream.
132     *
133     * @param stream Stream to print to.
134     */

135    public void printStackTrace(final PrintStream JavaDoc stream) {
136       if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED) {
137          super.printStackTrace(stream);
138       }
139       NestedThrowable.Util.print(nested, stream);
140    }
141
142    /**
143     * Prints the composite message and the embedded stack trace to the
144     * specified print writer.
145     *
146     * @param writer Writer to print to.
147     */

148    public void printStackTrace(final PrintWriter JavaDoc writer) {
149       if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED) {
150          super.printStackTrace(writer);
151       }
152       NestedThrowable.Util.print(nested, writer);
153    }
154
155    /**
156     * Prints the composite message and the embedded stack trace to
157     * <tt>System.err</tt>.
158     */

159    public void printStackTrace() {
160       printStackTrace(System.err);
161    }
162 }
163
Popular Tags