KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26
27 import javax.transaction.xa.XAException JavaDoc;
28
29 import org.jboss.util.NestedException;
30 import org.jboss.util.NestedThrowable;
31
32 /**
33  * An XAException with a nested throwable
34  *
35  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
36  * @version <tt>$Revision: 45317 $</tt>
37  */

38 public class SpyXAException extends XAException JavaDoc implements NestedThrowable
39 {
40    /** The serialVersionUID */
41    static final long serialVersionUID = 7814140228056884098L;
42
43    /** The nested throwable */
44    protected Throwable JavaDoc nested;
45
46    /**
47     * Converts a throwable to an XAException if it is not already
48     *
49     * @param message any message to add to a constructed XAException
50     * @param t the throwable
51     * @return XAException never (always throws)
52     * @throws XAException always
53     */

54    public static XAException JavaDoc rethrowAsXAException(String JavaDoc message, Throwable JavaDoc t) throws XAException JavaDoc
55    {
56       throw getAsXAException(message, t);
57    }
58    
59    /**
60     * Converts a throwable to an XAException if it is not already
61     *
62     * @param message any message to add to a constructed XAException
63     * @param t the throwable
64     * @return an XAException
65     */

66    public static XAException JavaDoc getAsXAException(String JavaDoc message, Throwable JavaDoc t)
67    {
68       if (t instanceof XAException JavaDoc)
69          return (XAException JavaDoc) t;
70       else
71       {
72          SpyXAException e = new SpyXAException(message, t);
73          e.errorCode = XAException.XAER_RMERR;
74          return e;
75       }
76    }
77
78    /**
79     * Construct a <tt>SpyXAException</tt>
80     */

81    public SpyXAException()
82    {
83       super();
84       this.nested = null;
85    }
86
87    /**
88     * Construct a <tt>SpyXAException</tt> with the specified detail message.
89     *
90     * @param msg Detail message.
91     */

92    public SpyXAException(final String JavaDoc msg)
93    {
94       super(msg);
95       this.nested = null;
96    }
97
98    /**
99     * Construct a <tt>SpyXAException</tt> with the specified detail message
100     * and error code.
101     *
102     * @param code Error code.
103     */

104    public SpyXAException(final int code)
105    {
106       super(code);
107       this.nested = null;
108    }
109
110    /**
111     * Construct a <tt>SpyXAException</tt>
112     *
113     * @param throwable the nested throwable.
114     */

115    public SpyXAException(Throwable JavaDoc t)
116    {
117       super();
118       this.nested = t;
119    }
120
121    /**
122     * Construct a <tt>SpyXAException</tt> with the specified detail message.
123     *
124     * @param msg Detail message.
125     * @param throwable the nested throwable.
126     */

127    public SpyXAException(final String JavaDoc msg, Throwable JavaDoc t)
128    {
129       super(msg);
130       this.nested = t;
131    }
132
133    /**
134     * Construct a <tt>SpyXAException</tt> with the specified detail message
135     * and error code.
136     *
137     * @param code Error code.
138     * @param throwable the nested throwable.
139     */

140    public SpyXAException(final int code, Throwable JavaDoc t)
141    {
142       super(code);
143       this.nested = t;
144    }
145
146    public Throwable JavaDoc getNested()
147    {
148       return nested;
149    }
150
151    public Throwable JavaDoc getCause()
152    {
153       return nested;
154    }
155
156    public void setLinkedException(final Exception JavaDoc e)
157    {
158       this.nested = e;
159    }
160
161    public Exception JavaDoc getLinkedException()
162    {
163       //
164
// jason: this is bad, but whatever... the jms folks should have had more
165
// insight
166
//
167
if (nested == null)
168          return this;
169       if (nested instanceof Exception JavaDoc)
170       {
171          return (Exception JavaDoc) nested;
172       }
173       return new NestedException(nested);
174    }
175
176    public String JavaDoc getMessage()
177    {
178       return NestedThrowable.Util.getMessage(super.getMessage(), nested);
179    }
180
181    public void printStackTrace(final PrintStream JavaDoc stream)
182    {
183       if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED)
184       {
185          super.printStackTrace(stream);
186       }
187       NestedThrowable.Util.print(nested, stream);
188    }
189
190    public void printStackTrace(final PrintWriter JavaDoc writer)
191    {
192       if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED)
193       {
194          super.printStackTrace(writer);
195       }
196       NestedThrowable.Util.print(nested, writer);
197    }
198
199    public void printStackTrace()
200    {
201       printStackTrace(System.err);
202    }
203 }
Popular Tags