View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.util.exception;
21  
22  
23  import java.io.PrintStream;
24  import java.io.PrintWriter;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Iterator;
29  
30  
31  /**
32   * This exception is thrown when Base class for nested exceptions.
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class RuntimeMultiException extends RuntimeException
37  {
38      /** The serialVersionUID. */
39      private static final long serialVersionUID = 8582253398936366771L;
40  
41      /** Collection of nested exceptions. */
42      private final Collection<Throwable> nestedExceptions = new ArrayList<>();
43  
44  
45      /**
46       * Constructs an Exception without a message.
47       */
48      public RuntimeMultiException()
49      {
50          super();
51      }
52  
53  
54      /**
55       * Constructs an Exception with a detailed message.
56       *
57       * @param message The message associated with the exception.
58       */
59      public RuntimeMultiException( String message )
60      {
61          super( message );
62      }
63  
64  
65      /**
66       * Constructs an Exception with a detailed message.
67       *
68       * @param message The message associated with the exception.
69       * @param t The Throwable that causes the Exception 
70       */
71      public RuntimeMultiException( String message, Throwable t )
72      {
73          super( message );
74          nestedExceptions.add( t );
75      }
76  
77  
78      /**
79       * Lists the nested exceptions that this Exception encapsulates.
80       * 
81       * @return an Iterator over the nested exceptions.
82       */
83      public Iterator<Throwable> listNestedExceptions()
84      {
85          return nestedExceptions.iterator();
86      }
87  
88  
89      /**
90       * Gets the size (number of) exceptions nested within this exception.
91       * 
92       * @return the size of this nested exception.
93       */
94      public int size()
95      {
96          return nestedExceptions.size();
97      }
98  
99  
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 }