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.resource;
023:
024: import java.io.PrintWriter;
025: import java.io.PrintStream;
026: import java.lang.reflect.UndeclaredThrowableException;
027:
028: import javax.resource.ResourceException;
029:
030: import org.jboss.util.NestedThrowable;
031:
032: /**
033: * Thrown to indicate a problem with a resource related operation.
034: *
035: * <p>
036: * Properly displays linked exception (ie. nested exception)
037: * when printing the stack trace.
038: *
039: * @version <tt>$Revision: 57189 $</tt>
040: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
041: */
042: public class JBossResourceException extends ResourceException implements
043: NestedThrowable {
044: /** The servial version uid*/
045: private static final long serialVersionUID = 6614203184612359692L;
046:
047: /**
048: * Rethrow as a resource exception if it is not already
049: *
050: * @param message the message
051: * @param t the original exception
052: * @throws ResourceException the resource exception
053: */
054: public static void rethrowAsResourceException(String message,
055: Throwable t) throws ResourceException {
056: if (t instanceof ResourceException)
057: throw (ResourceException) t;
058: else
059: throw new JBossResourceException(message, t);
060: }
061:
062: /**
063: * Construct a <tt>JBossResourceException</tt> with the specified detail
064: * message.
065: *
066: * @param msg Detail message.
067: */
068: public JBossResourceException(final String msg) {
069: super (msg);
070: }
071:
072: /**
073: * Construct a <tt>JBossResourceException</tt> with the specified detail
074: * message and error code.
075: *
076: * @param msg Detail message.
077: * @param code Error code.
078: */
079: public JBossResourceException(final String msg, final String code) {
080: super (msg, code);
081: }
082:
083: /**
084: * Construct a <tt>JBossResourceException</tt> with the specified detail
085: * message, error code and linked <tt>Exception</tt>.
086: *
087: * @param msg Detail message.
088: * @param code Error code.
089: * @param linked Linked <tt>Exception</tt>.
090: */
091: public JBossResourceException(final String msg, final String code,
092: final Throwable linked) {
093: super (msg, code);
094: setLinkedException(process(linked));
095: }
096:
097: /**
098: * Construct a <tt>JBossResourceException</tt> with the specified detail
099: * message and linked <tt>Exception</tt>.
100: *
101: * @param msg Detail message.
102: * @param linked Linked <tt>Exception</tt>.
103: */
104: public JBossResourceException(final String msg,
105: final Throwable linked) {
106: super (msg);
107: setLinkedException(process(linked));
108: }
109:
110: /**
111: * Construct a <tt>JBossResourceException</tt> with the specified
112: * linked <tt>Exception</tt>.
113: *
114: * @param linked Linked <tt>Exception</tt>.
115: */
116: public JBossResourceException(final Throwable linked) {
117: this (linked.getMessage(), linked);
118: }
119:
120: /**
121: * Return the nested <tt>Throwable</tt>.
122: *
123: * @return Nested <tt>Throwable</tt>.
124: */
125: public Throwable getNested() {
126: return getLinkedException();
127: }
128:
129: /**
130: * Return the nested <tt>Throwable</tt>.
131: *
132: * <p>For JDK 1.4 compatibility.
133: *
134: * @return Nested <tt>Throwable</tt>.
135: */
136: public Throwable getCause() {
137: return getLinkedException();
138: }
139:
140: /**
141: * Returns the composite throwable message.
142: *
143: * @return The composite throwable message.
144: */
145: public String getMessage() {
146: return NestedThrowable.Util.getMessage(super .getMessage(),
147: getLinkedException());
148: }
149:
150: /**
151: * Prints the composite message and the embedded stack trace to the
152: * specified print stream.
153: *
154: * @param stream Stream to print to.
155: */
156: public void printStackTrace(final PrintStream stream) {
157: Exception linked = getLinkedException();
158: if (linked == null || NestedThrowable.PARENT_TRACE_ENABLED) {
159: super .printStackTrace(stream);
160: }
161: NestedThrowable.Util.print(linked, stream);
162: }
163:
164: /**
165: * Prints the composite message and the embedded stack trace to the
166: * specified print writer.
167: *
168: * @param writer Writer to print to.
169: */
170: public void printStackTrace(final PrintWriter writer) {
171: Exception linked = getLinkedException();
172: if (linked == null || NestedThrowable.PARENT_TRACE_ENABLED) {
173: super .printStackTrace(writer);
174: }
175: NestedThrowable.Util.print(linked, writer);
176: }
177:
178: /**
179: * Prints the composite message and the embedded stack trace to
180: * <tt>System.err</tt>.
181: */
182: public void printStackTrace() {
183: printStackTrace(System.err);
184: }
185:
186: private Exception process(Throwable t) {
187: if (t instanceof Exception) {
188: return (Exception) t;
189: } // end of if ()
190: return new UndeclaredThrowableException(t);
191: }
192: }
|