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: */package org.apache.geronimo.common;
017:
018: import java.util.List;
019: import java.util.ArrayList;
020: import java.io.PrintStream;
021: import java.io.PrintWriter;
022:
023: /**
024: * @version $Rev: 556119 $ $Date: 2007-07-13 12:34:02 -0700 (Fri, 13 Jul 2007) $
025: */
026: public class DeploymentException extends Exception {
027:
028: private final List<? extends Throwable> causes;
029:
030: public DeploymentException() {
031: causes = null;
032: }
033:
034: public DeploymentException(Throwable cause) {
035: super (cause);
036: causes = null;
037: }
038:
039: public DeploymentException(String message) {
040: super (message);
041: causes = null;
042: }
043:
044: public DeploymentException(String message, Throwable cause) {
045: super (message, cause);
046: causes = null;
047: }
048:
049: public DeploymentException(String message,
050: List<? extends Throwable> causes) {
051: super (message);
052: this .causes = causes;
053: }
054:
055: public void printStackTrace(PrintStream ps) {
056: super .printStackTrace(ps);
057: if (causes != null) {
058: for (Throwable cause : causes) {
059: //TODO trim duplicate stack trace elements
060: cause.printStackTrace(ps);
061: }
062: }
063: }
064:
065: public void printStackTrace(PrintWriter pw) {
066: super .printStackTrace(pw);
067: if (causes != null) {
068: for (Throwable cause : causes) {
069: //TODO trim duplicate stack trace elements
070: cause.printStackTrace(pw);
071: }
072: }
073: }
074:
075: public DeploymentException cleanse() {
076: if (null != getCause()) {
077: return cleanse(this );
078: }
079: if (causes != null) {
080: List<CleanseException> cleansedCauses = new ArrayList<CleanseException>(
081: causes.size());
082: for (Throwable cause : causes) {
083: CleanseException cleansed = cleanse(cause);
084: cleansedCauses.add(cleansed);
085: }
086: return new DeploymentException(getMessage(), cleansedCauses);
087: }
088: return this ;
089: }
090:
091: protected static CleanseException cleanse(Throwable root) {
092: CleanseException previousEx = null;
093: CleanseException rootEx = null;
094: while (null != root) {
095: Throwable e = root.getCause();
096: CleanseException exception = new CleanseException(root
097: .getMessage(), root.toString());
098: if (null == rootEx) {
099: rootEx = exception;
100: }
101: exception.setStackTrace(root.getStackTrace());
102: if (null != previousEx) {
103: previousEx.initCause(exception);
104: }
105: previousEx = exception;
106: root = e;
107: }
108: return rootEx;
109: }
110:
111: private static class CleanseException extends DeploymentException {
112: private final String toString;
113:
114: public CleanseException(String message, String toString) {
115: super (message);
116: this .toString = toString;
117: }
118:
119: public String toString() {
120: return toString;
121: }
122: }
123: }
|