File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\message\DbException.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.message;
8
9 import java.io.ByteArrayInputStream;
10 import java.io.IOException;
11 import java.lang.reflect.InvocationTargetException;
12 import java.sql.SQLException;
13 import java.text.MessageFormat;
14 import java.util.Locale;
15 import java.util.Properties;
16 import java.util.Map.Entry;
17 import org.h2.constant.ErrorCode;
18 import org.h2.jdbc.JdbcSQLException;
19 import org.h2.util.SortedProperties;
20 import org.h2.util.StringUtils;
21 import org.h2.util.Utils;
22
23 /**
24 * This exception wraps a checked exception.
25 * It is used in methods where checked exceptions are not supported,
26 * for example in a Comparator.
27 */
28 public class DbException extends RuntimeException {
29
30 private static final long serialVersionUID = 1L;
31
32 private static final Properties MESSAGES = new Properties();
33
34 private Object source;
35
36 static {
37 try {
38 byte[] messages = Utils.getResource("/org/h2/res/_messages_en.prop");
39 if (messages != null) {
40 MESSAGES.load(new ByteArrayInputStream(messages));
41 }
42 String language = Locale.getDefault().getLanguage();
43 if (!"en".equals(language)) {
44 byte[] translations = Utils.getResource("/org/h2/res/_messages_" + language + ".prop");
45 // message: translated message + english
46 // (otherwise certain applications don't work)
47 if (translations != null) {
48 Properties p = SortedProperties.fromLines(new String(translations, "UTF-8"));
49 for (Entry<Object, Object> e : p.entrySet()) {
50 String key = (String) e.getKey();
51 String translation = (String) e.getValue();
52 if (translation != null && !translation.startsWith("#")) {
53 String original = MESSAGES.getProperty(key);
54 String message = translation + "\n" + original;
55 MESSAGES.put(key, message);
56 }
57 }
58 }
59 }
60 } catch (IOException e) {
61 TraceSystem.traceThrowable(e);
62 }
63 }
64
65 private DbException(SQLException e) {
66 super(e.getMessage(), e);
67 }
68
69 private static String translate(String key, String... params) {
70 String message = null;
71 if (MESSAGES != null) {
72 // Tomcat sets final static fields to null sometimes
73 message = MESSAGES.getProperty(key);
74 }
75 if (message == null) {
76 message = "(Message " + key + " not found)";
77 }
78 if (params != null) {
79 for (int i = 0; i < params.length; i++) {
80 String s = params[i];
81 if (s != null && s.length() > 0) {
82 params[i] = StringUtils.quoteIdentifier(s);
83 }
84 }
85 message = MessageFormat.format(message, (Object[]) params);
86 }
87 return message;
88 }
89
90 /**
91 * Get the SQLException object.
92 *
93 * @return the exception
94 */
95 public SQLException getSQLException() {
96 return (SQLException) getCause();
97 }
98
99 /**
100 * Get the error code.
101 *
102 * @return the error code
103 */
104 public int getErrorCode() {
105 return getSQLException().getErrorCode();
106 }
107
108 /**
109 * Set the SQL statement of the given exception.
110 * This method may create a new object.
111 *
112 * @param sql the SQL statement
113 * @return the exception
114 */
115 public DbException addSQL(String sql) {
116 SQLException e = getSQLException();
117 if (e instanceof JdbcSQLException) {
118 JdbcSQLException j = (JdbcSQLException) e;
119 if (j.getSQL() == null) {
120 j.setSQL(sql);
121 }
122 return this;
123 }
124 e = new JdbcSQLException(e.getMessage(), sql, e.getSQLState(), e.getErrorCode(), e, null);
125 return new DbException(e);
126 }
127
128 /**
129 * Create a database exception for a specific error code.
130 *
131 * @param errorCode the error code
132 * @return the exception
133 */
134 public static DbException get(int errorCode) {
135 return get(errorCode, (String) null);
136 }
137
138 /**
139 * Create a database exception for a specific error code.
140 *
141 * @param errorCode the error code
142 * @param p1 the first parameter of the message
143 * @return the exception
144 */
145 public static DbException get(int errorCode, String p1) {
146 return get(errorCode, new String[] { p1 });
147 }
148
149 /**
150 * Create a database exception for a specific error code.
151 *
152 * @param errorCode the error code
153 * @param cause the cause of the exception
154 * @param params the list of parameters of the message
155 * @return the exception
156 */
157 public static DbException get(int errorCode, Throwable cause, String... params) {
158 return new DbException(getJdbcSQLException(errorCode, cause, params));
159 }
160
161 /**
162 * Create a database exception for a specific error code.
163 *
164 * @param errorCode the error code
165 * @param params the list of parameters of the message
166 * @return the exception
167 */
168 public static DbException get(int errorCode, String... params) {
169 return new DbException(getJdbcSQLException(errorCode, null, params));
170 }
171
172 /**
173 * Create a syntax error exception.
174 *
175 * @param sql the SQL statement
176 * @param index the position of the error in the SQL statement
177 * @return the exception
178 */
179 public static DbException getSyntaxError(String sql, int index) {
180 sql = StringUtils.addAsterisk(sql, index);
181 return get(ErrorCode.SYNTAX_ERROR_1, sql);
182 }
183
184 /**
185 * Create a syntax error exception.
186 *
187 * @param sql the SQL statement
188 * @param index the position of the error in the SQL statement
189 * @param expected the expected keyword at the given position
190 * @return the exception
191 */
192 public static DbException getSyntaxError(String sql, int index, String expected) {
193 sql = StringUtils.addAsterisk(sql, index);
194 return get(ErrorCode.SYNTAX_ERROR_2, sql, expected);
195 }
196
197 /**
198 * Gets a SQL exception meaning this feature is not supported.
199 *
200 * @param message what exactly is not supported
201 * @return the exception
202 */
203 public static DbException getUnsupportedException(String message) {
204 return get(ErrorCode.FEATURE_NOT_SUPPORTED_1, message);
205 }
206
207 /**
208 * Gets a SQL exception meaning this value is invalid.
209 *
210 * @param param the name of the parameter
211 * @param value the value passed
212 * @return the IllegalArgumentException object
213 */
214 public static DbException getInvalidValueException(String param, Object value) {
215 return get(ErrorCode.INVALID_VALUE_2, value == null ? "null" : value.toString(), param);
216 }
217
218 /**
219 * Throw an internal error. This method seems to return an exception object,
220 * so that it can be used instead of 'return', but in fact it always throws
221 * the exception.
222 *
223 * @param s the message
224 * @return the RuntimeException object
225 * @throws RuntimeException the exception
226 */
227 public static RuntimeException throwInternalError(String s) {
228 RuntimeException e = new RuntimeException(s);
229 TraceSystem.traceThrowable(e);
230 throw e;
231 }
232
233 /**
234 * Throw an internal error. This method seems to return an exception object,
235 * so that it can be used instead of 'return', but in fact it always throws
236 * the exception.
237 *
238 * @return the RuntimeException object
239 */
240 public static RuntimeException throwInternalError() {
241 return throwInternalError("Unexpected code path");
242 }
243
244 /**
245 * Convert an exception to a SQL exception using the default mapping.
246 *
247 * @param e the root cause
248 * @return the SQL exception object
249 */
250 public static SQLException toSQLException(Exception e) {
251 if (e instanceof SQLException) {
252 return (SQLException) e;
253 }
254 return convert(e).getSQLException();
255 }
256
257 /**
258 * Convert a throwable to an SQL exception using the default mapping. All
259 * errors except the following are re-thrown: StackOverflowError,
260 * LinkageError.
261 *
262 * @param e the root cause
263 * @return the exception object
264 */
265 public static DbException convert(Throwable e) {
266 if (e instanceof DbException) {
267 return (DbException) e;
268 } else if (e instanceof SQLException) {
269 return new DbException((SQLException) e);
270 } else if (e instanceof InvocationTargetException) {
271 return convertInvocation((InvocationTargetException) e, null);
272 } else if (e instanceof IOException) {
273 return get(ErrorCode.IO_EXCEPTION_1, e, e.toString());
274 } else if (e instanceof OutOfMemoryError) {
275 return get(ErrorCode.OUT_OF_MEMORY, e);
276 } else if (e instanceof StackOverflowError || e instanceof LinkageError) {
277 return get(ErrorCode.GENERAL_ERROR_1, e, e.toString());
278 } else if (e instanceof Error) {
279 throw (Error) e;
280 }
281 return get(ErrorCode.GENERAL_ERROR_1, e, e.toString());
282 }
283
284 /**
285 * Convert an InvocationTarget exception to a database exception.
286 *
287 * @param te the root cause
288 * @param message the added message or null
289 * @return the database exception object
290 */
291 public static DbException convertInvocation(InvocationTargetException te, String message) {
292 Throwable t = te.getTargetException();
293 if (t instanceof SQLException || t instanceof DbException) {
294 return convert(t);
295 }
296 message = message == null ? t.getMessage() : message + ": " + t.getMessage();
297 return get(ErrorCode.EXCEPTION_IN_FUNCTION_1, t, message);
298 }
299
300 /**
301 * Convert an IO exception to a database exception.
302 *
303 * @param e the root cause
304 * @param message the message or null
305 * @return the database exception object
306 */
307 public static DbException convertIOException(IOException e, String message) {
308 if (message == null) {
309 Throwable t = e.getCause();
310 if (t instanceof DbException) {
311 return (DbException) t;
312 }
313 return get(ErrorCode.IO_EXCEPTION_1, e, e.toString());
314 }
315 return get(ErrorCode.IO_EXCEPTION_2, e, e.toString(), message);
316 }
317
318 /**
319 * Gets the SQL exception object for a specific error code.
320 *
321 * @param errorCode the error code
322 * @param cause the cause of the exception
323 * @param params the list of parameters of the message
324 * @return the SQLException object
325 */
326 private static JdbcSQLException getJdbcSQLException(int errorCode, Throwable cause, String... params) {
327 String sqlstate = ErrorCode.getState(errorCode);
328 String message = translate(sqlstate, params);
329 return new JdbcSQLException(message, null, sqlstate, errorCode, cause, null);
330 }
331
332 /**
333 * Convert an exception to an IO exception.
334 *
335 * @param e the root cause
336 * @return the IO exception
337 */
338 public static IOException convertToIOException(Throwable e) {
339 if (e instanceof IOException) {
340 return (IOException) e;
341 }
342 if (e instanceof JdbcSQLException) {
343 JdbcSQLException e2 = (JdbcSQLException) e;
344 if (e2.getOriginalCause() != null) {
345 e = e2.getOriginalCause();
346 }
347 }
348 IOException io = new IOException(e.toString());
349 io.initCause(e);
350 return io;
351 }
352
353 public Object getSource() {
354 return source;
355 }
356
357 public void setSource(Object source) {
358 this.source = source;
359 }
360
361 }
362