KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > exceptions > TopLinkException


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.exceptions;
23
24 import java.io.*;
25 import oracle.toplink.essentials.internal.helper.JavaPlatform;
26 import oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator;
27 import oracle.toplink.essentials.internal.sessions.AbstractSession;
28
29 /**
30  * <p>
31  * <b>Purpose</b>: Any exception raised by TopLink should be a subclass of this exception class.
32  */

33 public abstract class TopLinkException extends RuntimeException JavaDoc {
34     protected transient AbstractSession session;
35     protected Throwable JavaDoc internalException;
36     protected static Boolean JavaDoc shouldPrintInternalException = null;
37     protected String JavaDoc indentationString;
38     protected int errorCode;
39     protected static final String JavaDoc CR = System.getProperty("line.separator");
40     //Bug#3559280 Added to avoid logging an exception twice
41
protected boolean hasBeenLogged;
42
43     /**
44      * INTERNAL:
45      * Return a new exception.
46      */

47     public TopLinkException() {
48         this("");
49     }
50
51     /**
52      * INTERNAL:
53      * TopLink exception should only be thrown by TopLink.
54      */

55     public TopLinkException(String JavaDoc theMessage) {
56         super(theMessage);
57         this.indentationString = "";
58         hasBeenLogged = false;
59     }
60
61     /**
62      * INTERNAL:
63      * TopLink exception should only be thrown by TopLink.
64      */

65     public TopLinkException(String JavaDoc message, Throwable JavaDoc internalException) {
66         this(message);
67         setInternalException(internalException);
68     }
69
70     /**
71      * INTERNAL:
72      * Convenience method - return a platform-specific line-feed.
73      */

74     protected static String JavaDoc cr() {
75         return oracle.toplink.essentials.internal.helper.Helper.cr();
76     }
77
78     /**
79      * PUBLIC:
80      * Return the exception error code.
81      */

82     public int getErrorCode() {
83         return errorCode;
84     }
85
86     /**
87      * INTERNAL:
88      * Used to print things nicely in the testing tool.
89      */

90     public String JavaDoc getIndentationString() {
91         return indentationString;
92     }
93
94     /**
95      * PUBLIC:
96      * Return the internal native exception.
97      * TopLink frequently catches Java exceptions and wraps them in its own exception
98      * classes to provide more information.
99      * The internal exception can still be accessed if required.
100      */

101     public Throwable JavaDoc getInternalException() {
102         return internalException;
103     }
104
105     /**
106      * PUBLIC:
107      * Return the exception error message.
108      * TopLink error messages are multi-line so that detail descriptions of the exception are given.
109      */

110     public String JavaDoc getMessage() {
111         StringWriter writer = new StringWriter(100);
112
113         // Avoid printing internal exception error message twice.
114
if ((getInternalException() == null) || (!super.getMessage().equals(getInternalException().toString()))) {
115             writer.write(cr());
116             writer.write(getIndentationString());
117             writer.write(ExceptionMessageGenerator.getHeader("DescriptionHeader"));
118             writer.write(super.getMessage());
119         }
120
121         if (getInternalException() != null) {
122             writer.write(cr());
123             writer.write(getIndentationString());
124             writer.write(ExceptionMessageGenerator.getHeader("InternalExceptionHeader"));
125             writer.write(getInternalException().toString());
126
127             if ((getInternalException() instanceof java.lang.reflect.InvocationTargetException JavaDoc) && ((((java.lang.reflect.InvocationTargetException JavaDoc)getInternalException()).getTargetException()) != null)) {
128                 writer.write(cr());
129                 writer.write(getIndentationString());
130                 writer.write(ExceptionMessageGenerator.getHeader("TargetInvocationExceptionHeader"));
131                 writer.write(((java.lang.reflect.InvocationTargetException JavaDoc)getInternalException()).getTargetException().toString());
132             }
133         }
134
135         return writer.toString();
136     }
137
138     /**
139      * PUBLIC:
140      * Return the session.
141      */

142     public AbstractSession getSession() {
143         return session;
144     }
145
146     /**
147      * INTERNAL:
148      * Return if this exception has been logged to avoid being logged more than once.
149      */

150     public boolean hasBeenLogged() {
151         return hasBeenLogged;
152     }
153
154     /**
155      * PUBLIC:
156      * Print both the normal and internal stack traces.
157      */

158     public void printStackTrace() {
159         printStackTrace(System.err);
160     }
161
162     /**
163      * PUBLIC:
164      * Print both the normal and internal stack traces.
165      */

166     public void printStackTrace(PrintStream outStream) {
167         printStackTrace(new PrintWriter(outStream));
168     }
169
170     /**
171      * PUBLIC:
172      * Print both the normal and internal stack traces.
173      */

174     public void printStackTrace(PrintWriter writer) {
175         writer.write(ExceptionMessageGenerator.getHeader("LocalExceptionStackHeader"));
176         writer.write(cr());
177         super.printStackTrace(writer);
178
179         if ((getInternalException() != null) && shouldPrintInternalException()) {
180             writer.write(ExceptionMessageGenerator.getHeader("InternalExceptionStackHeader"));
181             writer.write(cr());
182             getInternalException().printStackTrace(writer);
183
184             if ((getInternalException() instanceof java.lang.reflect.InvocationTargetException JavaDoc) && ((((java.lang.reflect.InvocationTargetException JavaDoc)getInternalException()).getTargetException()) != null)) {
185                 writer.write(ExceptionMessageGenerator.getHeader("TargetInvocationExceptionStackHeader"));
186                 writer.write(cr());
187                 ((java.lang.reflect.InvocationTargetException JavaDoc)getInternalException()).getTargetException().printStackTrace(writer);
188             }
189         }
190         writer.flush();
191     }
192
193     /**
194      * INTERNAL:
195      */

196     public void setErrorCode(int errorCode) {
197         this.errorCode = errorCode;
198     }
199
200     /**
201      * INTERNAL:
202      * Set this flag to avoid logging an exception more than once.
203      */

204     public void setHasBeenLogged(boolean logged) {
205         this.hasBeenLogged = logged;
206     }
207
208     /**
209      * INTERNAL:
210      * Used to print things nicely in the testing tool.
211      */

212     public void setIndentationString(String JavaDoc indentationString) {
213         this.indentationString = indentationString;
214     }
215
216     /**
217      * INTERNAL:
218      * Used to specify the internal exception.
219      */

220     public void setInternalException(Throwable JavaDoc anException) {
221         internalException = anException;
222         JavaPlatform.setExceptionCause(this, anException);
223     }
224
225     /**
226      * INTERNAL:
227      */

228     public void setSession(AbstractSession session) {
229         this.session = session;
230     }
231
232     /**
233      * PUBLIC:
234      * Allows overiding of TopLink's exception chaining detection.
235      * @param booleam printException - If printException is true, the TopLink-stored
236      * Internal exception will be included in a stack traceor in the exception message of a TopLinkException.
237      * If printException is false, the TopLink-stored Internal Exception will not be included
238      * in the stack trace or the exception message of TopLinkExceptions
239      */

240     public static void setShouldPrintInternalException(boolean printException) {
241         shouldPrintInternalException = new Boolean JavaDoc(printException);
242     }
243
244     /**
245      * INTERNAL
246      * Check to see if the TopLink-stored internal exception should be printed in this
247      * a TopLinkException's stack trace. This method will check the static ShouldPrintInternalException
248      * variable and if it is not set, estimate based on the JDK version used.
249      */

250     public static boolean shouldPrintInternalException() {
251         if (shouldPrintInternalException == null) {
252             shouldPrintInternalException = new Boolean JavaDoc(JavaPlatform.shouldPrintInternalException());
253         }
254         return shouldPrintInternalException.booleanValue();
255     }
256
257     /**
258      * INTERNAL:
259      */

260     public String JavaDoc toString() {
261         return getIndentationString() + ExceptionMessageGenerator.getHeader("ExceptionHeader") + getErrorCode() + "] (" + oracle.toplink.essentials.sessions.DatabaseLogin.getVersion() + "): " + getClass().getName() + getMessage();
262     }
263 }
264
Popular Tags