KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > JBossResourceException


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
27
28 import javax.resource.ResourceException JavaDoc;
29
30 import org.jboss.util.NestedThrowable;
31
32 /**
33  * Thrown to indicate a problem with a resource related operation.
34  *
35  * <p>
36  * Properly displays linked exception (ie. nested exception)
37  * when printing the stack trace.
38  *
39  * @version <tt>$Revision: 37459 $</tt>
40  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
41  */

42 public class JBossResourceException
43    extends ResourceException JavaDoc
44    implements NestedThrowable
45 {
46    /** The servial version uid*/
47    private static final long serialVersionUID = 6614203184612359692L;
48    
49    /**
50     * Rethrow as a resource exception if it is not already
51     *
52     * @param message the message
53     * @param t the original exception
54     * @throws ResourceException the resource exception
55     */

56    public static void rethrowAsResourceException(String JavaDoc message, Throwable JavaDoc t) throws ResourceException JavaDoc
57    {
58       if (t instanceof ResourceException JavaDoc)
59          throw (ResourceException JavaDoc) t;
60       else
61          throw new JBossResourceException(message, t);
62    }
63    
64    /**
65     * Construct a <tt>JBossResourceException</tt> with the specified detail
66     * message.
67     *
68     * @param msg Detail message.
69     */

70    public JBossResourceException(final String JavaDoc msg)
71    {
72       super(msg);
73    }
74
75    /**
76     * Construct a <tt>JBossResourceException</tt> with the specified detail
77     * message and error code.
78     *
79     * @param msg Detail message.
80     * @param code Error code.
81     */

82    public JBossResourceException(final String JavaDoc msg, final String JavaDoc code)
83    {
84       super(msg, code);
85    }
86
87    /**
88     * Construct a <tt>JBossResourceException</tt> with the specified detail
89     * message, error code and linked <tt>Exception</tt>.
90     *
91     * @param msg Detail message.
92     * @param code Error code.
93     * @param linked Linked <tt>Exception</tt>.
94     */

95    public JBossResourceException(final String JavaDoc msg, final String JavaDoc code, final Throwable JavaDoc linked)
96    {
97       super(msg, code);
98       setLinkedException(process(linked));
99    }
100
101    /**
102     * Construct a <tt>JBossResourceException</tt> with the specified detail
103     * message and linked <tt>Exception</tt>.
104     *
105     * @param msg Detail message.
106     * @param linked Linked <tt>Exception</tt>.
107     */

108    public JBossResourceException(final String JavaDoc msg, final Throwable JavaDoc linked)
109    {
110       super(msg);
111       setLinkedException(process(linked));
112    }
113
114    /**
115     * Construct a <tt>JBossResourceException</tt> with the specified
116     * linked <tt>Exception</tt>.
117     *
118     * @param linked Linked <tt>Exception</tt>.
119     */

120    public JBossResourceException(final Throwable JavaDoc linked)
121    {
122       this(linked.getMessage(), linked);
123    }
124
125    /**
126     * Return the nested <tt>Throwable</tt>.
127     *
128     * @return Nested <tt>Throwable</tt>.
129     */

130    public Throwable JavaDoc getNested()
131    {
132       return getLinkedException();
133    }
134
135    /**
136     * Return the nested <tt>Throwable</tt>.
137     *
138     * <p>For JDK 1.4 compatibility.
139     *
140     * @return Nested <tt>Throwable</tt>.
141     */

142    public Throwable JavaDoc getCause()
143    {
144       return getLinkedException();
145    }
146
147    /**
148     * Returns the composite throwable message.
149     *
150     * @return The composite throwable message.
151     */

152    public String JavaDoc getMessage()
153    {
154       return NestedThrowable.Util.getMessage(super.getMessage(), getLinkedException());
155    }
156
157    /**
158     * Prints the composite message and the embedded stack trace to the
159     * specified print stream.
160     *
161     * @param stream Stream to print to.
162     */

163    public void printStackTrace(final PrintStream JavaDoc stream)
164    {
165       Exception JavaDoc linked = getLinkedException();
166       if (linked == null || NestedThrowable.PARENT_TRACE_ENABLED)
167       {
168          super.printStackTrace(stream);
169       }
170       NestedThrowable.Util.print(linked, stream);
171    }
172
173    /**
174     * Prints the composite message and the embedded stack trace to the
175     * specified print writer.
176     *
177     * @param writer Writer to print to.
178     */

179    public void printStackTrace(final PrintWriter JavaDoc writer)
180    {
181       Exception JavaDoc linked = getLinkedException();
182       if (linked == null || NestedThrowable.PARENT_TRACE_ENABLED)
183       {
184          super.printStackTrace(writer);
185       }
186       NestedThrowable.Util.print(linked, writer);
187    }
188
189    /**
190     * Prints the composite message and the embedded stack trace to
191     * <tt>System.err</tt>.
192     */

193    public void printStackTrace()
194    {
195       printStackTrace(System.err);
196    }
197
198    private Exception JavaDoc process(Throwable JavaDoc t)
199    {
200       if (t instanceof Exception JavaDoc)
201       {
202          return (Exception JavaDoc)t;
203       } // end of if ()
204
return new UndeclaredThrowableException JavaDoc(t);
205    }
206 }
207
Popular Tags