KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > JBossXAException


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.tm;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import javax.transaction.xa.XAException JavaDoc;
27
28 import org.jboss.util.NestedThrowable;
29
30 /**
31  * Thrown to indicate a problem with a xaresource related operation.
32  *
33  * <p>
34  * Properly displays linked exception (ie. nested exception)
35  * when printing the stack trace.
36  *
37  * @version <tt>$Revision: 37459 $</tt>
38  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
39  */

40 public class JBossXAException
41    extends XAException JavaDoc
42    implements NestedThrowable
43 {
44    /** The servial version uid*/
45    private static final long serialVersionUID = 6614203184612359692L;
46
47    /** The linked exception */
48    Throwable JavaDoc linked;
49    
50    /**
51     * Rethrow as an xa exception if it is not already
52     *
53     * @param message the message
54     * @param t the original exception
55     * @throws XAException the xa exception
56     */

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

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

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

94    public JBossXAException(final String JavaDoc msg, final Throwable JavaDoc linked)
95    {
96       super(msg);
97       this.linked = linked;
98    }
99
100    /**
101     * Construct a <tt>JBossXAException</tt> with the specified
102     * linked <tt>Exception</tt>.
103     *
104     * @param linked Linked <tt>Exception</tt>.
105     */

106    public JBossXAException(final Throwable JavaDoc linked)
107    {
108       this(linked.getMessage(), linked);
109    }
110
111    /**
112     * Return the nested <tt>Throwable</tt>.
113     *
114     * @return Nested <tt>Throwable</tt>.
115     */

116    public Throwable JavaDoc getNested()
117    {
118       return linked;
119    }
120
121    /**
122     * Return the nested <tt>Throwable</tt>.
123     *
124     * <p>For JDK 1.4 compatibility.
125     *
126     * @return Nested <tt>Throwable</tt>.
127     */

128    public Throwable JavaDoc getCause()
129    {
130       return linked;
131    }
132
133    /**
134     * Returns the composite throwable message.
135     *
136     * @return The composite throwable message.
137     */

138    public String JavaDoc getMessage()
139    {
140       return NestedThrowable.Util.getMessage(super.getMessage(), linked);
141    }
142
143    /**
144     * Prints the composite message and the embedded stack trace to the
145     * specified print stream.
146     *
147     * @param stream Stream to print to.
148     */

149    public void printStackTrace(final PrintStream JavaDoc stream)
150    {
151       if (linked == null || NestedThrowable.PARENT_TRACE_ENABLED)
152          super.printStackTrace(stream);
153       NestedThrowable.Util.print(linked, stream);
154    }
155
156    /**
157     * Prints the composite message and the embedded stack trace to the
158     * specified print writer.
159     *
160     * @param writer Writer to print to.
161     */

162    public void printStackTrace(final PrintWriter JavaDoc writer)
163    {
164       if (linked == null || NestedThrowable.PARENT_TRACE_ENABLED)
165          super.printStackTrace(writer);
166       NestedThrowable.Util.print(linked, writer);
167    }
168
169    /**
170     * Prints the composite message and the embedded stack trace to
171     * <tt>System.err</tt>.
172     */

173    public void printStackTrace()
174    {
175       printStackTrace(System.err);
176    }
177 }
178
Popular Tags