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   *    http://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 MultiException extends Exception
37  {
38      /** The serialVersionUID. */
39      static final long serialVersionUID = 2889747406899775761L;
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 MultiException()
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 MultiException( String message )
60      {
61          super( message );
62      }
63  
64  
65      /**
66       * Lists the nested exceptions that this Exception encapsulates.
67       * 
68       * @return an Iterator over the nested exceptions.
69       */
70      public Iterator<Throwable> listNestedExceptions()
71      {
72          return nestedExceptions.iterator();
73      }
74  
75  
76      /**
77       * Gets the size of this nested exception which equals the number of
78       * exception nested within.
79       * 
80       * @return the size of this nested exception.
81       */
82      public int size()
83      {
84          return nestedExceptions.size();
85      }
86  
87  
88      /**
89       * Tests to see if there are any nested exceptions within this
90       * MultiException.
91       * 
92       * @return true if no exceptions are nested, false otherwise.
93       */
94      public boolean isEmpty()
95      {
96          return nestedExceptions.isEmpty();
97      }
98  
99  
100     /**
101      * Add an exception to this multiexception.
102      * 
103      * @param nested exception to add to this MultiException.
104      */
105     public void addThrowable( Throwable nested )
106     {
107         nestedExceptions.add( nested );
108     }
109 
110 
111     // ///////////////////////////////////////////
112     // Overriden Throwable Stack Trace Methods //
113     // ///////////////////////////////////////////
114 
115     /**
116      * Beside printing out the standard stack trace this method prints out the
117      * stack traces of all the nested exceptions.
118      * 
119      * @param out PrintWriter to write the nested stack trace to.
120      */
121     @Override
122     public void printStackTrace( PrintWriter out )
123     {
124         super.printStackTrace( out );
125 
126         out.println( "Nested exceptions to follow:\n" );
127         boolean isFirst = true;
128 
129         for ( Throwable throwable : nestedExceptions )
130         {
131             if ( isFirst )
132             {
133                 isFirst = false;
134             }
135             else
136             {
137                 out.println( "\n\t<<========= Next Nested Exception" + " ========>>\n" );
138             }
139 
140             throwable.printStackTrace( out );
141         }
142 
143         out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
144     }
145 
146 
147     /**
148      * Beside printing out the standard stack trace this method prints out the
149      * stack traces of all the nested exceptions.
150      * 
151      * @param out PrintStream to write the nested stack trace to.
152      */
153     @Override
154     public void printStackTrace( PrintStream out )
155     {
156         super.printStackTrace( out );
157 
158         out.println( "Nested exceptions to follow:\n" );
159         boolean isFirst = true;
160 
161         for ( Throwable throwable : nestedExceptions )
162         {
163             if ( isFirst )
164             {
165                 isFirst = false;
166             }
167             else
168             {
169                 out.println( "\n\t<<========= Next Nested Exception" + " ========>>\n" );
170             }
171 
172             throwable.printStackTrace( out );
173         }
174 
175         out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
176     }
177 
178 
179     /**
180      * Beside printing out the standard stack trace this method prints out the
181      * stack traces of all the nested exceptions using standard error.
182      */
183     @Override
184     public void printStackTrace()
185     {
186         this.printStackTrace( System.err );
187     }
188 }