1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a 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;
25 import java.io.PrintStream;
26 import java.lang.reflect.UndeclaredThrowableException;
27
28 import javax.resource.ResourceException;
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: 71547 $</tt>
40 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
41 */
42 public class JBossResourceException
43 extends ResourceException
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 message, Throwable t) throws ResourceException
57 {
58 if (t instanceof ResourceException)
59 throw (ResourceException) 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 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 msg, final String 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 msg, final String code, final Throwable 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 msg, final Throwable 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 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 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 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 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 stream)
164 {
165 Exception 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 writer)
180 {
181 Exception 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 process(Throwable t)
199 {
200 if (t instanceof Exception)
201 {
202 return (Exception)t;
203 } // end of if ()
204 return new UndeclaredThrowableException(t);
205 }
206 }