KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > naming > NamingExceptionWrapper


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.naming;
30
31 import com.caucho.util.ExceptionWrapper;
32
33 import javax.naming.NamingException JavaDoc;
34 import java.io.PrintStream JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36
37 /**
38  * Wraps the actual exception with a Naming exception
39  */

40 public class NamingExceptionWrapper extends NamingException JavaDoc
41   implements ExceptionWrapper {
42   private Throwable JavaDoc rootCause;
43
44   /**
45    * Null constructor for beans
46    */

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

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

65   public NamingExceptionWrapper(Throwable JavaDoc rootCause)
66   {
67     super(rootCause.getMessage());
68
69     this.rootCause = rootCause;
70   }
71
72   /**
73    * Returns the root exception if it exists.
74    *
75    * @return the underlying wrapped exception.
76    */

77   public Throwable JavaDoc getRootCause()
78   {
79     return rootCause;
80   }
81
82   /**
83    * Returns the appropriate exception message.
84    */

85   public String JavaDoc getMessage()
86   {
87     if (rootCause != null)
88       return rootCause.getMessage();
89     else
90       return super.getMessage();
91   }
92
93   /**
94    * Prints the stack trace, preferring the root cause if it exists.
95    */

96   public void printStackTrace()
97   {
98     if (rootCause != null)
99       rootCause.printStackTrace();
100     else
101       super.printStackTrace();
102   }
103
104   /**
105    * Prints the stack trace, preferring the root cause if it exists.
106    */

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

118   public void printStackTrace(PrintWriter JavaDoc os)
119   {
120     if (rootCause != null)
121       rootCause.printStackTrace(os);
122     else
123       super.printStackTrace(os);
124   }
125
126   /**
127    * Print the exception as a string.
128    */

129   public String JavaDoc toString()
130   {
131     if (rootCause == null)
132       return super.toString();
133     else
134       return getClass().getName() + ": " + rootCause;
135   }
136 }
137
138
Popular Tags