001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.aegis;
019:
020: import java.io.PrintStream;
021: import java.io.PrintWriter;
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: /**
026: *
027: *
028: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
029: * @since Feb 14, 2004
030: */
031: public class DatabindingException extends RuntimeException {
032:
033: private final List<String> extraMessages = new LinkedList<String>();
034:
035: /**
036: * Constructs a new exception with the specified detail
037: * message.
038: *
039: * @param message the detail message.
040: */
041: public DatabindingException(String message) {
042: super (message);
043: }
044:
045: /**
046: * Constructs a new exception with the specified detail
047: * message and cause.
048: *
049: * @param message the detail message.
050: * @param cause the cause.
051: */
052: public DatabindingException(String message, Throwable cause) {
053: super (message, cause);
054: }
055:
056: /**
057: * Return the detail message, including the message from the
058: * {@link #getCause() nested exception} if there is one.
059: *
060: * @return the detail message.
061: */
062: public String getMessage() {
063: if (getCause() == null || getCause() == this ) {
064: return getActualMessage();
065: } else {
066: return getActualMessage() + ". Nested exception is "
067: + getCause().getClass().getName() + ": "
068: + getCause().getMessage();
069: }
070: }
071:
072: public String getActualMessage() {
073: if (extraMessages.isEmpty()) {
074: return super .getMessage();
075: }
076: StringBuffer buf = new StringBuffer();
077: for (String s : extraMessages) {
078: buf.append(s);
079: }
080: return buf.toString();
081: }
082:
083: /**
084: * Prints this throwable and its backtrace to the specified print stream.
085: *
086: * @param s <code>PrintStream</code> to use for output
087: */
088: @Override
089: public void printStackTrace(PrintStream s) {
090: if (getCause() == null || getCause() == this ) {
091: super .printStackTrace(s);
092: } else {
093: s.println(this );
094: getCause().printStackTrace(s);
095: }
096: }
097:
098: /**
099: * Prints this throwable and its backtrace to the specified print writer.
100: *
101: * @param w <code>PrintWriter</code> to use for output
102: */
103: @Override
104: public void printStackTrace(PrintWriter w) {
105: if (getCause() == null || getCause() == this ) {
106: super .printStackTrace(w);
107: } else {
108: w.println(this );
109: getCause().printStackTrace(w);
110: }
111: }
112:
113: public final void prepend(String m) {
114: extraMessages.add(0, m + ": ");
115: }
116:
117: public void setMessage(String s) {
118: extraMessages.clear();
119: extraMessages.add(s);
120: }
121: }
|