001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: ChainedThrowableUtil.java,v 1.2 2006-06-15 13:47:00 sinisa Exp $
022: *
023: * formatted with JxBeauty (c) johann.langhofer@nextra.at
024: */
025:
026: package com.lutris.util;
027:
028: import java.io.PrintStream;
029: import java.io.PrintWriter;
030:
031: /**
032: * Utilities used to implement the Chained* throwables.
033: */
034: // compliance with WEBDOCWF begin
035: // WebDocWf extension
036: // The following line has been changed:
037: public class ChainedThrowableUtil {
038:
039: // before : class ChainedThrowableUtil {
040: // end of WebDocWf extension
041: // original line
042: // class ChainedThrowableUtil {
043: // compliance with WEBDOCWF end
044: /**
045: * Can't makes instances.
046: */
047: private ChainedThrowableUtil() {
048: }
049:
050: /**
051: * Generate the message to set for the exception message.
052: * When not explicit message is supplied.
053: */
054: // compliance with WEBDOCWF begin
055: // WebDocWf extension
056: // The following line has been changed:
057: public static String makeMessage(Throwable cause) {
058: // before: protected static String makeMessage(Throwable cause) {
059: // end of WebDocWf extension
060: // original line
061: //protected static String makeMessage(Throwable cause) {
062: // compliance with WEBDOCWF end
063: String causeMsg = cause.getMessage();
064: if (causeMsg == null) {
065: return cause.getClass().getName();
066: } else {
067: return causeMsg;
068: }
069: }
070:
071: /**
072: * Get the message associated with this exception.
073: */
074: // compliance with WEBDOCWF begin
075: // WebDocWf extension
076: // The following line has been changed:
077: public static String getMessage(ChainedThrowable except,
078: String super Msg) {
079: //before: protected static String getMessage(ChainedThrowable except, String superMsg) {
080: // end of WebDocWf extension
081: // original line
082: // protected static String getMessage(ChainedThrowable except,
083: // String superMsg) {
084: // compliance with WEBDOCWF end
085: Throwable cause = except.getCause();
086: if (cause == null) {
087: return super Msg;
088: } else {
089: String causeMsg = cause.getMessage();
090: if ((causeMsg == null) || (causeMsg.length() == 0)) {
091: causeMsg = cause.getClass().getName();
092: }
093: return super Msg + ": " + causeMsg;
094: }
095: }
096:
097: /**
098: * Do work of printing the causes of a chained exception. This is
099: * recursive. This attempts to following causing exceptions that are
100: * chained in other types of exception objects. If only Sun would
101: * standardize a mechanism in throwable instead of letting it accumulate
102: * in an ad-hoc manner.
103: */
104: private static void printChainedCauses(Throwable cause,
105: PrintWriter out) {
106: if (cause != null) {
107: out.println("*** Caused by:");
108: cause.printStackTrace(out);
109: if (cause instanceof ChainedThrowable) {
110: // If cause was is a ChainedThrowable, its chain has already
111: // been followed, otherwise, see what we can do.
112: } else if (cause instanceof java.awt.print.PrinterIOException) {
113: printChainedCauses(
114: ((java.awt.print.PrinterIOException) cause)
115: .getIOException(), out);
116: } else if (cause instanceof java.io.WriteAbortedException) {
117: printChainedCauses(
118: ((java.io.WriteAbortedException) cause).detail,
119: out);
120: } else if (cause instanceof java.lang.ClassNotFoundException) {
121: printChainedCauses(
122: ((java.lang.ClassNotFoundException) cause)
123: .getException(), out);
124: } else if (cause instanceof java.lang.ExceptionInInitializerError) {
125: printChainedCauses(
126: ((java.lang.ExceptionInInitializerError) cause)
127: .getException(), out);
128: } else if (cause instanceof java.lang.reflect.InvocationTargetException) {
129: printChainedCauses(
130: ((java.lang.reflect.InvocationTargetException) cause)
131: .getTargetException(), out);
132: } else if (cause instanceof java.rmi.RemoteException) {
133: printChainedCauses(
134: ((java.rmi.RemoteException) cause).detail, out);
135: } else if (cause instanceof java.rmi.activation.ActivationException) {
136: printChainedCauses(
137: ((java.rmi.activation.ActivationException) cause).detail,
138: out);
139: } else if (cause instanceof java.rmi.server.ServerCloneException) {
140: printChainedCauses(
141: ((java.rmi.server.ServerCloneException) cause).detail,
142: out);
143: } else if (cause instanceof java.security.PrivilegedActionException) {
144: printChainedCauses(
145: ((java.security.PrivilegedActionException) cause)
146: .getException(), out);
147: } else if (cause instanceof java.sql.SQLException) {
148: printChainedCauses(((java.sql.SQLException) cause)
149: .getNextException(), out);
150: } else if (cause instanceof org.xml.sax.SAXException) {
151: printChainedCauses(((org.xml.sax.SAXException) cause)
152: .getException(), out);
153: }
154: }
155: }
156:
157: /**
158: * Prints stacktrace and cause stacktrace.
159: */
160: // compliance with WEBDOCWF begin
161: // WebDocWf extension
162: // The following line has been changed:
163: public static void printCauseTrace(ChainedThrowable except) {
164: //before: protected static void printCauseTrace(ChainedThrowable except) {
165: // end of WebDocWf extension
166: // original line
167: // protected static void printCauseTrace(ChainedThrowable except) {
168: // compliance with WEBDOCWF end
169: PrintWriter pw = new PrintWriter(System.err);
170: printChainedCauses(except.getCause(), pw);
171: pw.flush();
172: }
173:
174: /**
175: * Prints stacktrace and cause stacktrace.
176: */
177: // compliance with WEBDOCWF begin
178: // WebDocWf extension
179: // The following line has been changed:
180: public static void printCauseTrace(ChainedThrowable except,
181: PrintStream s) {
182: //before: protected static void printCauseTrace(ChainedThrowable except, PrintStream s) {
183: // end of WebDocWf extension
184: // original line
185: // protected static void printCauseTrace(ChainedThrowable except,
186: //PrintStream s) {
187: // compliance with WEBDOCWF end
188: PrintWriter pw = new PrintWriter(s);
189: printChainedCauses(except.getCause(), pw);
190: pw.flush();
191: }
192:
193: /**
194: * Prints stacktrace and cause stacktrace.
195: */
196: public static void printCauseTrace(ChainedThrowable except,
197: PrintWriter out) {
198: printChainedCauses(except.getCause(), out);
199: out.flush();
200: }
201: }
|