KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > util > NestedServletException


1 /*
2  * Copyright 2002-2006 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.web.util;
18
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23
24 import org.springframework.core.NestedExceptionUtils;
25
26 /**
27  * Subclass of ServletException that properly handles a root cause in terms
28  * of message and stacktrace, just like NestedChecked/RuntimeException does.
29  * Note that the plain ServletException doesn't expose its root cause at all,
30  * neither in the exception message nor in printed stack traces!
31  *
32  * <p>The similarity between this class and the NestedChecked/RuntimeException
33  * class is unavoidable, as this class needs to derive from ServletException
34  * and cannot derive from NestedCheckedException.
35  *
36  * @author Juergen Hoeller
37  * @since 1.2.5
38  * @see #getMessage
39  * @see #printStackTrace
40  * @see org.springframework.core.NestedCheckedException
41  * @see org.springframework.core.NestedRuntimeException
42  */

43 public class NestedServletException extends ServletException JavaDoc {
44
45     /** Use serialVersionUID from Spring 1.2 for interoperability */
46     private static final long serialVersionUID = -5292377985529381145L;
47
48
49     /**
50      * Construct a <code>NestedServletException</code> with the specified detail message.
51      * @param msg the detail message
52      */

53     public NestedServletException(String JavaDoc msg) {
54         super(msg);
55     }
56
57     /**
58      * Construct a <code>NestedServletException</code> with the specified detail message
59      * and nested exception.
60      * @param msg the detail message
61      * @param cause the nested exception
62      */

63     public NestedServletException(String JavaDoc msg, Throwable JavaDoc cause) {
64         super(msg, cause);
65     }
66
67
68     /**
69      * Return the detail message, including the message from the nested exception
70      * if there is one.
71      */

72     public String JavaDoc getMessage() {
73         return NestedExceptionUtils.buildMessage(super.getMessage(), getRootCause());
74     }
75
76     /**
77      * Print the composite message and the embedded stack trace to the specified stream.
78      * @param ps the print stream
79      */

80     public void printStackTrace(PrintStream JavaDoc ps) {
81         Throwable JavaDoc cause = getRootCause();
82         if (cause == null) {
83             super.printStackTrace(ps);
84         }
85         else {
86             ps.println(this);
87             ps.print("Caused by: ");
88             cause.printStackTrace(ps);
89         }
90     }
91
92     /**
93      * Print the composite message and the embedded stack trace to the specified print writer.
94      * @param pw the print writer
95      */

96     public void printStackTrace(PrintWriter JavaDoc pw) {
97         Throwable JavaDoc cause = getRootCause();
98         if (cause == null) {
99             super.printStackTrace(pw);
100         }
101         else {
102             pw.println(this);
103             pw.print("Caused by: ");
104             cause.printStackTrace(pw);
105         }
106     }
107
108 }
109
Popular Tags