001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: *
028: * $Id: RemoteExceptionWrapper.java,v 1.2 2004/09/29 00:13:01 cvs Exp $
029: */
030:
031: package com.caucho.ejb;
032:
033: import com.caucho.util.ExceptionWrapper;
034:
035: import java.io.PrintStream;
036: import java.io.PrintWriter;
037:
038: /**
039: * Wraps the actual exception with an Remote exception
040: */
041: public class RemoteExceptionWrapper extends java.rmi.RemoteException
042: implements ExceptionWrapper {
043: private Throwable rootCause;
044:
045: /**
046: * Null constructor for beans
047: */
048: public RemoteExceptionWrapper() {
049: }
050:
051: /**
052: * Create a basic RemoteExceptionWrapper with a message.
053: *
054: * @param msg the exception message.
055: */
056: public RemoteExceptionWrapper(String msg) {
057: super (msg);
058: }
059:
060: /**
061: * Create a RemoteExceptionWrapper wrapping a root exception.
062: *
063: * @param rootCause the underlying wrapped exception.
064: */
065: public RemoteExceptionWrapper(Throwable rootCause) {
066: super (rootCause.getMessage());
067:
068: this .rootCause = rootCause;
069: }
070:
071: public static RemoteExceptionWrapper create(Throwable rootCause) {
072: if (rootCause instanceof RemoteExceptionWrapper)
073: return (RemoteExceptionWrapper) rootCause;
074: else
075: return new RemoteExceptionWrapper(rootCause);
076: }
077:
078: /**
079: * Returns the root exception if it exists.
080: *
081: * @return the underlying wrapped exception.
082: */
083: public Throwable getRootCause() {
084: return rootCause;
085: }
086:
087: /**
088: * Returns the appropriate exception message.
089: */
090: public String getMessage() {
091: if (rootCause != null)
092: return rootCause.getMessage();
093: else
094: return super .getMessage();
095: }
096:
097: /**
098: * Prints the stack trace, preferring the root cause if it exists.
099: */
100: public void printStackTrace() {
101: if (rootCause != null)
102: rootCause.printStackTrace();
103: else
104: super .printStackTrace();
105: }
106:
107: /**
108: * Prints the stack trace, preferring the root cause if it exists.
109: */
110: public void printStackTrace(PrintStream os) {
111: if (rootCause != null)
112: rootCause.printStackTrace(os);
113: else
114: super .printStackTrace(os);
115: }
116:
117: /**
118: * Prints the stack trace, preferring the root cause if it exists.
119: */
120: public void printStackTrace(PrintWriter os) {
121: if (rootCause != null)
122: rootCause.printStackTrace(os);
123: else
124: super .printStackTrace(os);
125: }
126:
127: /**
128: * Print the exception as a string.
129: */
130: public String toString() {
131: if (rootCause == null)
132: return super .toString();
133: else
134: return getClass().getName() + ": " + rootCause;
135: }
136: }
|