KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: RemoteExceptionWrapper.java,v 1.2 2004/09/29 00:13:01 cvs Exp $
29  */

30
31 package com.caucho.ejb;
32
33 import com.caucho.util.ExceptionWrapper;
34
35 import java.io.PrintStream JavaDoc;
36 import java.io.PrintWriter JavaDoc;
37
38 /**
39  * Wraps the actual exception with an Remote exception
40  */

41 public class RemoteExceptionWrapper extends java.rmi.RemoteException JavaDoc
42   implements ExceptionWrapper {
43   private Throwable JavaDoc rootCause;
44
45   /**
46    * Null constructor for beans
47    */

48   public RemoteExceptionWrapper()
49   {
50   }
51   /**
52    * Create a basic RemoteExceptionWrapper with a message.
53    *
54    * @param msg the exception message.
55    */

56   public RemoteExceptionWrapper(String JavaDoc msg)
57   {
58     super(msg);
59   }
60
61   /**
62    * Create a RemoteExceptionWrapper wrapping a root exception.
63    *
64    * @param rootCause the underlying wrapped exception.
65    */

66   public RemoteExceptionWrapper(Throwable JavaDoc rootCause)
67   {
68     super(rootCause.getMessage());
69
70     this.rootCause = rootCause;
71   }
72
73   public static RemoteExceptionWrapper create(Throwable JavaDoc rootCause)
74   {
75     if (rootCause instanceof RemoteExceptionWrapper)
76       return (RemoteExceptionWrapper) rootCause;
77     else
78       return new RemoteExceptionWrapper(rootCause);
79   }
80
81   /**
82    * Returns the root exception if it exists.
83    *
84    * @return the underlying wrapped exception.
85    */

86   public Throwable JavaDoc getRootCause()
87   {
88     return rootCause;
89   }
90
91   /**
92    * Returns the appropriate exception message.
93    */

94   public String JavaDoc getMessage()
95   {
96     if (rootCause != null)
97       return rootCause.getMessage();
98     else
99       return super.getMessage();
100   }
101
102   /**
103    * Prints the stack trace, preferring the root cause if it exists.
104    */

105   public void printStackTrace()
106   {
107     if (rootCause != null)
108       rootCause.printStackTrace();
109     else
110       super.printStackTrace();
111   }
112
113   /**
114    * Prints the stack trace, preferring the root cause if it exists.
115    */

116   public void printStackTrace(PrintStream JavaDoc os)
117   {
118     if (rootCause != null)
119       rootCause.printStackTrace(os);
120     else
121       super.printStackTrace(os);
122   }
123
124   /**
125    * Prints the stack trace, preferring the root cause if it exists.
126    */

127   public void printStackTrace(PrintWriter JavaDoc os)
128   {
129     if (rootCause != null)
130       rootCause.printStackTrace(os);
131     else
132       super.printStackTrace(os);
133   }
134
135   /**
136    * Print the exception as a string.
137    */

138   public String JavaDoc toString()
139   {
140     if (rootCause == null)
141       return super.toString();
142     else
143       return getClass().getName() + ": " + rootCause;
144   }
145 }
146
147
Popular Tags