001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.shared.util.exception;
021
022
023import java.io.PrintStream;
024import java.io.PrintWriter;
025
026import java.util.ArrayList;
027import java.util.Collection;
028import java.util.Iterator;
029
030
031/**
032 * This exception is thrown when Base class for nested exceptions.
033 * 
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class MultiException extends Exception
037{
038    /** The serialVersionUID. */
039    static final long serialVersionUID = 2889747406899775761L;
040
041    /** Collection of nested exceptions. */
042    private Collection<Throwable> nestedExceptions = new ArrayList<Throwable>();
043
044
045    /**
046     * Constructs an Exception without a message.
047     */
048    public MultiException()
049    {
050        super();
051    }
052
053
054    /**
055     * Constructs an Exception with a detailed message.
056     * 
057     * @param message
058     *            The message associated with the exception.
059     */
060    public MultiException( String message )
061    {
062        super( message );
063    }
064
065
066    /**
067     * Lists the nested exceptions that this Exception encapsulates.
068     * 
069     * @return an Iterator over the nested exceptions.
070     */
071    public Iterator<Throwable> listNestedExceptions()
072    {
073        return nestedExceptions.iterator();
074    }
075
076
077    /**
078     * Gets the size of this nested exception which equals the number of
079     * exception nested within.
080     * 
081     * @return the size of this nested exception.
082     */
083    public int size()
084    {
085        return nestedExceptions.size();
086    }
087
088
089    /**
090     * Tests to see if there are any nested exceptions within this
091     * MultiException.
092     * 
093     * @return true if no exceptions are nested, false otherwise.
094     */
095    public boolean isEmpty()
096    {
097        return nestedExceptions.isEmpty();
098    }
099
100
101    /**
102     * Add an exeception to this multiexception.
103     * 
104     * @param nested
105     *            exception to add to this MultiException.
106     */
107    public void addThrowable( Throwable nested )
108    {
109        nestedExceptions.add( nested );
110    }
111
112
113    // ///////////////////////////////////////////
114    // Overriden Throwable Stack Trace Methods //
115    // ///////////////////////////////////////////
116
117    /**
118     * Beside printing out the standard stack trace this method prints out the
119     * stack traces of all the nested exceptions.
120     * 
121     * @param out
122     *            PrintWriter to write the nested stack trace to.
123     */
124    public void printStackTrace( PrintWriter out )
125    {
126        super.printStackTrace( out );
127
128        out.println( "Nested exceptions to follow:\n" );
129        boolean isFirst = true;
130
131        for ( Throwable throwable:nestedExceptions )
132        {
133            if ( isFirst )
134            {
135                isFirst = false;
136            }
137            else
138            {
139                out.println( "\n\t<<========= Next Nested Exception" + " ========>>\n" );
140            }
141
142            throwable.printStackTrace();
143        }
144        
145        out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
146    }
147
148
149    /**
150     * Beside printing out the standard stack trace this method prints out the
151     * stack traces of all the nested exceptions.
152     * 
153     * @param out
154     *            PrintStream to write the nested stack trace to.
155     */
156    public void printStackTrace( PrintStream out )
157    {
158        super.printStackTrace( out );
159
160        out.println( "Nested exceptions to follow:\n" );
161        boolean isFirst = true;
162
163        for ( Throwable throwable:nestedExceptions )
164        {
165            if ( isFirst )
166            {
167                isFirst = false;
168            }
169            else
170            {
171                out.println( "\n\t<<========= Next Nested Exception" + " ========>>\n" );
172            }
173
174            throwable.printStackTrace();
175        }
176        
177        out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
178    }
179
180
181    /**
182     * Beside printing out the standard stack trace this method prints out the
183     * stack traces of all the nested exceptions using standard error.
184     */
185    public void printStackTrace()
186    {
187        this.printStackTrace( System.err );
188    }
189}