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.mq;
23
24 import java.io.PrintWriter;
25 import java.io.PrintStream;
26
27 import javax.jms.JMSException;
28
29 import org.jboss.util.NestedThrowable;
30 import org.jboss.util.NestedException;
31
32 /**
33 * A common superclass for <tt>JMSException</tt> classes that can contain a
34 * nested <tt>Throwable</tt> detail object.
35 *
36 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
37 * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
38 * @version <tt>$Revision: 45317 $</tt>
39 */
40 public class SpyJMSException extends JMSException implements NestedThrowable
41 {
42 /** The serialVersionUID */
43 static final long serialVersionUID = 5216406958161784593L;
44
45 /** The nested throwable */
46 protected Throwable nested;
47
48 /**
49 * Converts a throwable to a JMSException if it is not already
50 *
51 * @param message any message to add to a constructed JMSException
52 * @param t the throwable
53 * @throws JMSException always
54 */
55 public static void rethrowAsJMSException(String message, Throwable t) throws JMSException
56 {
57 throw getAsJMSException(message, t);
58 }
59
60 /**
61 * Converts a throwable to a JMSException if it is not already
62 *
63 * @param message any message to add to a constructed JMSException
64 * @param t the throwable
65 * @return a JMSException
66 */
67 public static JMSException getAsJMSException(String message, Throwable t)
68 {
69 if (t instanceof JMSException)
70 return (JMSException) t;
71 else
72 return new SpyJMSException(message, t);
73 }
74
75 /**
76 * Construct a <tt>SpyJMSException</tt> with the specified detail message.
77 *
78 * @param msg Detail message.
79 */
80 public SpyJMSException(final String msg)
81 {
82 super(msg);
83 this.nested = null;
84 }
85
86 /**
87 * Construct a <tt>SpyJMSException</tt> with the specified detail message
88 * and error code.
89 *
90 * @param msg Detail message.
91 * @param code Error code.
92 */
93 public SpyJMSException(final String msg, final String code)
94 {
95 super(msg, code);
96 this.nested = null;
97 }
98
99 /**
100 * Construct a <tt>SpyJMSException</tt> with the specified detail message
101 * and nested <tt>Throwable</tt>.
102 *
103 * @param msg Detail message.
104 * @param nested Nested <tt>Throwable</tt>.
105 */
106 public SpyJMSException(final String msg, final Throwable nested)
107 {
108 super(msg);
109 this.nested = nested;
110 NestedThrowable.Util.checkNested(this, nested);
111 }
112
113 /**
114 * Construct a <tt>SpyJMSException</tt> with the specified nested <tt>Throwable</tt>.
115 *
116 * @param nested Nested <tt>Throwable</tt>.
117 */
118 public SpyJMSException(final Throwable nested)
119 {
120 this(nested.getMessage(), nested);
121 }
122
123 public void setLinkedException(final Exception e)
124 {
125 this.nested = e;
126 }
127
128 public Exception getLinkedException()
129 {
130 // jason: this is bad, but whatever... the jms folks should have had more
131 // insight
132 if (nested == null)
133 return this;
134 if (nested instanceof Exception)
135 return (Exception) nested;
136 return new NestedException(nested);
137 }
138
139 public Throwable getNested()
140 {
141 return nested;
142 }
143
144 public Throwable getCause()
145 {
146 return nested;
147 }
148
149 public String getMessage()
150 {
151 return NestedThrowable.Util.getMessage(super.getMessage(), nested);
152 }
153
154 public void printStackTrace(final PrintStream stream)
155 {
156 if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED)
157 super.printStackTrace(stream);
158 NestedThrowable.Util.print(nested, stream);
159 }
160
161 public void printStackTrace(final PrintWriter writer)
162 {
163 if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED)
164 super.printStackTrace(writer);
165 NestedThrowable.Util.print(nested, writer);
166 }
167
168 public void printStackTrace()
169 {
170 printStackTrace(System.err);
171 }
172 }