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