KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyJMSException


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 JavaDoc;
25 import java.io.PrintStream JavaDoc;
26
27 import javax.jms.JMSException JavaDoc;
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 JavaDoc implements NestedThrowable
41 {
42    /** The serialVersionUID */
43    static final long serialVersionUID = 5216406958161784593L;
44
45    /** The nested throwable */
46    protected Throwable JavaDoc 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 JavaDoc message, Throwable JavaDoc t) throws JMSException JavaDoc
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 JavaDoc getAsJMSException(String JavaDoc message, Throwable JavaDoc t)
68    {
69       if (t instanceof JMSException JavaDoc)
70          return (JMSException JavaDoc) 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 JavaDoc 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 JavaDoc msg, final String JavaDoc 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 JavaDoc msg, final Throwable JavaDoc 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 JavaDoc nested)
119    {
120       this(nested.getMessage(), nested);
121    }
122
123    public void setLinkedException(final Exception JavaDoc e)
124    {
125       this.nested = e;
126    }
127
128    public Exception JavaDoc 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 JavaDoc)
135          return (Exception JavaDoc) nested;
136       return new NestedException(nested);
137    }
138
139    public Throwable JavaDoc getNested()
140    {
141       return nested;
142    }
143
144    public Throwable JavaDoc getCause()
145    {
146       return nested;
147    }
148
149    public String JavaDoc getMessage()
150    {
151       return NestedThrowable.Util.getMessage(super.getMessage(), nested);
152    }
153
154    public void printStackTrace(final PrintStream JavaDoc 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 JavaDoc 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 }
Popular Tags