KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > Util


1 /*
2
3    Derby - Class org.apache.derby.impl.jdbc.Util
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.jdbc;
23
24 import org.apache.derby.iapi.error.StandardException;
25 import org.apache.derby.iapi.services.i18n.MessageService;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28 import org.apache.derby.iapi.services.io.StoredFormatIds;
29 import org.apache.derby.iapi.types.TypeId;
30
31 import org.apache.derby.iapi.error.ExceptionSeverity;
32
33 import org.apache.derby.iapi.reference.SQLState;
34 import org.apache.derby.iapi.reference.MessageId;
35 import org.apache.derby.iapi.reference.JDBC30Translation;
36 import org.apache.derby.iapi.reference.JDBC40Translation;
37
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Types JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.PrintStream JavaDoc;
42 import java.io.PrintWriter JavaDoc;
43
44 /**
45     This class understands the message protocol and looks up
46     SQLExceptions based on keys, so that the Local JDBC driver's
47     messages can be localized.
48
49     REMIND: May want to investigate putting some of this in the protocol
50     side, for the errors that any Cloudscape JDBC driver might return.
51
52     The ASSERT mechanism is a wrapper of the basic services,
53     to ensure that failed asserts at this level will behave
54     well in a JDBC environment.
55
56     @author ames
57 */

58 //In the past, this class was sent on the wire to the client and because it
59
//has the message protcol stuff and also the detailed stack trace as one
60
//of it's member variable, the client.jar files was really big. To get
61
//around this problem, now we have added EmbedSQLException which is
62
//just a java sql exception with the stack trace information variable
63
//transient so it doesn't get transported to the client side and thus
64
//reducing the size of client.jar The bug for this fix was 1850. The
65
//p4 number for it will have the details of all the files impacted and
66
//the actual changes made.
67
public abstract class Util {
68
69
70     private static SQLExceptionFactory exceptionFactory =
71                                     new SQLExceptionFactory ();
72
73     /*
74     ** Methods of Throwable
75     */

76
77     // class implementation
78

79     /**
80      * This looks up the message and sqlstate values and calls
81      * the SQLExceptionFactory method to generate
82      * the appropriate exception off of them.
83      */

84
85     private static SQLException JavaDoc newEmbedSQLException(String JavaDoc messageId,
86             Object JavaDoc[] args, SQLException JavaDoc next, int severity, Throwable JavaDoc t) {
87         String JavaDoc message = MessageService.getCompleteMessage
88                                         (messageId, args);
89         return exceptionFactory.getSQLException (
90                 message, messageId, next, severity, t, args);
91     }
92
93     public static SQLException JavaDoc newEmbedSQLException(String JavaDoc messageId,
94             Object JavaDoc[] args, int severity) {
95         return newEmbedSQLException(messageId, args, (SQLException JavaDoc) null, severity, (Throwable JavaDoc) null);
96     }
97
98     private static SQLException JavaDoc newEmbedSQLException(String JavaDoc messageId,
99             Object JavaDoc[] args, int severity, Throwable JavaDoc t) {
100         return newEmbedSQLException(messageId,args, (SQLException JavaDoc) null, severity, t);
101     }
102
103     private static SQLException JavaDoc newEmbedSQLException(
104             String JavaDoc messageId, int severity) {
105         return newEmbedSQLException(messageId, (Object JavaDoc[]) null, (SQLException JavaDoc) null, severity, (Throwable JavaDoc) null);
106     }
107
108     // class interface
109

110
111     /**
112         Mimic SanityManager.ASSERT in a JDBC-friendly way,
113         and providing system cleanup for JDBC failures.
114         We need the connection to do cleanup...
115
116         @exception SQLException the exception
117      */

118     public static void ASSERT(EmbedConnection conn, boolean mustBeTrue, String JavaDoc msg) throws SQLException JavaDoc {
119         if (SanityManager.DEBUG) {
120             try {
121                 SanityManager.ASSERT(mustBeTrue, msg);
122             } catch (Throwable JavaDoc t) {
123                 SQLException JavaDoc se = conn.handleException(t);
124                 // get around typing constraints.
125
// it must be a Util, we wrapped it.
126
SanityManager.ASSERT(se instanceof EmbedSQLException);
127                 throw (EmbedSQLException)se;
128             }
129         }
130     }
131
132     /**
133         Mimic SanityManager.THROWASSERT in a JDBC-friendly way,
134         and providing system cleanup for JDBC failures.
135         We need the connection to do cleanup...
136      */

137     static void THROWASSERT(EmbedConnection conn, String JavaDoc msg) throws SQLException JavaDoc {
138         if (SanityManager.DEBUG) {
139             try {
140                 SanityManager.THROWASSERT(msg);
141             } catch (Throwable JavaDoc t) {
142                 SQLException JavaDoc se = conn.handleException(t);
143                 // get around typing constraints.
144
// it must be a Util, we wrapped it.
145
SanityManager.ASSERT(se instanceof EmbedSQLException);
146                 throw (EmbedSQLException)se;
147             }
148         }
149     }
150
151     /*
152     ** There is at least one static method for each message id.
153     ** Its parameters are specific to its message.
154     ** These will throw SQLException when the message repository
155     ** cannot be located.
156     ** Note that these methods call the static method newEmbedSQLException,
157     ** they don't directly do a new Util.
158     */

159
160     /* 3 arguments */
161     static SQLException JavaDoc newException(String JavaDoc messageID, Object JavaDoc a1,
162             Object JavaDoc a2, Object JavaDoc a3) {
163         return newEmbedSQLException(messageID, new Object JavaDoc[] {a1, a2, a3},
164                 StandardException.getSeverityFromIdentifier(messageID));
165     }
166
167
168     public static SQLException JavaDoc generateCsSQLException(String JavaDoc error) {
169         return newEmbedSQLException(error,
170                 StandardException.getSeverityFromIdentifier(error));
171     }
172
173     public static SQLException JavaDoc generateCsSQLException(String JavaDoc error, Object JavaDoc arg1) {
174         return newEmbedSQLException(error,
175             new Object JavaDoc[] {arg1},
176                 StandardException.getSeverityFromIdentifier(error));
177     }
178
179     public static SQLException JavaDoc generateCsSQLException(
180                              String JavaDoc error, Object JavaDoc arg1, Object JavaDoc arg2){
181         return newEmbedSQLException(error,
182             new Object JavaDoc[] {arg1, arg2},
183                 StandardException.getSeverityFromIdentifier(error));
184     }
185
186     public static SQLException JavaDoc generateCsSQLException(
187         String JavaDoc error, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3) {
188
189         return newEmbedSQLException(error,
190             new Object JavaDoc[] {arg1, arg2, arg3},
191                 StandardException.getSeverityFromIdentifier(error));
192     }
193
194
195     static SQLException JavaDoc generateCsSQLException(
196                     String JavaDoc error, Object JavaDoc arg1, Throwable JavaDoc t) {
197         return newEmbedSQLException(error,
198             new Object JavaDoc[] {arg1},
199                 StandardException.getSeverityFromIdentifier(error), t);
200     }
201
202     public static SQLException JavaDoc generateCsSQLException(StandardException se) {
203         return exceptionFactory.getSQLException(
204                 se.getMessage(), se.getMessageId(), (SQLException JavaDoc) null,
205                 se.getSeverity(), se, se.getArguments());
206     }
207
208     public static SQLException JavaDoc noCurrentConnection() {
209         return newEmbedSQLException(SQLState.NO_CURRENT_CONNECTION,
210                 StandardException.getSeverityFromIdentifier(SQLState.NO_CURRENT_CONNECTION));
211     }
212
213     public static SQLException JavaDoc javaException(Throwable JavaDoc t) {
214         String JavaDoc name, msg;
215
216         msg = t.getMessage();
217         if (msg == null) msg = "";
218         name = t.getClass().getName();
219         return newEmbedSQLException(SQLState.JAVA_EXCEPTION,
220             new Object JavaDoc[] {name, msg}, ExceptionSeverity.NO_APPLICABLE_SEVERITY, t);
221     }
222
223
224     public static SQLException JavaDoc notImplemented() {
225
226         return notImplemented( MessageService.getTextMessage(MessageId.CONN_NO_DETAILS) );
227     }
228
229     public static SQLException JavaDoc notImplemented(String JavaDoc feature) {
230
231         return newEmbedSQLException(SQLState.NOT_IMPLEMENTED,
232             new Object JavaDoc[] {feature},
233                 StandardException.getSeverityFromIdentifier(SQLState.NOT_IMPLEMENTED));
234     }
235
236     static SQLException JavaDoc setStreamFailure(IOException JavaDoc e) {
237         String JavaDoc msg;
238
239         msg = e.getMessage();
240         if (msg == null)
241             msg = e.getClass().getName();
242         return newEmbedSQLException(SQLState.SET_STREAM_FAILURE,
243             new Object JavaDoc[] {msg},
244                 StandardException.getSeverityFromIdentifier(SQLState.SET_STREAM_FAILURE));
245     }
246
247     static SQLException JavaDoc typeMisMatch(int targetSQLType) {
248         return newEmbedSQLException(SQLState.TYPE_MISMATCH,
249             new Object JavaDoc[] {typeName(targetSQLType)},
250                 StandardException.getSeverityFromIdentifier(SQLState.TYPE_MISMATCH));
251     }
252
253     /**
254      * this method is called to replace the exception factory to be
255      * used to generate the SQLException or the subclass
256      */

257
258     public static void setExceptionFactory (SQLExceptionFactory factory) {
259         exceptionFactory = factory;
260     }
261
262     /**
263      * Get the exception factory specific to the version of JDBC which
264      * we are running.
265      */

266     public static SQLExceptionFactory getExceptionFactory() { return exceptionFactory; }
267
268     public static String JavaDoc typeName(int jdbcType) {
269         switch (jdbcType) {
270             case Types.ARRAY: return TypeId.ARRAY_NAME;
271             case Types.BIT : return TypeId.BIT_NAME;
272             case JDBC30Translation.SQL_TYPES_BOOLEAN : return TypeId.BOOLEAN_NAME;
273             case JDBC30Translation.DATALINK: return TypeId.DATALINK_NAME;
274             case Types.TINYINT : return TypeId.TINYINT_NAME;
275             case Types.SMALLINT : return TypeId.SMALLINT_NAME;
276             case Types.INTEGER : return TypeId.INTEGER_NAME;
277             case Types.BIGINT : return TypeId.LONGINT_NAME;
278
279             case Types.FLOAT : return TypeId.FLOAT_NAME;
280             case Types.REAL : return TypeId.REAL_NAME;
281             case Types.DOUBLE : return TypeId.DOUBLE_NAME;
282
283             case Types.NUMERIC : return TypeId.NUMERIC_NAME;
284             case Types.DECIMAL : return TypeId.DECIMAL_NAME;
285
286             case Types.CHAR : return TypeId.CHAR_NAME;
287             case JDBC40Translation.NCHAR:
288                 return TypeId.NATIONAL_CHAR_NAME;
289             case Types.VARCHAR : return TypeId.VARCHAR_NAME;
290             case JDBC40Translation.NVARCHAR:
291                 return TypeId.NATIONAL_VARCHAR_NAME;
292             case Types.LONGVARCHAR : return "LONGVARCHAR";
293             case JDBC40Translation.LONGNVARCHAR:
294                 return TypeId.NATIONAL_LONGVARCHAR_NAME;
295             case Types.CLOB : return TypeId.CLOB_NAME;
296             case JDBC40Translation.NCLOB: return TypeId.NCLOB_NAME;
297
298             case Types.DATE : return TypeId.DATE_NAME;
299             case Types.TIME : return TypeId.TIME_NAME;
300             case Types.TIMESTAMP : return TypeId.TIMESTAMP_NAME;
301
302             case Types.BINARY : return TypeId.BINARY_NAME;
303             case Types.VARBINARY : return TypeId.VARBINARY_NAME;
304             case Types.LONGVARBINARY : return TypeId.LONGVARBINARY_NAME;
305             case Types.BLOB : return TypeId.BLOB_NAME;
306
307             case Types.OTHER : return "OTHER";
308             case Types.JAVA_OBJECT : return "Types.JAVA_OBJECT";
309             case Types.REF : return TypeId.REF_NAME;
310             case JDBC40Translation.ROWID: return TypeId.ROWID_NAME;
311             case Types.STRUCT: return TypeId.STRUCT_NAME;
312             case StoredFormatIds.XML_TYPE_ID : return TypeId.XML_NAME;
313             case JDBC40Translation.SQLXML: return TypeId.SQLXML_NAME;
314             default : return String.valueOf(jdbcType);
315         }
316     }
317 }
318
Popular Tags