KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > message > Message


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.message;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.lang.reflect.InvocationTargetException JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.text.MessageFormat JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 import org.h2.jdbc.JdbcSQLException;
15 import org.h2.util.Resources;
16 import org.h2.util.StringUtils;
17
18 /**
19  * @author Thomas
20  */

21 public class Message {
22
23     private static final Properties JavaDoc MESSAGES = new Properties JavaDoc();
24
25     static {
26         // TODO multilanguage messages
27
// String language = Locale.getDefault().getLanguage();
28
try {
29             byte[] messages = Resources.get("/org/h2/res/messages.properties");
30             if(messages != null) {
31                 MESSAGES.load(new ByteArrayInputStream JavaDoc(messages));
32             }
33         } catch (IOException JavaDoc e) {
34             TraceSystem.traceThrowable(e);
35         }
36     }
37
38     /**
39      * Gets the SQL Exception object for a specific SQLState. Supported
40      * sqlstates are:
41      *
42      * @param sqlstate -
43      * the SQL State
44      * @param param -
45      * the parameter of the message
46      * @return the SQLException object
47      */

48     public static JdbcSQLException getSQLException(int sqlstate, String JavaDoc p1) {
49         return getSQLException(sqlstate, new String JavaDoc[] { p1 }, null);
50     }
51
52     public static String JavaDoc translate(String JavaDoc key, String JavaDoc[] param) {
53         String JavaDoc message = MESSAGES.getProperty(key);
54         if(message == null) {
55             message = "(Message " +key+ " not found)";
56         }
57         if (param != null) {
58             Object JavaDoc[] o = param;
59             message = MessageFormat.format(message, o);
60         }
61         return message;
62     }
63
64     public static JdbcSQLException getSQLException(int errorCode, String JavaDoc[] param, Throwable JavaDoc cause) {
65         String JavaDoc sqlstate = getState(errorCode);
66         String JavaDoc message = translate(sqlstate, param);
67         return new JdbcSQLException(message, sqlstate, errorCode, cause);
68     }
69
70     public static SQLException JavaDoc getSyntaxError(String JavaDoc sql, int index) {
71         sql = StringUtils.addAsterix(sql, index);
72         return Message.getSQLException(Message.SYNTAX_ERROR_1, sql);
73     }
74
75     public static SQLException JavaDoc getSyntaxError(String JavaDoc sql, int index, String JavaDoc expected) {
76         sql = StringUtils.addAsterix(sql, index);
77         return Message.getSQLException(Message.SYNTAX_ERROR_2, new String JavaDoc[]{sql, expected}, null);
78     }
79
80     /**
81      * Gets the SQL Exception object for a specific SQLState.
82      *
83      * @param sqlstate -
84      * the SQL State
85      * @return the SQLException object
86      */

87     public static JdbcSQLException getSQLException(int sqlstate) {
88         return getSQLException(sqlstate, null);
89     }
90
91     public static JdbcSQLException getUnsupportedException() {
92         return getSQLException(Message.FEATURE_NOT_SUPPORTED);
93     }
94
95     public static JdbcSQLException getInvalidValueException(String JavaDoc value, String JavaDoc param) {
96         return getSQLException(Message.INVALID_VALUE_2, new String JavaDoc[]{value, param}, null);
97     }
98
99     public static Error JavaDoc getInternalError(String JavaDoc s) {
100         Error JavaDoc e = new Error JavaDoc(s);
101         TraceSystem.traceThrowable(e);
102         return e;
103     }
104
105     public static Error JavaDoc getInternalError(String JavaDoc s, Exception JavaDoc e) {
106 //#ifdef JDK14
107
Error JavaDoc e2 = new Error JavaDoc(s, e);
108 //#endif
109
//#ifdef JDK13
110
/*
111         Error e2 = new Error(s);
112 */

113 //#endif
114
TraceSystem.traceThrowable(e2);
115         return e2;
116     }
117
118     private static String JavaDoc getState(int errorCode) {
119         switch(errorCode) {
120         // 02: no data
121
case NO_DATA_AVAILABLE: return "02000";
122
123         // 07: dynamic SQL error
124
case INVALID_PARAMETER_COUNT_1: return "07001";
125
126         // 08: connection exception
127
case ERROR_OPENING_DATABASE: return "08000";
128         case WRONG_USER_OR_PASSWORD: return "08004";
129
130         // 21: cardinality violation
131
case COLUMN_COUNT_DOES_NOT_MATCH: return "21S02";
132
133         // 22: data exception
134
case NUMERIC_VALUE_OUT_OF_RANGE: return "22003";
135         case DIVISION_BY_ZERO_1: return "22012";
136         case LIKE_ESCAPE_ERROR_1: return "22025";
137
138         // 23: integrity constraint violation
139
case CHECK_CONSTRAINT_VIOLATED_1: return "23000";
140         case DUPLICATE_KEY_1: return "23001"; // integrity constraint violation
141

142         // 3B: savepoint exception
143

144         // 42: syntax error or access rule violation
145
case SYNTAX_ERROR_1: return "42000";
146         case SYNTAX_ERROR_2: return "42001";
147         case TABLE_OR_VIEW_ALREADY_EXISTS_1: return "42S01";
148         case TABLE_OR_VIEW_NOT_FOUND_1: return "42S02";
149         case INDEX_ALREADY_EXISTS_1: return "42S11";
150         case INDEX_NOT_FOUND_1: return "42S12";
151         case DUPLICATE_COLUMN_NAME_1: return "42S21";
152         case COLUMN_NOT_FOUND_1: return "42S22";
153         case SETTING_NOT_FOUND_1: return "42S32";
154
155         // 0A: feature not supported
156

157         // HZ: remote database access
158

159         //
160
case GENERAL_ERROR_1: return "HY000";
161         case UNKNOWN_DATA_TYPE_1: return "HY004";
162
163         case FEATURE_NOT_SUPPORTED: return "HYC00";
164         case LOCK_TIMEOUT_1: return "HYT00";
165
166         }
167         return "" + errorCode;
168     }
169
170     // 02: no data
171
public static final int NO_DATA_AVAILABLE = 2000;
172
173     // 07: dynamic SQL error
174
public static final int INVALID_PARAMETER_COUNT_1 = 7001;
175
176     // 08: connection exception
177
public static final int ERROR_OPENING_DATABASE = 8000;
178     public static final int WRONG_USER_OR_PASSWORD = 8004;
179
180     // 21: cardinality violation
181
public static final int COLUMN_COUNT_DOES_NOT_MATCH = 21002;
182
183     // 22: data exception
184
public static final int NUMERIC_VALUE_OUT_OF_RANGE = 22003;
185     public static final int DIVISION_BY_ZERO_1 = 22012;
186     public static final int LIKE_ESCAPE_ERROR_1 = 22025;
187
188     // 23: integrity constraint violation
189
public static final int CHECK_CONSTRAINT_VIOLATED_1 = 23000;
190     public static final int DUPLICATE_KEY_1 = 23001; // integrity constraint violation
191

192     // 3B: savepoint exception
193

194     // 42: syntax error or access rule violation
195
public static final int SYNTAX_ERROR_1 = 42000;
196     public static final int SYNTAX_ERROR_2 = 42001;
197     public static final int TABLE_OR_VIEW_ALREADY_EXISTS_1 = 42101;
198     public static final int TABLE_OR_VIEW_NOT_FOUND_1 = 42102;
199     public static final int INDEX_ALREADY_EXISTS_1 = 42111;
200     public static final int INDEX_NOT_FOUND_1 = 42112;
201     public static final int DUPLICATE_COLUMN_NAME_1 = 42121;
202     public static final int COLUMN_NOT_FOUND_1 = 42122;
203     public static final int SETTING_NOT_FOUND_1 = 42132;
204
205     // 0A: feature not supported
206

207     // HZ: remote database access
208

209     //
210
public static final int GENERAL_ERROR_1 = 50000;
211     public static final int UNKNOWN_DATA_TYPE_1 = 50004;
212
213     public static final int FEATURE_NOT_SUPPORTED = 50100;
214     public static final int LOCK_TIMEOUT_1 = 50200;
215
216     public static final int FUNCTION_MUST_RETURN_RESULT_SET_1 = 90000;
217     public static final int METHOD_NOT_ALLOWED_FOR_QUERY = 90001;
218     public static final int METHOD_ONLY_ALLOWED_FOR_QUERY = 90002;
219     public static final int HEX_STRING_ODD_1 = 90003;
220     public static final int HEX_STRING_WRONG_1 = 90004;
221     public static final int VALUE_TOO_LONG_1 = 90005;
222     public static final int NULL_NOT_ALLOWED = 90006;
223     public static final int OBJECT_CLOSED = 90007;
224     public static final int INVALID_VALUE_2 = 90008;
225     public static final int DATE_CONSTANT_1 = 90009;
226     public static final int TIME_CONSTANT_1 = 90010;
227     public static final int TIMESTAMP_CONSTANT_1 = 90011;
228     public static final int PARAMETER_NOT_SET_1 = 90012;
229     public static final int DATABASE_NOT_FOUND_1 = 90013;
230     public static final int PARSE_ERROR_1 = 90014;
231     public static final int SUM_OR_AVG_ON_WRONG_DATATYPE_1 = 90015;
232     public static final int MUST_GROUP_BY_COLUMN_1 = 90016;
233     public static final int SECOND_PRIMARY_KEY = 90017;
234     public static final int TRACE_CONNECTION_NOT_CLOSED = 90018;
235     public static final int CANT_DROP_CURRENT_USER = 90019;
236     public static final int DATABASE_ALREADY_OPEN_1 = 90020;
237     public static final int DATA_CONVERSION_ERROR_1 = 90021;
238     public static final int FUNCTION_NOT_FOUND_1 = 90022;
239     public static final int COLUMN_MUST_NOT_BE_NULLABLE_1 = 90023;
240     public static final int FILE_RENAME_FAILED_2 = 90024;
241     public static final int FILE_DELETE_FAILED_1 = 90025;
242     public static final int SERIALIZATION_FAILED = 90026;
243     public static final int DESERIALIZATION_FAILED = 90027;
244     public static final int IO_EXCEPTION_1 = 90028;
245     public static final int NOT_ON_UPDATABLE_ROW = 90029;
246     public static final int FILE_CORRUPTED_1 = 90030;
247     public static final int CONNECTION_NOT_CLOSED = 90031;
248     public static final int USER_NOT_FOUND_1 = 90032;
249     public static final int USER_ALREADY_EXISTS_1 = 90033;
250     public static final int LOG_FILE_ERROR_1 = 90034;
251     public static final int SEQUENCE_ALREADY_EXISTS_1 = 90035;
252     public static final int SEQUENCE_NOT_FOUND_1 = 90036;
253     public static final int VIEW_NOT_FOUND_1 = 90037;
254     public static final int VIEW_ALREADY_EXISTS_1 = 90038;
255     public static final int VALUE_TOO_LARGE_FOR_PRECISION_1 = 90039;
256     public static final int ADMIN_RIGHTS_REQUIRED = 90040;
257     public static final int TRIGGER_ALREADY_EXISTS_1 = 90041;
258     public static final int TRIGGER_NOT_FOUND_1 = 90042;
259     public static final int ERROR_CREATING_TRIGGER_OBJECT_2 = 90043;
260     public static final int ERROR_EXECUTING_TRIGGER_2 = 90044;
261     public static final int CONSTRAINT_ALREADY_EXISTS_1 = 90045;
262     public static final int URL_FORMAT_ERROR_2 = 90046;
263     public static final int DRIVER_VERSION_ERROR_2 = 90047;
264     public static final int FILE_VERSION_ERROR_1 = 90048;
265     public static final int FILE_ENCRYPTION_ERROR_1 = 90049;
266     public static final int WRONG_PASSWORD_FORMAT = 90050;
267     public static final int STATEMENT_WAS_CANCELLED = 90051;
268     public static final int SUBQUERY_IS_NOT_SINGLE_COLUMN = 90052;
269     public static final int SCALAR_SUBQUERY_CONTAINS_MORE_THAN_ONE_ROW = 90053;
270     public static final int INVALID_USE_OF_AGGREGATE_FUNCTION_1 = 90054;
271     public static final int UNSUPPORTED_CIPHER = 90055;
272     public static final int NO_DEFAULT_SET_1 = 90056;
273     public static final int CONSTRAINT_NOT_FOUND_1 = 90057;
274     public static final int DUPLICATE_TABLE_ALIAS = 90058;
275     public static final int AMBIGUOUS_COLUMN_NAME_1 = 90059;
276     public static final int UNSUPPORTED_LOCK_METHOD_1 = 90060;
277     public static final int EXCEPTION_OPENING_PORT_1 = 90061;
278     public static final int FILE_CREATION_FAILED_1 = 90062;
279     public static final int SAVEPOINT_IS_INVALID_1 = 90063;
280     public static final int SAVEPOINT_IS_UNNAMED = 90064;
281     public static final int SAVEPOINT_IS_NAMED = 90065;
282     public static final int DUPLICATE_PROPERTY_1 = 90066;
283     public static final int CONNECTION_BROKEN = 90067;
284     public static final int ORDER_BY_NOT_IN_RESULT = 90068;
285     public static final int ROLE_ALREADY_EXISTS_1 = 90069;
286     public static final int ROLE_NOT_FOUND_1 = 90070;
287     public static final int USER_OR_ROLE_NOT_FOUND_1 = 90071;
288     public static final int ROLES_AND_RIGHT_CANNOT_BE_MIXED = 90072;
289     public static final int RIGHT_NOT_FOUND = 90073;
290     public static final int ROLE_ALREADY_GRANTED_1 = 90074;
291     public static final int COLUMN_IS_PART_OF_INDEX_1 = 90075;
292     public static final int FUNCTION_ALIAS_ALREADY_EXISTS_1 = 90076;
293     public static final int FUNCTION_ALIAS_NOT_FOUND_1 = 90077;
294     public static final int SCHEMA_ALREADY_EXISTS_1 = 90078;
295     public static final int SCHEMA_NOT_FOUND_1 = 90079;
296     public static final int SCHEMA_NAME_MUST_MATCH = 90080;
297     public static final int COLUMN_CONTAINS_NULL_VALUES_1 = 90081;
298     public static final int SEQUENCE_BELONGS_TO_A_TABLE_1 = 90082;
299     public static final int COLUMN_MAY_BE_REFERENCED_1 = 90083;
300     public static final int CANT_DROP_LAST_COLUMN = 90084;
301     public static final int INDEX_BELONGS_TO_CONSTRAINT_1 = 90085;
302     public static final int CLASS_NOT_FOUND_1 = 90086;
303     public static final int METHOD_NOT_FOUND_1 = 90087;
304     public static final int UNKNOWN_MODE_1 = 90088;
305     public static final int COLLATION_CHANGE_WITH_DATA_TABLE_1 = 90089;
306     public static final int SCHEMA_CAN_NOT_BE_DROPPED_1 = 90090;
307     public static final int ROLE_CAN_NOT_BE_DROPPED_1 = 90091;
308     public static final int UNSUPPORTED_JAVA_VERSION = 90092;
309     public static final int CLUSTER_ERROR_DATABASE_RUNS_ALONE = 90093;
310     public static final int CLUSTER_ERROR_DATABASE_RUNS_CLUSTERED_1 = 90094;
311     public static final int STRING_FORMAT_ERROR_1 = 90095;
312     public static final int NOT_ENOUGH_RIGHTS_FOR_1 = 90096;
313     public static final int DATABASE_IS_READ_ONLY = 90097;
314     public static final int SIMULATED_POWER_OFF = 90098;
315     public static final int ERROR_SETTING_DATABASE_EVENT_LISTENER = 90099;
316     public static final int NO_DISK_SPACE_AVAILABLE = 90100;
317     public static final int WRONG_XID_FORMAT_1 = 90101;
318     public static final int UNSUPPORTED_COMPRESSION_OPTIONS_1 = 90102;
319     public static final int UNSUPPORTED_COMPRESSION_ALGORITHM_1 = 90103;
320     public static final int COMPRESSION_ERROR = 90104;
321     private static final int EXCEPTION_IN_FUNCTION = 90105;
322     public static final int CANT_TRUNCATE_1 = 90106;
323     public static final int CANT_DROP_2 = 90107;
324     public static final int STACK_OVERFLOW = 90108;
325     public static final int VIEW_IS_INVALID_1 = 90109;
326     public static final int OVERFLOW_FOR_TYPE_1 = 90110;
327     public static final int ERROR_ACCESSING_LINKED_TABLE_1 = 90111;
328     public static final int ROW_NOT_FOUND_WHEN_DELETING_1 = 90112;
329     public static final int UNSUPPORTED_SETTING_1 = 90113;
330     public static final int CONSTANT_ALREADY_EXISTS_1 = 90114;
331     public static final int CONSTANT_NOT_FOUND_1 = 90115;
332     public static final int LITERALS_ARE_NOT_ALLOWED = 90116;
333     public static final int REMOTE_CONNECTION_NOT_ALLOWED = 90117;
334     public static final int CANT_DROP_TABLE_1 = 90118;
335     public static final int USER_DATA_TYPE_ALREADY_EXISTS_1 = 90119;
336     public static final int USER_DATA_TYPE_NOT_FOUND_1 = 90120;
337     public static final int DATABASE_CALLED_AT_SHUTDOWN = 90121;
338     public static final int OPERATION_NOT_SUPPORTED_WITH_VIEWS_2 = 90122;
339     public static final int CANT_MIX_INDEXED_AND_UNINDEXED_PARAMS = 90123;
340     public static final int FILE_NOT_FOUND_1 = 90124;
341     public static final int INVALID_CLASS_2 = 90125;
342
343     public static SQLException JavaDoc addSQL(SQLException JavaDoc e, String JavaDoc sql) {
344         if(e.getMessage().indexOf("SQL")>=0) {
345             return e;
346         }
347         if(e instanceof JdbcSQLException) {
348             JdbcSQLException j = (JdbcSQLException) e;
349             return new JdbcSQLException(j.getOriginalMessage()+"; SQL statement: "+sql, j.getSQLState(), j.getErrorCode(), j);
350         } else {
351             return new JdbcSQLException(e.getMessage()+"; SQL statement: "+sql, e.getSQLState(), e.getErrorCode(), e);
352         }
353     }
354
355     public static SQLException JavaDoc convert(Throwable JavaDoc e) {
356         if(e instanceof InternalException) {
357             e = ((InternalException)e).getOriginalCause();
358         }
359         if(e instanceof SQLException JavaDoc) {
360             return (SQLException JavaDoc)e;
361         } else if(e instanceof InvocationTargetException JavaDoc) {
362             InvocationTargetException JavaDoc ite = (InvocationTargetException JavaDoc)e;
363             Throwable JavaDoc t = ite.getTargetException();
364             if(t instanceof SQLException JavaDoc) {
365                 return (SQLException JavaDoc)t;
366             }
367             return Message.getSQLException(Message.EXCEPTION_IN_FUNCTION, null, e);
368         } else if(e instanceof IOException JavaDoc) {
369             return Message.getSQLException(Message.IO_EXCEPTION_1, new String JavaDoc[]{e.toString()}, e);
370         }
371         return Message.getSQLException(Message.GENERAL_ERROR_1, new String JavaDoc[]{e.toString()}, e);
372     }
373
374     public static Error JavaDoc getInternalError() {
375         return getInternalError("unexpected code path");
376     }
377
378     public static InternalException convertToInternal(Exception JavaDoc e) {
379         return new InternalException(e);
380     }
381
382     public static IOException JavaDoc convertToIOException(Throwable JavaDoc e) {
383         if(e instanceof JdbcSQLException) {
384             JdbcSQLException e2 = (JdbcSQLException)e;
385             if(e2.getOriginalCause() != null) {
386                 e = e2.getOriginalCause();
387             }
388         }
389         IOException JavaDoc io = new IOException JavaDoc(e.toString());
390         io.fillInStackTrace();
391         return io;
392     }
393
394 }
395
Popular Tags