001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2001 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: NestedIOException.java 5951 2006-05-30 22:18:48Z bsnyder $
044: */package org.exolab.castor.util;
045:
046: import java.io.PrintStream;
047: import java.io.PrintWriter;
048:
049: /**
050: * An exception that is used to signal I/O errors which are
051: * caused by other exceptions. This class allows the user
052: * get to the original exception.
053: *
054: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
055: * @version $Revision: 5951 $ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
056: **/
057: public class NestedIOException extends java.io.IOException {
058: /** SerialVersionUID */
059: private static final long serialVersionUID = -4698274786487914369L;
060:
061: /**
062: * A nested exception
063: */
064: private Exception _exception = null;
065:
066: /**
067: * A flag to indicate a local stack trace only
068: **/
069: private boolean _localTrace = false;
070:
071: /**
072: * Creates a new NestedIOException with no message,
073: * or nested Exception
074: **/
075: public NestedIOException() {
076: super ();
077: } //-- NestedIOException
078:
079: /**
080: * Creates a new NestedIOException with the given message.
081: * @param message the message for this Exception
082: **/
083: public NestedIOException(String message) {
084: super (message);
085: } //-- NestedIOException(String)
086:
087: /**
088: * Creates a new NestedIOException with the given nested
089: * exception.
090: *
091: * @param exception the nested exception. (Must not be null).
092: **/
093: public NestedIOException(Exception exception) {
094: super (exception.getMessage());
095: _exception = exception;
096: } //-- NestedIOException(Exception)
097:
098: /**
099: * Creates a new NestedIOException with the given message
100: * and nested exception.
101: *
102: * @param message the detail message for this exception
103: * @param exception the nested exception
104: **/
105: public NestedIOException(String message, Exception exception) {
106: super (message);
107: _exception = exception;
108: } //-- NestedIOException(String, Exception)
109:
110: /**
111: * Returns the exception, which in turn caused this Exception to
112: * be thrown, or null if nested exception exists.
113: *
114: * @return the exception, which in turn caused this Exception to
115: * be thrown, or null if nested exception exists.
116: **/
117: public Exception getException() {
118: return _exception;
119: } //-- getException
120:
121: /**
122: * Sets whether or not to print the local stack trace or
123: * the nested stack trace when calls to #printStackTrace are made. By
124: * default the nested exception is used for printing stack trace.
125: *
126: * @param localTrace a boolean when true enables local stack trace only.
127: **/
128: public void setLocalStackTraceOnly(boolean localTrace) {
129: _localTrace = localTrace;
130: } //-- setLocalStackTraceOnly
131:
132: /**
133: * Returns the String representation of this Exception.
134: *
135: * @return the String representation of this Exception.
136: **/
137: public String toString() {
138: StringBuffer sb = new StringBuffer("NestedIOException: ");
139: if (getMessage() != null) {
140: sb.append(getMessage());
141: }
142: if ((_exception != null) && (_exception.getMessage() != null)) {
143: sb.append(" { nested error: ");
144: sb.append(_exception.getMessage());
145: sb.append('}');
146: }
147: return sb.toString();
148: } //-- toString
149:
150: //----------------------------------------------------------------/
151: //- Overwrite printStackTrace methods to handle nested exception -/
152: //----------------------------------------------------------------/
153:
154: public void printStackTrace() {
155: if ((_exception == null) || _localTrace)
156: super .printStackTrace();
157: else
158: _exception.printStackTrace();
159: }
160:
161: public void printStackTrace(PrintWriter printer) {
162: if (_localTrace || (_exception == null))
163: super .printStackTrace(printer);
164: else
165: _exception.printStackTrace(printer);
166: }
167:
168: public void printStackTrace(PrintStream printer) {
169: if (_localTrace || (_exception == null))
170: super .printStackTrace(printer);
171: else
172: _exception.printStackTrace(printer);
173: }
174:
175: } //-- NestedIOException
|