001: // ========================================================================
002: // Copyright 1999-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import java.io.PrintStream;
018: import java.io.PrintWriter;
019: import java.util.List;
020:
021: /* ------------------------------------------------------------ */
022: /** Wraps multiple exceptions.
023: *
024: * Allows multiple exceptions to be thrown as a single exception.
025: *
026: * @author Greg Wilkins (gregw)
027: */
028: public class MultiException extends Exception {
029: private Object nested;
030:
031: /* ------------------------------------------------------------ */
032: public MultiException() {
033: super ("Multiple exceptions");
034: }
035:
036: /* ------------------------------------------------------------ */
037: public void add(Throwable e) {
038: if (e instanceof MultiException) {
039: MultiException me = (MultiException) e;
040: for (int i = 0; i < LazyList.size(me.nested); i++)
041: nested = LazyList.add(nested, LazyList
042: .get(me.nested, i));
043: } else
044: nested = LazyList.add(nested, e);
045: }
046:
047: /* ------------------------------------------------------------ */
048: public int size() {
049: return LazyList.size(nested);
050: }
051:
052: /* ------------------------------------------------------------ */
053: public List getThrowables() {
054: return LazyList.getList(nested);
055: }
056:
057: /* ------------------------------------------------------------ */
058: public Throwable getThrowable(int i) {
059: return (Throwable) LazyList.get(nested, i);
060: }
061:
062: /* ------------------------------------------------------------ */
063: /** Throw a multiexception.
064: * If this multi exception is empty then no action is taken. If it
065: * contains a single exception that is thrown, otherwise the this
066: * multi exception is thrown.
067: * @exception Exception
068: */
069: public void ifExceptionThrow() throws Exception {
070: switch (LazyList.size(nested)) {
071: case 0:
072: break;
073: case 1:
074: Throwable th = (Throwable) LazyList.get(nested, 0);
075: if (th instanceof Error)
076: throw (Error) th;
077: if (th instanceof Exception)
078: throw (Exception) th;
079: default:
080: throw this ;
081: }
082: }
083:
084: /* ------------------------------------------------------------ */
085: /** Throw a Runtime exception.
086: * If this multi exception is empty then no action is taken. If it
087: * contains a single error or runtime exception that is thrown, otherwise the this
088: * multi exception is thrown, wrapped in a runtime exception.
089: * @exception Error If this exception contains exactly 1 {@link Error}
090: * @exception RuntimeException If this exception contains 1 {@link Throwable} but it is not an error,
091: * or it contains more than 1 {@link Throwable} of any type.
092: */
093: public void ifExceptionThrowRuntime() throws Error {
094: switch (LazyList.size(nested)) {
095: case 0:
096: break;
097: case 1:
098: Throwable th = (Throwable) LazyList.get(nested, 0);
099: if (th instanceof Error)
100: throw (Error) th;
101: else if (th instanceof RuntimeException)
102: throw (RuntimeException) th;
103: else
104: throw new RuntimeException(th);
105: default:
106: throw new RuntimeException(this );
107: }
108: }
109:
110: /* ------------------------------------------------------------ */
111: /** Throw a multiexception.
112: * If this multi exception is empty then no action is taken. If it
113: * contains a any exceptions then this
114: * multi exception is thrown.
115: */
116: public void ifExceptionThrowMulti() throws MultiException {
117: if (LazyList.size(nested) > 0)
118: throw this ;
119: }
120:
121: /* ------------------------------------------------------------ */
122: public String toString() {
123: if (LazyList.size(nested) > 0)
124: return "org.mortbay.util.MultiException"
125: + LazyList.getList(nested);
126: return "org.mortbay.util.MultiException[]";
127: }
128:
129: /* ------------------------------------------------------------ */
130: public void printStackTrace() {
131: super .printStackTrace();
132: for (int i = 0; i < LazyList.size(nested); i++)
133: ((Throwable) LazyList.get(nested, i)).printStackTrace();
134: }
135:
136: /* ------------------------------------------------------------------------------- */
137: /**
138: * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
139: */
140: public void printStackTrace(PrintStream out) {
141: super .printStackTrace(out);
142: for (int i = 0; i < LazyList.size(nested); i++)
143: ((Throwable) LazyList.get(nested, i)).printStackTrace(out);
144: }
145:
146: /* ------------------------------------------------------------------------------- */
147: /**
148: * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
149: */
150: public void printStackTrace(PrintWriter out) {
151: super .printStackTrace(out);
152: for (int i = 0; i < LazyList.size(nested); i++)
153: ((Throwable) LazyList.get(nested, i)).printStackTrace(out);
154: }
155:
156: }
|