KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > AbstractNestedThrowable


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.util;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.PrintStream JavaDoc;
26
27 /**
28  * A common superclass for <tt>Throwable</tt> classes that can contain
29  * a nested <tt>Throwable</tt> detail object.
30  *
31  * @version <tt>$Revision: 1958 $</tt>
32  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
33  */

34 public abstract class AbstractNestedThrowable
35    extends Throwable JavaDoc
36    implements NestedThrowable
37 {
38    /** The nested throwable */
39    protected final Throwable JavaDoc nested;
40
41    /**
42     * Construct a <tt>AbstractNestedThrowable</tt> with the specified
43     * detail message.
44     *
45     * @param msg Detail message.
46     */

47    public AbstractNestedThrowable(final String JavaDoc msg) {
48       super(msg);
49       this.nested = null;
50    }
51
52    /**
53     * Construct a <tt>AbstractNestedThrowable</tt> with the specified detail
54     * message and nested <tt>Throwable</tt>.
55     *
56     * @param msg Detail message.
57     * @param nested Nested <tt>Throwable</tt>.
58     */

59    public AbstractNestedThrowable(final String JavaDoc msg, final Throwable JavaDoc nested) {
60       super(msg);
61       this.nested = nested;
62       NestedThrowable.Util.checkNested(this, nested);
63    }
64
65    /**
66     * Construct a <tt>AbstractNestedThrowable</tt> with the specified
67     * nested <tt>Throwable</tt>.
68     *
69     * @param nested Nested <tt>Throwable</tt>.
70     */

71    public AbstractNestedThrowable(final Throwable JavaDoc nested) {
72       this(nested.getMessage(), nested);
73    }
74
75    /**
76     * Construct a <tt>AbstractNestedThrowable</tt> with no detail.
77     */

78    public AbstractNestedThrowable() {
79       super();
80       this.nested = null;
81    }
82
83    /**
84     * Return the nested <tt>Throwable</tt>.
85     *
86     * @return Nested <tt>Throwable</tt>.
87     */

88    public Throwable JavaDoc getNested() {
89       return nested;
90    }
91
92    /**
93     * Return the nested <tt>Throwable</tt>.
94     *
95     * <p>For JDK 1.4 compatibility.
96     *
97     * @return Nested <tt>Throwable</tt>.
98     */

99    public Throwable JavaDoc getCause() {
100       return nested;
101    }
102    
103    /**
104     * Returns the composite throwable message.
105     *
106     * @return The composite throwable message.
107     */

108    public String JavaDoc getMessage() {
109       return NestedThrowable.Util.getMessage(super.getMessage(), nested);
110    }
111
112    /**
113     * Prints the composite message and the embedded stack trace to the
114     * specified print stream.
115     *
116     * @param stream Stream to print to.
117     */

118    public void printStackTrace(final PrintStream JavaDoc stream) {
119       if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED) {
120          super.printStackTrace(stream);
121       }
122       NestedThrowable.Util.print(nested, stream);
123    }
124
125    /**
126     * Prints the composite message and the embedded stack trace to the
127     * specified print writer.
128     *
129     * @param writer Writer to print to.
130     */

131    public void printStackTrace(final PrintWriter JavaDoc writer) {
132       if (nested == null || NestedThrowable.PARENT_TRACE_ENABLED) {
133          super.printStackTrace(writer);
134       }
135       NestedThrowable.Util.print(nested, writer);
136    }
137
138    /**
139     * Prints the composite message and the embedded stack trace to
140     * <tt>System.err</tt>.
141     */

142    public void printStackTrace() {
143       printStackTrace(System.err);
144    }
145 }
146
Popular Tags