KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringWriter JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import oracle.toplink.essentials.queryframework.*;
27 import oracle.toplink.essentials.internal.databaseaccess.*;
28 import oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator;
29 import oracle.toplink.essentials.sessions.Record;
30 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
31 import oracle.toplink.essentials.internal.sessions.AbstractSession;
32
33 /**
34  * <P><B>Purpose</B>:
35  * Wrapper for any database exception that occurred through TopLink.
36  */

37 public class DatabaseException extends TopLinkException {
38     protected SQLException JavaDoc exception;
39     protected transient Call call;
40     protected transient DatabaseQuery query;
41     protected transient AbstractRecord queryArguments;
42     protected transient Accessor accessor;
43     public static final int SQL_EXCEPTION = 4002;
44     public static final int CONFIGURATION_ERROR_CLASS_NOT_FOUND = 4003;
45     public static final int DATABASE_ACCESSOR_NOT_CONNECTED = 4005;
46     public static final int ERROR_READING_BLOB_DATA = 4006;
47     public static final int COULD_NOT_CONVERT_OBJECT_TYPE = 4007;
48     public static final int LOGOUT_WHILE_TRANSACTION_IN_PROGRESS = 4008;
49     public static final int SEQUENCE_TABLE_INFORMATION_NOT_COMPLETE = 4009;
50     public static final int ERROR_PREALLOCATING_SEQUENCE_NUMBERS = 4011;
51     public static final int CANNOT_REGISTER_SYNCHRONIZATIONLISTENER_FOR_UNITOFWORK = 4014;
52     public static final int SYNCHRONIZED_UNITOFWORK_DOES_NOT_SUPPORT_COMMITANDRESUME = 4015;
53     public static final int CONFIGURATION_ERROR_NEW_INSTANCE_INSTANTIATION_EXCEPTION = 4016;
54     public static final int CONFIGURATION_ERROR_NEW_INSTANCE_ILLEGAL_ACCESS_EXCEPTION = 4017;
55     public static final int TRANSACTION_MANAGER_NOT_SET_FOR_JTS_DRIVER = 4018;
56     public static final int ERROR_RETRIEVE_DB_METADATA_THROUGH_JDBC_CONNECTION = 4019;
57
58     /**
59      * INTERNAL:
60      * TopLink exceptions should only be thrown by the TopLink code.
61      */

62     protected DatabaseException(String JavaDoc message) {
63         super(message);
64     }
65
66     /**
67      * INTERNAL:
68      * TopLink exceptions should only be thrown by the TopLink code.
69      */

70     protected DatabaseException(SQLException JavaDoc exception) {
71         super(exception.toString(), exception);
72     }
73
74     public static DatabaseException cannotRegisterSynchronizatonListenerForUnitOfWork(Exception JavaDoc e) {
75         Object JavaDoc[] args = { e };
76
77         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, CANNOT_REGISTER_SYNCHRONIZATIONLISTENER_FOR_UNITOFWORK, args));
78         databaseException.setErrorCode(CANNOT_REGISTER_SYNCHRONIZATIONLISTENER_FOR_UNITOFWORK);
79         databaseException.setInternalException(e);
80         return databaseException;
81     }
82
83     public static DatabaseException configurationErrorClassNotFound(String JavaDoc className) {
84         Object JavaDoc[] args = { className };
85
86         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, CONFIGURATION_ERROR_CLASS_NOT_FOUND, args));
87         databaseException.setErrorCode(CONFIGURATION_ERROR_CLASS_NOT_FOUND);
88         return databaseException;
89     }
90
91     public static DatabaseException configurationErrorNewInstanceIllegalAccessException(IllegalAccessException JavaDoc exception, Class JavaDoc javaClass) {
92         Object JavaDoc[] args = { javaClass };
93
94         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, CONFIGURATION_ERROR_NEW_INSTANCE_ILLEGAL_ACCESS_EXCEPTION, args));
95         databaseException.setErrorCode(CONFIGURATION_ERROR_NEW_INSTANCE_ILLEGAL_ACCESS_EXCEPTION);
96         databaseException.setInternalException(exception);
97         return databaseException;
98     }
99
100     public static DatabaseException configurationErrorNewInstanceInstantiationException(InstantiationException JavaDoc exception, Class JavaDoc javaClass) {
101         Object JavaDoc[] args = { javaClass };
102
103         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, CONFIGURATION_ERROR_NEW_INSTANCE_INSTANTIATION_EXCEPTION, args));
104         databaseException.setErrorCode(CONFIGURATION_ERROR_NEW_INSTANCE_INSTANTIATION_EXCEPTION);
105         databaseException.setInternalException(exception);
106         return databaseException;
107     }
108
109     public static DatabaseException couldNotConvertObjectType(int type) {
110         Object JavaDoc[] args = { CR, new Integer JavaDoc(type) };
111
112         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, COULD_NOT_CONVERT_OBJECT_TYPE, args));
113         databaseException.setErrorCode(COULD_NOT_CONVERT_OBJECT_TYPE);
114         return databaseException;
115     }
116
117     public static DatabaseException databaseAccessorNotConnected() {
118         Object JavaDoc[] args = { };
119         String JavaDoc message = oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator.buildMessage(DatabaseException.class, DATABASE_ACCESSOR_NOT_CONNECTED, args);
120         DatabaseException databaseException = new DatabaseException(message);
121         databaseException.setErrorCode(DATABASE_ACCESSOR_NOT_CONNECTED);
122         return databaseException;
123     }
124
125     public static DatabaseException databaseAccessorNotConnected(DatabaseAccessor databaseAccessor) {
126         Object JavaDoc[] args = { };
127
128         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, DATABASE_ACCESSOR_NOT_CONNECTED, args));
129         databaseException.setErrorCode(DATABASE_ACCESSOR_NOT_CONNECTED);
130         databaseException.setAccessor(databaseAccessor);
131         return databaseException;
132     }
133
134     public static DatabaseException errorPreallocatingSequenceNumbers() {
135         Object JavaDoc[] args = { };
136
137         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, ERROR_PREALLOCATING_SEQUENCE_NUMBERS, args));
138         databaseException.setErrorCode(ERROR_PREALLOCATING_SEQUENCE_NUMBERS);
139         return databaseException;
140     }
141
142     public static DatabaseException errorReadingBlobData() {
143         Object JavaDoc[] args = { };
144
145         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, ERROR_READING_BLOB_DATA, args));
146         databaseException.setErrorCode(ERROR_READING_BLOB_DATA);
147         return databaseException;
148     }
149
150     /**
151      * PUBLIC:
152      * Return the accessor.
153      */

154     public Accessor getAccessor() {
155         return accessor;
156     }
157
158     /**
159      * PUBLIC:
160      * This is the database error number.
161      * Since it is possible to have no internal exception the errorCode will be zero in this case.
162      */

163     public int getDatabaseErrorCode() {
164         if (getInternalException() == null) {
165             return super.getErrorCode();
166         }
167         return ((SQLException JavaDoc)getInternalException()).getErrorCode();
168     }
169
170     /**
171      * PUBLIC:
172      * This is the database error message.
173      */

174     public String JavaDoc getMessage() {
175         if (getInternalException() == null) {
176             return super.getMessage();
177         } else {
178             StringWriter JavaDoc writer = new StringWriter JavaDoc();
179             writer.write(super.getMessage());
180             writer.write(getIndentationString());
181             writer.write(ExceptionMessageGenerator.getHeader("ErrorCodeHeader"));
182             if (getInternalException() instanceof SQLException JavaDoc) {
183                 writer.write(Integer.toString(((SQLException JavaDoc)getInternalException()).getErrorCode()));
184             } else {
185                 writer.write("000");
186             }
187             if (getCall() != null) {
188                 writer.write(cr());
189                 writer.write(getIndentationString());
190                 writer.write("Call:");
191                 if (getAccessor() != null) {
192                     writer.write(getCall().getLogString(getAccessor()));
193                 } else {
194                     writer.write(getCall().toString());
195                 }
196             }
197             if (getQuery() != null) {
198                 writer.write(cr());
199                 writer.write(getIndentationString());
200                 writer.write("Query:");
201                 try {
202                     writer.write(getQuery().toString());
203                 } catch (RuntimeException JavaDoc badTooString) {
204                 }
205             }
206             return writer.toString();
207         }
208     }
209
210     /**
211      * PUBLIC:
212      * This method returns the databaseQuery.
213      * DatabaseQuery is a visible class to the TopLink user.
214      * Users create an appropriate query by creating an instance
215      * of a concrete subclasses of DatabaseQuery.
216      */

217     public DatabaseQuery getQuery() {
218         return query;
219     }
220
221     /**
222      * PUBLIC:
223      * Return the call that caused the exception.
224      */

225     public Call getCall() {
226         return call;
227     }
228
229     /**
230      * INTERNAL:
231      * Set the call that caused the exception.
232      */

233     public void setCall(Call call) {
234         this.call = call;
235     }
236
237     /**
238      * PUBLIC:
239      * Return the query argements used in the original query when exception is thrown
240      */

241     public Record getQueryArgumentsRecord() {
242         return queryArguments;
243     }
244
245     public static DatabaseException logoutWhileTransactionInProgress() {
246         Object JavaDoc[] args = { };
247
248         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, LOGOUT_WHILE_TRANSACTION_IN_PROGRESS, args));
249         databaseException.setErrorCode(LOGOUT_WHILE_TRANSACTION_IN_PROGRESS);
250         return databaseException;
251     }
252
253     public static DatabaseException sequenceTableInformationNotComplete() {
254         Object JavaDoc[] args = { };
255
256         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, SEQUENCE_TABLE_INFORMATION_NOT_COMPLETE, args));
257         databaseException.setErrorCode(SEQUENCE_TABLE_INFORMATION_NOT_COMPLETE);
258         return databaseException;
259     }
260
261     /**
262      * INTERNAL:
263      * Set the Accessor.
264      */

265     public void setAccessor(Accessor accessor) {
266         this.accessor = accessor;
267     }
268
269     /**
270      * PUBLIC:
271      * This method set the databaseQuery.
272      * DatabaseQuery is a visible class to the TopLink user.
273      * Users create an appropriate query by creating an instance
274      * of a concrete subclasses of DatabaseQuery.
275      */

276     public void setQuery(DatabaseQuery query) {
277         this.query = query;
278     }
279
280     /**
281      * PUBLIC:
282      * Set the query argements used in the original query when exception is thrown
283      */

284     public void setQueryArguments(AbstractRecord queryArguments) {
285         this.queryArguments = queryArguments;
286     }
287
288     public static DatabaseException sqlException(SQLException JavaDoc exception) {
289         DatabaseException databaseException = new DatabaseException(exception);
290         databaseException.setErrorCode(SQL_EXCEPTION);
291         return databaseException;
292     }
293
294     public static DatabaseException sqlException(SQLException JavaDoc exception, Accessor accessor, AbstractSession session) {
295         DatabaseException databaseException = new DatabaseException(exception);
296         databaseException.setErrorCode(SQL_EXCEPTION);
297         databaseException.setAccessor(accessor);
298         databaseException.setSession(session);
299         return databaseException;
300     }
301
302     public static DatabaseException sqlException(SQLException JavaDoc exception, Call call, Accessor accessor, AbstractSession session) {
303         DatabaseException databaseException = new DatabaseException(exception);
304         databaseException.setErrorCode(SQL_EXCEPTION);
305         databaseException.setAccessor(accessor);
306         databaseException.setCall(call);
307         return databaseException;
308     }
309
310     public static DatabaseException synchronizedUnitOfWorkDoesNotSupportCommitAndResume() {
311         Object JavaDoc[] args = { };
312
313         String JavaDoc message = oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator.buildMessage(DatabaseException.class, SYNCHRONIZED_UNITOFWORK_DOES_NOT_SUPPORT_COMMITANDRESUME, args);
314         DatabaseException databaseException = new DatabaseException(message);
315         databaseException.setErrorCode(SYNCHRONIZED_UNITOFWORK_DOES_NOT_SUPPORT_COMMITANDRESUME);
316         return databaseException;
317     }
318
319     public static DatabaseException transactionManagerNotSetForJTSDriver() {
320         Object JavaDoc[] args = { };
321
322         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, TRANSACTION_MANAGER_NOT_SET_FOR_JTS_DRIVER, args));
323         databaseException.setErrorCode(TRANSACTION_MANAGER_NOT_SET_FOR_JTS_DRIVER);
324         return databaseException;
325     }
326     
327     public static DatabaseException errorRetrieveDbMetadataThroughJDBCConnection() {
328         Object JavaDoc[] args = { };
329
330         DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, ERROR_RETRIEVE_DB_METADATA_THROUGH_JDBC_CONNECTION, args));
331         databaseException.setErrorCode(ERROR_RETRIEVE_DB_METADATA_THROUGH_JDBC_CONNECTION);
332         return databaseException;
333     }
334 }
335
Popular Tags