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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb;
031:
032: import javax.ejb.EJBException;
033: import java.lang.reflect.InvocationTargetException;
034:
035: /**
036: * Wraps the actual exception with an EJB exception
037: */
038: public class EJBExceptionWrapper extends EJBException {
039: private Throwable _rootCause;
040:
041: /**
042: * Null constructor for beans
043: */
044: public EJBExceptionWrapper() {
045: }
046:
047: /**
048: * Create a basic EJBExceptionWrapper with a message.
049: *
050: * @param msg the exception message.
051: */
052: public EJBExceptionWrapper(String msg) {
053: super (msg);
054: }
055:
056: /**
057: * Create a EJBExceptionWrapper wrapping a root exception.
058: *
059: * @param rootCause the underlying wrapped exception.
060: */
061: public EJBExceptionWrapper(Throwable rootCause) {
062: super (rootCause.toString());
063:
064: _rootCause = rootCause;
065:
066: initCause(rootCause);
067: }
068:
069: /**
070: * Create a EJBExceptionWrapper wrapping a root exception.
071: *
072: * @param rootCause the underlying wrapped exception.
073: */
074: public EJBExceptionWrapper(String msg, Throwable rootCause) {
075: super (msg);
076:
077: _rootCause = rootCause;
078:
079: initCause(rootCause);
080: }
081:
082: public Throwable getCause() {
083: return _rootCause;
084: }
085:
086: /**
087: * Creates an EJBException from a throwable.
088: */
089: public static EJBException create(Throwable exn) {
090: if (exn instanceof EJBException)
091: return (EJBException) exn;
092: else if (exn instanceof InvocationTargetException)
093: return create(exn.getCause());
094: else
095: return new EJBExceptionWrapper(exn);
096: }
097:
098: /**
099: * Creates a runtime from a throwable.
100: */
101: public static RuntimeException createRuntime(Throwable rootCause) {
102: if (rootCause instanceof RuntimeException)
103: return ((RuntimeException) rootCause);
104: else
105: return new EJBExceptionWrapper(rootCause);
106: }
107: }
|