1 /***
2 * SQLExceptionHandlerFactory.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2004 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.sql;
27
28 import java.util.Hashtable;
29
30 /***
31 * This class enables to build a SQLExceptionHandler when needed from the name
32 * of the vendor.
33 *
34 * @author $Author: dbregeon $
35 * @version $Revision: 1.1 $
36 */
37 public class SQLExceptionHandlerFactory {
38 /***
39 * The unique instance of this class.
40 */
41 private static SQLExceptionHandlerFactory instance = null;
42
43 /***
44 * Keys are the vendor names, values are the SQLExceptionHandler classes.
45 */
46 private final Hashtable handlerClasses = new Hashtable();
47
48 /***
49 * Give access to the unique instance of the class.
50 *
51 * @return the unique instance of the class.
52 */
53 public static SQLExceptionHandlerFactory getInstance() {
54 if (instance == null) {
55 instance = new SQLExceptionHandlerFactory();
56 }
57 return instance;
58 }
59
60 /***
61 * Enables to associate a SQLExceptionHandler class to a product name. It
62 * silently ignores null and invalid values.
63 *
64 * @param productName
65 * the name of the product as returned from the DatabaseMetaData.
66 * @param handlerClass
67 * the class which instances can handle errors from the product.
68 */
69 public void registerSQLExceptionHandlerClass(String productName,
70 Class handlerClass) {
71 if ((productName != null) && (handlerClass != null)
72 && (SQLExceptionHandler.class.isAssignableFrom(handlerClass))) {
73 this.handlerClasses.put(productName, handlerClass);
74 }
75 }
76
77 /***
78 * Builds a new instance of the SQLExceptionHandler class associated to the
79 * product name. If no such class exists, an instance of
80 * DefaultSQLExceptionHandler is returned.
81 *
82 * @param productName
83 * the name of the product as returned from the DatabaseMetaData.
84 * @return an instance of a SQLExceptionHandler implementation.
85 */
86 public SQLExceptionHandler buildSQLExceptionHandler(String productName) {
87 SQLExceptionHandler result = null;
88 if (productName != null) {
89 Class clazz = (Class) this.handlerClasses.get(productName);
90 if (clazz != null) {
91 try {
92 result = (SQLExceptionHandler) clazz.newInstance();
93 } catch (InstantiationException e) {
94 e.printStackTrace();
95 } catch (IllegalAccessException e) {
96 e.printStackTrace();
97 }
98 }
99 }
100 if (result == null) {
101 result = new DefaultSQLExceptionHandler();
102 }
103 return result;
104 }
105
106 }