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: DodsBaseExceptionUtil.java,v 1.1 2007-01-24 16:59:09 sinisa Exp $
022: *
023: * formatted with JxBeauty (c) johann.langhofer@nextra.at
024: */
025:
026: package org.enhydra.dods.exceptions;
027:
028: import java.io.*;
029:
030: /**
031: * Utilities used to implement the DodsBaseException.
032: */
033: // compliance with WEBDOCWF begin
034: // WebDocWf extension
035: // The following line has been changed:
036: public class DodsBaseExceptionUtil {
037:
038: // before : class ChainedThrowableUtil {
039: // end of WebDocWf extension
040: // original line
041: // class ChainedThrowableUtil {
042: // compliance with WEBDOCWF end
043: /**
044: * Can't makes instances.
045: */
046: private DodsBaseExceptionUtil() {
047: }
048:
049: /**
050: * Generate the message to set for the exception message.
051: * When not explicit message is supplied.
052: */
053: // compliance with WEBDOCWF begin
054: // WebDocWf extension
055: // The following line has been changed:
056: public static String makeMessage(Throwable cause) {
057: // before: protected static String makeMessage(Throwable cause) {
058: // end of WebDocWf extension
059: // original line
060: //protected static String makeMessage(Throwable cause) {
061: // compliance with WEBDOCWF end
062: String causeMsg = cause.getMessage();
063: if (causeMsg == null) {
064: return cause.getClass().getName();
065: } else {
066: return causeMsg;
067: }
068: }
069:
070: /**
071: * Get the message associated with this exception.
072: */
073: // compliance with WEBDOCWF begin
074: // WebDocWf extension
075: // The following line has been changed:
076: public static String getMessage(DodsBaseException except,
077: String super Msg) {
078: //before: protected static String getMessage(DodsBaseException except, String superMsg) {
079: // end of WebDocWf extension
080: // original line
081: // protected static String getMessage(DodsBaseException except,
082: // String superMsg) {
083: // compliance with WEBDOCWF end
084: Throwable cause = except.getCause();
085: if (cause == null) {
086: return super Msg;
087: } else {
088: String causeMsg = cause.getMessage();
089: if ((causeMsg == null) || (causeMsg.length() == 0)) {
090: causeMsg = cause.getClass().getName();
091: }
092: return super Msg + ": " + causeMsg;
093: }
094: }
095:
096: /**
097: * Do work of printing the causes of a chained exception. This is
098: * recursive. This attempts to following causing exceptions that are
099: * chained in other types of exception objects. If only Sun would
100: * standardize a mechanism in throwable instead of letting it accumulate
101: * in an ad-hoc manner.
102: */
103: private static void printChainedCauses(Throwable cause,
104: PrintWriter out) {
105: if (cause != null) {
106: out.println("*** Caused by:");
107: cause.printStackTrace(out);
108: if (cause instanceof DodsBaseException) {
109: // If cause was is a DodsBaseException, its chain has already
110: // been followed, otherwise, see what we can do.
111: } else if (cause instanceof java.awt.print.PrinterIOException) {
112: printChainedCauses(
113: ((java.awt.print.PrinterIOException) cause)
114: .getIOException(), out);
115: } else if (cause instanceof java.io.WriteAbortedException) {
116: printChainedCauses(
117: ((java.io.WriteAbortedException) cause).detail,
118: out);
119: } else if (cause instanceof java.lang.ClassNotFoundException) {
120: printChainedCauses(
121: ((java.lang.ClassNotFoundException) cause)
122: .getException(), out);
123: } else if (cause instanceof java.lang.ExceptionInInitializerError) {
124: printChainedCauses(
125: ((java.lang.ExceptionInInitializerError) cause)
126: .getException(), out);
127: } else if (cause instanceof java.lang.reflect.InvocationTargetException) {
128: printChainedCauses(
129: ((java.lang.reflect.InvocationTargetException) cause)
130: .getTargetException(), out);
131: } else if (cause instanceof java.rmi.RemoteException) {
132: printChainedCauses(
133: ((java.rmi.RemoteException) cause).detail, out);
134: } else if (cause instanceof java.rmi.activation.ActivationException) {
135: printChainedCauses(
136: ((java.rmi.activation.ActivationException) cause).detail,
137: out);
138: } else if (cause instanceof java.rmi.server.ServerCloneException) {
139: printChainedCauses(
140: ((java.rmi.server.ServerCloneException) cause).detail,
141: out);
142: } else if (cause instanceof java.security.PrivilegedActionException) {
143: printChainedCauses(
144: ((java.security.PrivilegedActionException) cause)
145: .getException(), out);
146: } else if (cause instanceof java.sql.SQLException) {
147: printChainedCauses(((java.sql.SQLException) cause)
148: .getNextException(), out);
149: } else if (cause instanceof org.xml.sax.SAXException) {
150: printChainedCauses(((org.xml.sax.SAXException) cause)
151: .getException(), out);
152: }
153: }
154: }
155:
156: /**
157: * Prints stacktrace and cause stacktrace.
158: */
159: // compliance with WEBDOCWF begin
160: // WebDocWf extension
161: // The following line has been changed:
162: public static void printCauseTrace(DodsBaseException except) {
163: //before: protected static void printCauseTrace(DodsBaseException except) {
164: // end of WebDocWf extension
165: // original line
166: // protected static void printCauseTrace(DodsBaseException except) {
167: // compliance with WEBDOCWF end
168: PrintWriter pw = new PrintWriter(System.err);
169: printChainedCauses(except.getCause(), pw);
170: pw.flush();
171: }
172:
173: /**
174: * Prints stacktrace and cause stacktrace.
175: */
176: // compliance with WEBDOCWF begin
177: // WebDocWf extension
178: // The following line has been changed:
179: public static void printCauseTrace(DodsBaseException except,
180: PrintStream s) {
181: //before: protected static void printCauseTrace(DodsBaseException except, PrintStream s) {
182: // end of WebDocWf extension
183: // original line
184: // protected static void printCauseTrace(DodsBaseException except,
185: //PrintStream s) {
186: // compliance with WEBDOCWF end
187: PrintWriter pw = new PrintWriter(s);
188: printChainedCauses(except.getCause(), pw);
189: pw.flush();
190: }
191:
192: /**
193: * Prints stacktrace and cause stacktrace.
194: */
195: public static void printCauseTrace(DodsBaseException except,
196: PrintWriter out) {
197: printChainedCauses(except.getCause(), out);
198: out.flush();
199: }
200: }
|