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