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     */
020    
021    package org.apache.directory.shared.ldap;
022    
023    
024    import java.io.PrintStream;
025    import java.io.PrintWriter;
026    
027    import java.util.ArrayList;
028    import java.util.Collection;
029    import java.util.Iterator;
030    
031    
032    /**
033     * This exception is thrown when Base class for nested exceptions.
034     * 
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Revision: 664290 $
037     */
038    public class RuntimeMultiException extends RuntimeException
039    {
040        private static final long serialVersionUID = 8582253398936366771L;
041    
042        /** Collection of nested exceptions. */
043        private Collection<Throwable> nestedExceptions = new ArrayList<Throwable>();
044    
045    
046        /**
047         * Constructs an Exception without a message.
048         */
049        public RuntimeMultiException()
050        {
051            super();
052        }
053    
054    
055        /**
056         * Constructs an Exception with a detailed message.
057         * 
058         * @param message
059         *            The message associated with the exception.
060         */
061        public RuntimeMultiException(String message)
062        {
063            super( message );
064        }
065    
066    
067        /**
068         * Lists the nested exceptions that this Exception encapsulates.
069         * 
070         * @return an Iterator over the nested exceptions.
071         */
072        public Iterator<Throwable> listNestedExceptions()
073        {
074            return nestedExceptions.iterator();
075        }
076    
077    
078        /**
079         * Gets the size (number of) exceptions nested within this exception.
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 exceptions are nested within this exception.
091         * 
092         * @return true if an exception is nested, false otherwise
093         */
094        public boolean isEmpty()
095        {
096            return nestedExceptions.isEmpty();
097        }
098    
099    
100        /**
101         * Add an exeception to this multiexception.
102         * 
103         * @param nested
104         *            exception to add to this MultiException.
105         */
106        public void addThrowable( Throwable nested )
107        {
108            nestedExceptions.add( nested );
109        }
110    
111    
112        // ///////////////////////////////////////////
113        // Overriden Throwable Stack Trace Methods //
114        // ///////////////////////////////////////////
115    
116        /**
117         * Beside printing out the standard stack trace this method prints out the
118         * stack traces of all the nested exceptions.
119         * 
120         * @param out
121         *            PrintWriter to write the nested stack trace to.
122         */
123        public void printStackTrace( PrintWriter out )
124        {
125            super.printStackTrace( out );
126    
127            out.println( "Nested exceptions to follow:\n" );
128            boolean isFirst = true;
129            
130            for ( Throwable throwable:nestedExceptions )
131            {
132                if ( isFirst )
133                {
134                    isFirst = false;
135                }
136                else
137                {
138                    out.println( "\n\t<<========= Next Nested Exception" + " ========>>\n" );
139                }
140    
141                throwable.printStackTrace();
142            }
143            
144            out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
145        }
146    
147    
148        /**
149         * Beside printing out the standard stack trace this method prints out the
150         * stack traces of all the nested exceptions.
151         * 
152         * @param out
153         *            PrintStream to write the nested stack trace to.
154         */
155        public void printStackTrace( PrintStream out )
156        {
157            super.printStackTrace( out );
158    
159            out.println( "Nested exceptions to follow:\n" );
160            boolean isFirst = true;
161    
162            for ( Throwable throwable:nestedExceptions )
163            {
164                if ( isFirst )
165                {
166                    isFirst = false;
167                }
168                else
169                {
170                    out.println( "\n\t<<========= Next Nested Exception" + " ========>>\n" );
171                }
172    
173                throwable.printStackTrace();
174            }
175            
176            out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
177        }
178    
179    
180        /**
181         * Beside printing out the standard stack trace this method prints out the
182         * stack traces of all the nested exceptions using standard error.
183         */
184        public void printStackTrace()
185        {
186            this.printStackTrace( System.err );
187        }
188    }