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