001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.tm;
023:
024: import java.io.PrintStream;
025: import java.io.PrintWriter;
026:
027: import javax.transaction.xa.XAException;
028:
029: import org.jboss.util.NestedThrowable;
030:
031: /**
032: * Thrown to indicate a problem with a xaresource related operation.
033: *
034: * <p>
035: * Properly displays linked exception (ie. nested exception)
036: * when printing the stack trace.
037: *
038: * @version <tt>$Revision: 57208 $</tt>
039: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
040: */
041: public class JBossXAException extends XAException implements
042: NestedThrowable {
043: /** The serial version uid*/
044: private static final long serialVersionUID = 6614203184612359692L;
045:
046: /** The linked exception */
047: Throwable linked;
048:
049: /**
050: * Rethrow as an xa exception if it is not already
051: *
052: * @param message the message
053: * @param t the original exception
054: * @throws XAException the xa exception
055: */
056: public static void rethrowAsXAException(String message, Throwable t)
057: throws XAException {
058: if (t instanceof XAException)
059: throw (XAException) t;
060: else
061: throw new JBossXAException(message, t);
062: }
063:
064: /**
065: * Construct a <tt>JBossXAException</tt> with the specified detail
066: * message.
067: *
068: * @param msg Detail message.
069: */
070: public JBossXAException(final String msg) {
071: super (msg);
072: }
073:
074: /**
075: * Construct a <tt>JBossXAException</tt> with the specified detail
076: * message and error code.
077: *
078: * @param code Error code.
079: */
080: public JBossXAException(final int code) {
081: super (code);
082: }
083:
084: /**
085: * Construct a <tt>JBossXAException</tt> with the specified detail
086: * message and linked <tt>Exception</tt>.
087: *
088: * @param msg Detail message.
089: * @param linked Linked <tt>Exception</tt>.
090: */
091: public JBossXAException(final String msg, final Throwable linked) {
092: super (msg);
093: this .linked = linked;
094: }
095:
096: /**
097: * Construct a <tt>JBossXAException</tt> with the specified
098: * linked <tt>Exception</tt>.
099: *
100: * @param linked Linked <tt>Exception</tt>.
101: */
102: public JBossXAException(final Throwable linked) {
103: this (linked.getMessage(), linked);
104: }
105:
106: /**
107: * Return the nested <tt>Throwable</tt>.
108: *
109: * @return Nested <tt>Throwable</tt>.
110: */
111: public Throwable getNested() {
112: return linked;
113: }
114:
115: /**
116: * Return the nested <tt>Throwable</tt>.
117: *
118: * <p>For JDK 1.4 compatibility.
119: *
120: * @return Nested <tt>Throwable</tt>.
121: */
122: public Throwable getCause() {
123: return linked;
124: }
125:
126: /**
127: * Returns the composite throwable message.
128: *
129: * @return The composite throwable message.
130: */
131: public String getMessage() {
132: return NestedThrowable.Util.getMessage(super .getMessage(),
133: linked);
134: }
135:
136: /**
137: * Prints the composite message and the embedded stack trace to the
138: * specified print stream.
139: *
140: * @param stream Stream to print to.
141: */
142: public void printStackTrace(final PrintStream stream) {
143: if (linked == null || NestedThrowable.PARENT_TRACE_ENABLED)
144: super .printStackTrace(stream);
145: NestedThrowable.Util.print(linked, stream);
146: }
147:
148: /**
149: * Prints the composite message and the embedded stack trace to the
150: * specified print writer.
151: *
152: * @param writer Writer to print to.
153: */
154: public void printStackTrace(final PrintWriter writer) {
155: if (linked == null || NestedThrowable.PARENT_TRACE_ENABLED)
156: super .printStackTrace(writer);
157: NestedThrowable.Util.print(linked, writer);
158: }
159:
160: /**
161: * Prints the composite message and the embedded stack trace to
162: * <tt>System.err</tt>.
163: */
164: public void printStackTrace() {
165: printStackTrace(System.err);
166: }
167: }
|