KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ExceptionWrapper;
32
33 import javax.ejb.RemoveException JavaDoc;
34
35 /**
36  * Wraps the actual exception with an EJB exception
37  */

38 public class RemoveExceptionWrapper extends javax.ejb.RemoveException JavaDoc
39   implements ExceptionWrapper {
40   private Throwable JavaDoc rootCause;
41
42   /**
43    * Null constructors for Serializable
44    */

45   public RemoveExceptionWrapper()
46   {
47   }
48   /**
49    * Remove a basic RemoveExceptionWrapper with a message.
50    *
51    * @param msg the exception message.
52    */

53   public RemoveExceptionWrapper(String JavaDoc msg)
54   {
55     super(msg);
56   }
57
58   /**
59    * Remove a RemoveExceptionWrapper wrapping a root exception.
60    *
61    * @param rootCause the underlying wrapped exception.
62    */

63   public RemoveExceptionWrapper(Throwable JavaDoc rootCause)
64   {
65     super(rootCause.getMessage());
66
67     this.rootCause = rootCause;
68   }
69
70   /**
71    * Wraps and exception with a remove exception wrapper.
72    */

73   public static RemoveException JavaDoc create(Throwable JavaDoc rootCause)
74   {
75     while (rootCause instanceof ExceptionWrapper) {
76       ExceptionWrapper wrapper = (ExceptionWrapper) rootCause;
77
78       if (wrapper.getRootCause() != null)
79         rootCause = wrapper.getRootCause();
80       else
81         break;
82     }
83
84     if (rootCause instanceof RemoveException JavaDoc)
85       return (RemoveException JavaDoc) rootCause;
86     else
87       return new RemoveExceptionWrapper(rootCause);
88   }
89
90   /**
91    * Returns the root exception if it exists.
92    *
93    * @return the underlying wrapped exception.
94    */

95   public Throwable JavaDoc getRootCause()
96   {
97     return rootCause;
98   }
99
100   /**
101    * Returns the appropriate exception message.
102    */

103   public String JavaDoc getMessage()
104   {
105     if (rootCause != null)
106       return rootCause.getMessage();
107     else
108       return super.getMessage();
109   }
110
111   /**
112    * Print the exception as a string.
113    */

114   public String JavaDoc toString()
115   {
116     if (rootCause == null)
117       return super.toString();
118     else
119       return getClass().getName() + ": " + rootCause;
120   }
121 }
122
123
Popular Tags