View Javadoc

1   /*
2    * Copyright 2000-2005 The Apache Software Foundation.
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  package org.apache.portals.graffito.jcr.exception;
17  
18  
19  import java.io.PrintStream;
20  import java.io.PrintWriter;
21  
22  /***
23   * Nestable runtime exception. Able to wrap a cause exception in JDK previous to 1.4
24   *
25   * @author Spring framework
26   */
27  public class NestableRuntimeException extends RuntimeException {
28  
29      /*** Use serialVersionUID for interoperability. */
30      private final static long serialVersionUID = -1939051127461985443L;
31  
32      /*** Root cause of this nested exception */
33      private Throwable cause;
34  
35      /***
36       * No-arg constructor used by markup exceptions.
37       */
38      protected NestableRuntimeException() {
39      }
40  
41      /***
42       * Construct a <code>NestableRuntimeException</code> with the specified detail message.
43       * @param msg the detail message
44       */
45      public NestableRuntimeException(String msg) {
46          super(msg);
47      }
48  
49      /***
50       * Construct a <code>NestableRuntimeException</code> with the specified detail message
51       * and nested exception.
52       * @param msg the detail message
53       * @param ex the nested exception
54       */
55      public NestableRuntimeException(String msg, Throwable ex) {
56          super(msg);
57          this.cause = ex;
58      }
59  
60      /***
61       * Construct a <code>NestableRuntimeException</code> with the specified
62       * nested exception.
63       *
64       * @param ex the nested exception
65       */
66      public NestableRuntimeException(Throwable ex) {
67          this.cause = ex;
68      }
69  
70      /***
71       * Return the nested cause, or <code>null</code> if none.
72       */
73      public Throwable getCause() {
74          // Even if you cannot set the cause of this exception other than through
75          // the constructor, we check for the cause being "this" here, as the cause
76          // could still be set to "this" via reflection: for example, by a remoting
77          // deserializer like Hessian's.
78          return ((this.cause == this) ? null : this.cause);
79      }
80  
81      /***
82       * Return the detail message, including the message from the nested exception
83       * if there is one.
84       */
85      public String getMessage() {
86          if (getCause() == null) {
87              return super.getMessage();
88          } else {
89              return super.getMessage() 
90                  + "; nested exception is " 
91                  + getCause().getClass().getName()
92                  + ": " + getCause().getMessage();
93          }
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 ps) {
101         if (getCause() == null) {
102             super.printStackTrace(ps);
103         } else {
104             ps.println(this);
105             getCause().printStackTrace(ps);
106         }
107     }
108 
109     /***
110      * Print the composite message and the embedded stack trace to the specified writer.
111      * @param pw the print writer
112      */
113     public void printStackTrace(PrintWriter pw) {
114         if (getCause() == null) {
115             super.printStackTrace(pw);
116         } else {
117             pw.println(this);
118             getCause().printStackTrace(pw);
119         }
120     }
121 }