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.api.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 RuntimeMultiException extends RuntimeException
037{
038    /** The serialVersionUID. */
039    private static final long serialVersionUID = 8582253398936366771L;
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 RuntimeMultiException()
049    {
050        super();
051    }
052
053
054    /**
055     * Constructs an Exception with a detailed message.
056     *
057     * @param message The message associated with the exception.
058     */
059    public RuntimeMultiException( String message )
060    {
061        super( message );
062    }
063
064
065    /**
066     * Constructs an Exception with a detailed message.
067     *
068     * @param message The message associated with the exception.
069     */
070    public RuntimeMultiException( String message, Throwable t )
071    {
072        super( message );
073        nestedExceptions.add( t );
074    }
075
076
077    /**
078     * Lists the nested exceptions that this Exception encapsulates.
079     * 
080     * @return an Iterator over the nested exceptions.
081     */
082    public Iterator<Throwable> listNestedExceptions()
083    {
084        return nestedExceptions.iterator();
085    }
086
087
088    /**
089     * Gets the size (number of) exceptions nested within this exception.
090     * 
091     * @return the size of this nested exception.
092     */
093    public int size()
094    {
095        return nestedExceptions.size();
096    }
097
098
099    /**
100     * Tests to see if exceptions are nested within this exception.
101     * 
102     * @return true if an exception is nested, false otherwise
103     */
104    public boolean isEmpty()
105    {
106        return nestedExceptions.isEmpty();
107    }
108
109
110    /**
111     * Add an exeception to this multiexception.
112     * 
113     * @param nested
114     *            exception to add to this MultiException.
115     */
116    public void addThrowable( Throwable nested )
117    {
118        nestedExceptions.add( nested );
119    }
120
121
122    // ///////////////////////////////////////////
123    // Overriden Throwable Stack Trace Methods //
124    // ///////////////////////////////////////////
125
126    /**
127     * Beside printing out the standard stack trace this method prints out the
128     * stack traces of all the nested exceptions.
129     * 
130     * @param out
131     *            PrintWriter to write the nested stack trace to.
132     */
133    public void printStackTrace( PrintWriter out )
134    {
135        super.printStackTrace( out );
136
137        out.println( "Nested exceptions to follow:\n" );
138        boolean isFirst = true;
139
140        for ( Throwable throwable : nestedExceptions )
141        {
142            if ( isFirst )
143            {
144                isFirst = false;
145            }
146            else
147            {
148                out.println( "\n\t<<========= Next Nested Exception" + " ========>>\n" );
149            }
150
151            throwable.printStackTrace();
152        }
153
154        out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
155    }
156
157
158    /**
159     * Beside printing out the standard stack trace this method prints out the
160     * stack traces of all the nested exceptions.
161     * 
162     * @param out
163     *            PrintStream to write the nested stack trace to.
164     */
165    public void printStackTrace( PrintStream out )
166    {
167        super.printStackTrace( out );
168
169        out.println( "Nested exceptions to follow:\n" );
170        boolean isFirst = true;
171
172        for ( Throwable throwable : nestedExceptions )
173        {
174            if ( isFirst )
175            {
176                isFirst = false;
177            }
178            else
179            {
180                out.println( "\n\t<<========= Next Nested Exception" + " ========>>\n" );
181            }
182
183            throwable.printStackTrace();
184        }
185
186        out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
187    }
188
189
190    /**
191     * Beside printing out the standard stack trace this method prints out the
192     * stack traces of all the nested exceptions using standard error.
193     */
194    public void printStackTrace()
195    {
196        this.printStackTrace( System.err );
197    }
198}