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:
029: package com.caucho.ejb;
030:
031: import com.caucho.amber.AmberObjectNotFoundException;
032: import com.caucho.util.ExceptionWrapper;
033:
034: import javax.ejb.FinderException;
035: import javax.ejb.ObjectNotFoundException;
036: import java.util.logging.Level;
037: import java.util.logging.Logger;
038:
039: /**
040: * Wraps the actual exception with an EJB exception
041: */
042: public class FinderExceptionWrapper extends javax.ejb.FinderException
043: implements ExceptionWrapper {
044: private static final Logger log = Logger
045: .getLogger(FinderExceptionWrapper.class.getName());
046:
047: /**
048: * Null constructors for Serializable
049: */
050: public FinderExceptionWrapper() {
051: }
052:
053: /**
054: * Finder a basic FinderExceptionWrapper with a message.
055: *
056: * @param msg the exception message.
057: */
058: public FinderExceptionWrapper(String msg) {
059: super (msg);
060: }
061:
062: /**
063: * Finder a FinderExceptionWrapper wrapping a root exception.
064: *
065: * @param rootCause the underlying wrapped exception.
066: */
067: public FinderExceptionWrapper(Throwable rootCause) {
068: super (rootCause.toString());
069:
070: initCause(rootCause);
071: }
072:
073: /**
074: * Wraps and exception with a finder exception wrapper.
075: */
076: public static FinderException create(Throwable rootCause) {
077: while (rootCause instanceof ExceptionWrapper) {
078: ExceptionWrapper wrapper = (ExceptionWrapper) rootCause;
079:
080: if (wrapper.getRootCause() != null)
081: rootCause = wrapper.getRootCause();
082: else
083: break;
084: }
085:
086: if (rootCause instanceof FinderException)
087: return (FinderException) rootCause;
088: else if (rootCause instanceof AmberObjectNotFoundException) {
089: log.log(Level.FINER, rootCause.toString(), rootCause);
090:
091: return new ObjectNotFoundException(rootCause.getMessage());
092: } else
093: return new FinderExceptionWrapper(rootCause);
094: }
095:
096: /**
097: * Returns the root exception if it exists.
098: *
099: * @return the underlying wrapped exception.
100: */
101: public Throwable getRootCause() {
102: return getCause();
103: }
104: }
|