KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > FinderExceptionWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb;
30
31 import com.caucho.amber.AmberObjectNotFoundException;
32 import com.caucho.util.ExceptionWrapper;
33
34 import javax.ejb.FinderException JavaDoc;
35 import javax.ejb.ObjectNotFoundException JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * Wraps the actual exception with an EJB exception
41  */

42 public class FinderExceptionWrapper extends javax.ejb.FinderException JavaDoc
43   implements ExceptionWrapper {
44   private static final Logger JavaDoc log
45     = Logger.getLogger(FinderExceptionWrapper.class.getName());
46   
47   /**
48    * Null constructors for Serializable
49    */

50   public FinderExceptionWrapper()
51   {
52   }
53   /**
54    * Finder a basic FinderExceptionWrapper with a message.
55    *
56    * @param msg the exception message.
57    */

58   public FinderExceptionWrapper(String JavaDoc msg)
59   {
60     super(msg);
61   }
62
63   /**
64    * Finder a FinderExceptionWrapper wrapping a root exception.
65    *
66    * @param rootCause the underlying wrapped exception.
67    */

68   public FinderExceptionWrapper(Throwable JavaDoc rootCause)
69   {
70     super(rootCause.toString());
71
72     initCause(rootCause);
73   }
74
75   /**
76    * Wraps and exception with a finder exception wrapper.
77    */

78   public static FinderException JavaDoc create(Throwable JavaDoc rootCause)
79   {
80     while (rootCause instanceof ExceptionWrapper) {
81       ExceptionWrapper wrapper = (ExceptionWrapper) rootCause;
82
83       if (wrapper.getRootCause() != null)
84         rootCause = wrapper.getRootCause();
85       else
86         break;
87     }
88
89     if (rootCause instanceof FinderException JavaDoc)
90       return (FinderException JavaDoc) rootCause;
91     else if (rootCause instanceof AmberObjectNotFoundException) {
92       log.log(Level.FINER, rootCause.toString(), rootCause);
93       
94       return new ObjectNotFoundException(rootCause.getMessage());
95     }
96     else
97       return new FinderExceptionWrapper(rootCause);
98   }
99
100   /**
101    * Returns the root exception if it exists.
102    *
103    * @return the underlying wrapped exception.
104    */

105   public Throwable JavaDoc getRootCause()
106   {
107     return getCause();
108   }
109 }
110
111
Popular Tags