View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 1999-2001 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  
46  package org.exolab.castor.util;
47  
48  import java.io.PrintStream;
49  import java.io.PrintWriter;
50  
51  /**
52   * An exception that is used to signal I/O errors which are
53   * caused by other exceptions. This class allows the user
54   * get to the original exception.
55   *
56   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
57   * @version $Revision$ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
58  **/
59  public class NestedIOException extends java.io.IOException {
60      /** SerialVersionUID */
61      private static final long serialVersionUID = -4698274786487914369L;
62  
63      /**
64       * A nested exception
65       */
66      private Exception _exception  = null;
67      
68      /**
69       * A flag to indicate a local stack trace only
70      **/
71      private boolean _localTrace = false;
72      
73      /**
74       * Creates a new NestedIOException with no message,
75       * or nested Exception
76      **/
77      public NestedIOException() {
78          super();
79      } //-- NestedIOException
80      
81      /**
82       * Creates a new NestedIOException with the given message.
83       * @param message the message for this Exception
84      **/
85      public NestedIOException(String message) {
86          super(message);
87      } //-- NestedIOException(String)
88  
89      
90      /**
91       * Creates a new NestedIOException with the given nested
92       * exception.
93       *
94       * @param exception the nested exception. (Must not be null).
95      **/
96      public NestedIOException(Exception exception) {
97          super(exception.getMessage());
98          _exception = exception;
99      } //-- NestedIOException(Exception)
100 
101     /**
102      * Creates a new NestedIOException with the given message
103      * and nested exception.
104      *
105      * @param message the detail message for this exception
106      * @param exception the nested exception
107     **/
108     public NestedIOException(String message, Exception exception) 
109     {
110         super(message);
111         _exception = exception;
112     } //-- NestedIOException(String, Exception)
113 
114     /**
115      * Returns the exception, which in turn caused this Exception to
116      * be thrown, or null if nested exception exists.
117      * 
118      * @return the exception, which in turn caused this Exception to
119      * be thrown, or null if nested exception exists.
120     **/
121     public Exception getException() {
122         return _exception;
123     } //-- getException
124     
125     /**
126      * Sets whether or not to print the local stack trace or
127      * the nested stack trace when calls to #printStackTrace are made. By
128      * default the nested exception is used for printing stack trace.
129      *
130      * @param localTrace a boolean when true enables local stack trace only.
131     **/
132     public void setLocalStackTraceOnly(boolean localTrace) {
133         _localTrace = localTrace;
134     } //-- setLocalStackTraceOnly
135     
136     /**
137      * Returns the String representation of this Exception.
138      *
139      * @return the String representation of this Exception.
140     **/
141     public String toString() {
142         StringBuffer sb = new StringBuffer("NestedIOException: ");
143         if (getMessage() != null) {
144             sb.append(getMessage());
145         }
146         if ((_exception != null) && (_exception.getMessage() != null)) {
147             sb.append(" { nested error: ");
148             sb.append(_exception.getMessage());
149             sb.append('}');
150         }
151         return sb.toString();
152     } //-- toString
153 
154     //----------------------------------------------------------------/
155     //- Overwrite printStackTrace methods to handle nested exception -/
156     //----------------------------------------------------------------/
157     
158     public void printStackTrace()
159     {
160         if (( _exception == null ) || _localTrace)
161             super.printStackTrace();
162         else
163             _exception.printStackTrace();
164     }
165 
166     public void printStackTrace( PrintWriter printer )
167     {
168         if (_localTrace || ( _exception == null ))
169             super.printStackTrace( printer );
170         else
171             _exception.printStackTrace( printer);
172     }
173 
174     public void printStackTrace( PrintStream printer )
175     {
176         if (_localTrace || ( _exception == null ))
177             super.printStackTrace( printer );
178         else
179             _exception.printStackTrace( printer );
180     }
181     
182 } //-- NestedIOException