001    /*
002     * Copyright 2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.scaffold.lang;
018    
019    import org.apache.commons.scaffold.text.ConvertUtils;
020    
021    
022        /**
023         * Mimicks new functionality in 1.4
024         * http://www.javaworld.com/javaworld/jw-08-2001/jw-0803-exceptions.html
025         * @author Brian Geotz
026         * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
027         */
028        public class ChainedException extends Exception {
029    
030             private static final String CAUSED_BY = "Caused by: ";
031    
032             private Throwable cause;
033    
034             public ChainedException() {
035                 super();
036             }
037    
038             public ChainedException(String message) {
039                 super(message);
040             }
041    
042             public ChainedException(String message, Throwable cause) {
043                 super(message);
044                 this.cause = cause;
045             }
046    
047             public ChainedException(Throwable cause) {
048                 super();
049                 this.cause = cause;
050             }
051    
052             public Throwable getCause() {
053                 return this.cause;
054             }
055    
056             public boolean isCause() {
057                 return (this.cause!=null);
058             }
059    
060             public String getCauseMessage() {
061                 if (this.cause==null)
062                    return null;
063                 return this.cause.getMessage();
064             }
065    
066             public void getMessage(StringBuffer sb) {
067                 sb.append(super.getMessage());
068                 sb.append(ConvertUtils.LINE_FEED);
069                 if (cause != null) {
070                     sb.append(CAUSED_BY);
071                     if (cause instanceof ChainedException) {
072                         ChainedException chainedCause = (ChainedException) cause;
073                         chainedCause.getMessage(sb);
074                     }
075                     else {
076                         sb.append(cause.getMessage());
077                     }
078                 }
079             }
080    
081             public void printStackTrace() {
082                 super.printStackTrace();
083                 if (cause != null) {
084                     System.err.println(CAUSED_BY);
085                     cause.printStackTrace();
086                 }
087             }
088    
089             public void printStackTrace(java.io.PrintStream ps) {
090                 super.printStackTrace(ps);
091                 if (cause != null) {
092                     ps.println(CAUSED_BY);
093                     cause.printStackTrace(ps);
094                 }
095             }
096    
097             public void printStackTrace(java.io.PrintWriter pw) {
098                 super.printStackTrace(pw);
099                 if (cause != null) {
100                     pw.println(CAUSED_BY);
101                     cause.printStackTrace(pw);
102                 }
103             }
104        }
105