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 final Collection<Throwable> nestedExceptions = new ArrayList<>();
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     * @param t The Throwable that causes the Exception 
070     */
071    public RuntimeMultiException( String message, Throwable t )
072    {
073        super( message );
074        nestedExceptions.add( t );
075    }
076
077
078    /**
079     * Lists the nested exceptions that this Exception encapsulates.
080     * 
081     * @return an Iterator over the nested exceptions.
082     */
083    public Iterator<Throwable> listNestedExceptions()
084    {
085        return nestedExceptions.iterator();
086    }
087
088
089    /**
090     * Gets the size (number of) exceptions nested within this exception.
091     * 
092     * @return the size of this nested exception.
093     */
094    public int size()
095    {
096        return nestedExceptions.size();
097    }
098
099
100    /**
101     * Tests to see if exceptions are nested within this exception.
102     * 
103     * @return true if an exception is nested, false otherwise
104     */
105    public boolean isEmpty()
106    {
107        return nestedExceptions.isEmpty();
108    }
109
110
111    /**
112     * Add an exeception to this multiexception.
113     * 
114     * @param nested 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 PrintWriter to write the nested stack trace to.
131     */
132    @Override
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( out );
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 PrintStream to write the nested stack trace to.
163     */
164    @Override
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( out );
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    @Override
195    public void printStackTrace()
196    {
197        this.printStackTrace( System.err );
198    }
199}