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.mq;
023:
024: import java.io.PrintWriter;
025: import java.io.PrintStream;
026:
027: import javax.jms.JMSException;
028:
029: import org.jboss.util.NestedThrowable;
030: import org.jboss.util.NestedException;
031:
032: /**
033: * A common superclass for <tt>JMSException</tt> classes that can contain a
034: * nested <tt>Throwable</tt> detail object.
035: *
036: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
037: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
038: * @version <tt>$Revision: 57198 $</tt>
039: */
040: public class SpyJMSException extends JMSException implements
041: NestedThrowable {
042: /** The serialVersionUID */
043: static final long serialVersionUID = 5216406958161784593L;
044:
045: /** The nested throwable */
046: protected Throwable nested;
047:
048: /**
049: * Converts a throwable to a JMSException if it is not already
050: *
051: * @param message any message to add to a constructed JMSException
052: * @param t the throwable
053: * @throws JMSException always
054: */
055: public static void rethrowAsJMSException(String message, Throwable t)
056: throws JMSException {
057: throw getAsJMSException(message, t);
058: }
059:
060: /**
061: * Converts a throwable to a JMSException if it is not already
062: *
063: * @param message any message to add to a constructed JMSException
064: * @param t the throwable
065: * @return a JMSException
066: */
067: public static JMSException getAsJMSException(String message,
068: Throwable t) {
069: if (t instanceof JMSException)
070: return (JMSException) t;
071: else
072: return new SpyJMSException(message, t);
073: }
074:
075: /**
076: * Construct a <tt>SpyJMSException</tt> with the specified detail message.
077: *
078: * @param msg Detail message.
079: */
080: public SpyJMSException(final String msg) {
081: super (msg);
082: this .nested = null;
083: }
084:
085: /**
086: * Construct a <tt>SpyJMSException</tt> with the specified detail message
087: * and error code.
088: *
089: * @param msg Detail message.
090: * @param code Error code.
091: */
092: public SpyJMSException(final String msg, final String code) {
093: super (msg, code);
094: this .nested = null;
095: }
096:
097: /**
098: * Construct a <tt>SpyJMSException</tt> with the specified detail message
099: * and nested <tt>Throwable</tt>.
100: *
101: * @param msg Detail message.
102: * @param nested Nested <tt>Throwable</tt>.
103: */
104: public SpyJMSException(final String msg, final Throwable nested) {
105: super (msg);
106: this .nested = nested;
107: NestedThrowable.Util.checkNested(this , nested);
108: }
109:
110: /**
111: * Construct a <tt>SpyJMSException</tt> with the specified nested <tt>Throwable</tt>.
112: *
113: * @param nested Nested <tt>Throwable</tt>.
114: */
115: public SpyJMSException(final Throwable nested) {
116: this (nested.getMessage(), nested);
117: }
118:
119: public void setLinkedException(final Exception e) {
120: this .nested = e;
121: }
122:
123: public Exception getLinkedException() {
124: // jason: this is bad, but whatever... the jms folks should have had more
125: // insight
126: if (nested == null)
127: return this ;
128: if (nested instanceof Exception)
129: return (Exception) nested;
130: return new NestedException(nested);
131: }
132:
133: public Throwable getNested() {
134: return nested;
135: }
136:
137: public Throwable getCause() {
138: return nested;
139: }
140:
141: public String getMessage() {
142: return NestedThrowable.Util.getMessage(super .getMessage(),
143: nested);
144: }
145:
146: public void printStackTrace(final PrintStream stream) {
147: if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED)
148: super .printStackTrace(stream);
149: NestedThrowable.Util.print(nested, stream);
150: }
151:
152: public void printStackTrace(final PrintWriter writer) {
153: if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED)
154: super .printStackTrace(writer);
155: NestedThrowable.Util.print(nested, writer);
156: }
157:
158: public void printStackTrace() {
159: printStackTrace(System.err);
160: }
161: }
|