1 package org.apache.maven.surefire.util;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 /*
23 * Copyright 2002-2005 the original author or authors.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
36 */
37
38 import java.io.PrintStream;
39 import java.io.PrintWriter;
40
41 /**
42 * <p>Copied from Spring framework to keep Java 1.3 compatibility.</p>
43 * <p/>
44 * <p>Handy class for wrapping checked Exceptions with a root cause.</p>
45 * <p/>
46 * <p>This time-honored technique is no longer necessary in Java 1.4, which
47 * finally provides built-in support for exception nesting. Thus exceptions in
48 * applications written to use Java 1.4 need not extend this class. To ease
49 * migration, this class mirrors Java 1.4's nested exceptions as closely as possible.
50 * <p/>
51 * <p>Abstract to force the programmer to extend the class. <code>getMessage</code>
52 * will include nested exception information; <code>printStackTrace</code> etc will
53 * delegate to the wrapped exception, if any.
54 * <p/>
55 * <p>The similarity between this class and the NestedRuntimeException class is
56 * unavoidable, as Java forces these two classes to have different superclasses
57 * (ah, the inflexibility of concrete inheritance!).
58 * <p/>
59 * <p>As discussed in
60 * <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0764543857/">Expert One-On-One J2EE Design and
61 * Development</a>, runtime exceptions are often a better alternative to checked exceptions.
62 * However, all exceptions should preserve their stack trace, if caused by a
63 * lower-level exception.
64 *
65 * @author Rod Johnson
66 * @author Juergen Hoeller
67 * @see #getMessage
68 * @see #printStackTrace
69 * @see NestedRuntimeException
70 */
71 public class NestedCheckedException
72 extends Exception
73 {
74
75 /**
76 * Root cause of this nested exception
77 */
78 private Throwable cause;
79
80 /**
81 * Construct a <code>NestedCheckedException</code> with the specified detail message.
82 *
83 * @param msg the detail message
84 */
85 public NestedCheckedException( String msg )
86 {
87 super( msg );
88 }
89
90 /**
91 * Construct a <code>NestedCheckedException</code> with the specified detail message
92 * and nested exception.
93 *
94 * @param msg the detail message
95 * @param ex the nested exception
96 */
97 public NestedCheckedException( String msg, Throwable ex )
98 {
99 super( msg );
100 this.cause = ex;
101 }
102
103 /**
104 * Return the nested cause, or <code>null</code> if none.
105 */
106 public Throwable getCause()
107 {
108 // Even if you cannot set the cause of this exception other than through
109 // the constructor, we check for the cause being "this" here, as the cause
110 // could still be set to "this" via reflection: for example, by a remoting
111 // deserializer like Hessian's.
112 return ( this.cause == this ? null : this.cause );
113 }
114
115 /**
116 * Return the detail message, including the message from the nested exception
117 * if there is one.
118 */
119 public String getMessage()
120 {
121 if ( getCause() == null )
122 {
123 return super.getMessage();
124 }
125 else
126 {
127 return super.getMessage() + "; nested exception is " + getCause().getClass().getName() + ": "
128 + getCause().getMessage();
129 }
130 }
131
132 /**
133 * Print the composite message and the embedded stack trace to the specified stream.
134 *
135 * @param ps the print stream
136 */
137 public void printStackTrace( PrintStream ps )
138 {
139 if ( getCause() == null )
140 {
141 super.printStackTrace( ps );
142 }
143 else
144 {
145 ps.println( this );
146 getCause().printStackTrace( ps );
147 }
148 }
149
150 /**
151 * Print the composite message and the embedded stack trace to the specified print writer.
152 *
153 * @param pw the print writer
154 */
155 public void printStackTrace( PrintWriter pw )
156 {
157 if ( getCause() == null )
158 {
159 super.printStackTrace( pw );
160 }
161 else
162 {
163 pw.println( this );
164 getCause().printStackTrace( pw );
165 }
166 }
167 }