001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.xbean.kernel; 018 019 import java.io.PrintStream; 020 import java.io.PrintWriter; 021 import java.util.Collections; 022 import java.util.Iterator; 023 import java.util.List; 024 import java.util.ListIterator; 025 026 /** 027 * Groups a collection of errors from a set of work so they maybe be thrown together from the kernel. This is used 028 * when the kernel does aggregate work on somthing that shouldn't fail such as when notifying kernel monitors or 029 * destroying the kernel. This allows the kernel to preform all required work and then throw any errors as a single 030 * exception object. 031 * 032 * @author Dain Sundstrom 033 * @version $Id$ 034 * @since 2.0 035 */ 036 public class KernelErrorsError extends Error { 037 private final List errors; 038 039 /** 040 * Creates an Errors error containing the list of errors. 041 * 042 * @param errors the errors 043 */ 044 public KernelErrorsError(List errors) { 045 if (errors == null) throw new NullPointerException("errors is null"); 046 if (errors.isEmpty()) throw new IllegalArgumentException("errors is empty"); 047 for (ListIterator iterator = errors.listIterator(); iterator.hasNext();) { 048 Object error = iterator.next(); 049 if (error == null) { 050 throw new IllegalArgumentException("Errors element " + iterator.previousIndex() + " is null"); 051 } 052 if (!(error instanceof Error)) { 053 throw new IllegalArgumentException("Errors element " + iterator.previousIndex() + 054 " is not an instance of java.lang.Error " + error.getClass() + ": " + error); 055 } 056 } 057 058 this.errors = Collections.unmodifiableList(errors); 059 } 060 061 /** 062 * Gets the errors that casued this error. 063 * 064 * @return the errors that casued this error 065 */ 066 public List getErrors() { 067 return errors; 068 } 069 070 public String getMessage() { 071 StringBuffer message = new StringBuffer(); 072 message.append(errors.size() + " Error(s) occured ["); 073 for (Iterator iterator = errors.iterator(); iterator.hasNext();) { 074 Error error = (Error) iterator.next(); 075 message.append('\"').append(error.getMessage()).append('\"'); 076 if (iterator.hasNext()) { 077 message.append(", "); 078 } 079 } 080 return message.append("]").toString(); 081 } 082 083 public String getLocalizedMessage() { 084 StringBuffer message = new StringBuffer(); 085 message.append(errors.size() + " Error(s) occured ["); 086 for (Iterator iterator = errors.iterator(); iterator.hasNext();) { 087 Error error = (Error) iterator.next(); 088 message.append('\"').append(error.getLocalizedMessage()).append('\"'); 089 if (iterator.hasNext()) { 090 message.append(", "); 091 } 092 } 093 return message.append("]").toString(); 094 } 095 096 public void printStackTrace(PrintStream stream) { 097 synchronized (stream) { 098 stream.println(this); 099 for (Iterator iterator = errors.iterator(); iterator.hasNext();) { 100 Error error = (Error) iterator.next(); 101 error.printStackTrace(stream); 102 } 103 } 104 } 105 106 public void printStackTrace(PrintWriter writer) { 107 synchronized (writer) { 108 writer.println(this); 109 for (Iterator iterator = errors.iterator(); iterator.hasNext();) { 110 Error error = (Error) iterator.next(); 111 error.printStackTrace(writer); 112 } 113 } 114 } 115 }