KickJava   Java API By Example, From Geeks To Geeks.

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


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 runtime <code>Exceptions</code> with a root cause.
24  *
25  * <p>This time-honoured 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 NestedCheckedException}
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 NestedCheckedException
44  */

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

59     public NestedRuntimeException(String JavaDoc msg) {
60         super(msg);
61     }
62
63     /**
64      * Construct a <code>NestedRuntimeException</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 NestedRuntimeException(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 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 <code>NestedRuntimeException</code> causes.
130      * Will use 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      * @since 2.0
133      */

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

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

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