1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12 * language governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16 * Sun designates this particular file as subject to the "Classpath" exception
17 * as provided by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the License
19 * Header, with the fields enclosed by brackets [] replaced by your own
20 * identifying information: "Portions Copyrighted [year]
21 * [name of copyright owner]"
22 *
23 * Contributor(s):
24 *
25 * If you wish your version of this file to be governed by only the CDDL or
26 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27 * elects to include this software in this distribution under the [CDDL or GPL
28 * Version 2] license." If you don't indicate a single choice of license, a
29 * recipient has the option to distribute your version of this file under
30 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31 * its licensees as provided above. However, if you add GPL Version 2 code
32 * and therefore, elected the GPL Version 2 license, then the option applies
33 * only if the new code is made subject to such option by the copyright
34 * holder.
35 */
36 package javax.ejb;
37
38 /**
39 * The EJBException exception is thrown by an enterprise Bean instance to
40 * its container to report that the invoked business method or callback method
41 * could not be completed because of an unexpected error (e.g. the instance
42 * failed to open a database connection).
43 */
44 public class EJBException extends java.lang.RuntimeException {
45 /**
46 * @serial
47 */
48 private Exception causeException = null;
49
50 /**
51 * Constructs an EJBException with no detail message.
52 */
53 public EJBException() {
54 }
55
56 /**
57 * Constructs an EJBException with the specified
58 * detailed message.
59 */
60 public EJBException(String message) {
61 super(message);
62 }
63
64 /**
65 * Constructs an EJBException that embeds the originally thrown exception.
66 */
67 public EJBException(Exception ex) {
68 super();
69 causeException = ex;
70 }
71
72 /**
73 * Constructs an EJBException that embeds the originally thrown exception
74 * with the specified detail message.
75 */
76 public EJBException(String message, Exception ex) {
77 super(message);
78 causeException = ex;
79 }
80
81
82 /**
83 * Obtain the exception that caused the EJBException being thrown.
84 */
85 public Exception getCausedByException() {
86 return causeException;
87 }
88
89 /**
90 * Returns the detail message, including the message from the nested
91 * exception if there is one.
92 */
93 public String getMessage() {
94 String msg = super.getMessage();
95 if (causeException == null)
96 return msg;
97 else if ( msg == null ) {
98 return "nested exception is: " + causeException.toString();
99 }
100 else {
101 return msg + "; nested exception is: " + causeException.toString();
102 }
103 }
104
105 /**
106 * Prints the composite message and the embedded stack trace to
107 * the specified stream <code>ps</code>.
108 * @param ps the print stream
109 */
110 public void printStackTrace(java.io.PrintStream ps)
111 {
112 if (causeException == null) {
113 super.printStackTrace(ps);
114 } else {
115 synchronized(ps) {
116 ps.println(this);
117 // Print the cause exception first, so that the output
118 // appears in stack order (i.e. innermost exception first)
119 causeException.printStackTrace(ps);
120 super.printStackTrace(ps);
121 }
122 }
123 }
124
125 /**
126 * Prints the composite message to <code>System.err</code>.
127 */
128 public void printStackTrace()
129 {
130 printStackTrace(System.err);
131 }
132
133 /**
134 * Prints the composite message and the embedded stack trace to
135 * the specified print writer <code>pw</code>.
136 * @param pw the print writer
137 */
138 public void printStackTrace(java.io.PrintWriter pw)
139 {
140 if (causeException == null) {
141 super.printStackTrace(pw);
142 } else {
143 synchronized(pw) {
144 pw.println(this);
145 // Print the cause exception first, so that the output
146 // appears in stack order (i.e. innermost exception first)
147 causeException.printStackTrace(pw);
148 super.printStackTrace(pw);
149 }
150 }
151 }
152 }