KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > NestedCheckedException


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core;
18
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 /**
23  * Handy class for wrapping checked <code>Exceptions</code> with a root cause.
24  *
25  * <p>This time-honored technique is no longer necessary in Java 1.4, which
26  * finally provides built-in support for exception nesting. Thus exceptions in
27  * applications written to use Java 1.4 need not extend this class. To ease
28  * migration, this class mirrors Java 1.4's nested exceptions as closely as possible.
29  *
30  * <p>This class is <code>abstract</code> to force the programmer to extend
31  * the class. <code>getMessage</code> will include nested exception
32  * information; <code>printStackTrace</code> and other like methods will
33  * delegate to the wrapped exception, if any.
34  *
35  * <p>The similarity between this class and the {@link NestedRuntimeException}
36  * class is unavoidable, as Java forces these two classes to have different
37  * superclasses (ah, the inflexibility of concrete inheritance!).
38  *
39  * @author Rod Johnson
40  * @author Juergen Hoeller
41  * @see #getMessage
42  * @see #printStackTrace
43  * @see NestedRuntimeException
44  */

45 public abstract class NestedCheckedException extends Exception JavaDoc {
46
47     /** Use serialVersionUID from Spring 1.2 for interoperability */
48     private static final long serialVersionUID = 7100714597678207546L;
49
50
51     /** Root cause of this nested exception */
52     private Throwable JavaDoc cause;
53
54
55     /**
56      * Construct a <code>NestedCheckedException</code> with the specified detail message.
57      * @param msg the detail message
58      */

59     public NestedCheckedException(String JavaDoc msg) {
60         super(msg);
61     }
62
63     /**
64      * Construct a <code>NestedCheckedException</code> with the specified detail message
65      * and nested exception.
66      * @param msg the detail message
67      * @param cause the nested exception
68      */

69     public NestedCheckedException(String JavaDoc msg, Throwable JavaDoc cause) {
70         super(msg);
71         this.cause = cause;
72     }
73
74
75     /**
76      * Return the nested cause, or <code>null</code> if none.
77      * <p>Note that this will only check one level of nesting.
78      * Use {@link #getRootCause()} to retrieve the innermost cause.
79      */

80     public Throwable JavaDoc getCause() {
81         // Even if you cannot set the cause of this exception other than through
82
// the constructor, we check for the cause being "this" here, as the cause
83
// could still be set to "this" via reflection: for example, by a remoting
84
// deserializer like Hessian's.
85
return (this.cause == this ? null : this.cause);
86     }
87
88     /**
89      * Return the detail message, including the message from the nested exception
90      * if there is one.
91      */

92     public String JavaDoc getMessage() {
93         return NestedExceptionUtils.buildMessage(super.getMessage(), getCause());
94     }
95
96     /**
97      * Print the composite message and the embedded stack trace to the specified stream.
98      * @param ps the print stream
99      */

100     public void printStackTrace(PrintStream JavaDoc ps) {
101         if (getCause() == null) {
102             super.printStackTrace(ps);
103         }
104         else {
105             ps.println(this);
106             ps.print("Caused by: ");
107             getCause().printStackTrace(ps);
108         }
109     }
110
111     /**
112      * Print the composite message and the embedded stack trace to the specified print writer.
113      * @param pw the print writer
114      */

115     public void printStackTrace(PrintWriter JavaDoc pw) {
116         if (getCause() == null) {
117             super.printStackTrace(pw);
118         }
119         else {
120             pw.println(this);
121             pw.print("Caused by: ");
122             getCause().printStackTrace(pw);
123         }
124     }
125
126
127     /**
128      * Retrieve the innermost cause of this exception, if any.
129      * <p>Currently just traverses NestedCheckedException causes. Will use
130      * the JDK 1.4 exception cause mechanism once Spring requires JDK 1.4.
131      * @return the innermost exception, or <code>null</code> if none
132      */

133     public Throwable JavaDoc getRootCause() {
134         Throwable JavaDoc cause = getCause();
135         if (cause instanceof NestedCheckedException) {
136             return ((NestedCheckedException) cause).getRootCause();
137         }
138         else {
139             return cause;
140         }
141     }
142
143     /**
144      * Retrieve the most specific cause of this exception, that is,
145      * either the innermost cause (root cause) or this exception itself.
146      * <p>Differs from {@link #getRootCause()} in that it falls back
147      * to the present exception if there is no root cause.
148      * @return the most specific cause (never <code>null</code>)
149      * @since 2.0.3
150      */

151     public Throwable JavaDoc getMostSpecificCause() {
152         Throwable JavaDoc rootCause = getRootCause();
153         return (rootCause != null ? rootCause : this);
154     }
155
156     /**
157      * Check whether this exception contains an exception of the given type:
158      * either it is of the given class itself or it contains a nested cause
159      * of the given type.
160      * <p>Currently just traverses <code>NestedCheckedException</code> causes.
161      * Will use the JDK 1.4 exception cause mechanism once Spring requires JDK 1.4.
162      * @param exType the exception type to look for
163      * @return whether there is a nested exception of the specified type
164      */

165     public boolean contains(Class JavaDoc exType) {
166         if (exType == null) {
167             return false;
168         }
169         if (exType.isInstance(this)) {
170             return true;
171         }
172         Throwable JavaDoc cause = getCause();
173         if (cause instanceof NestedCheckedException) {
174             return ((NestedCheckedException) cause).contains(exType);
175         }
176         else {
177             return (cause != null && exType.isInstance(cause));
178         }
179     }
180
181 }
182
Popular Tags