01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
05: * (C) 2005, Institut de Recherche pour le Développement
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: */
17: package org.geotools.resources;
18:
19: // J2SE dependencies
20: import java.util.Set;
21: import java.util.HashSet;
22: import java.util.logging.Level;
23: import java.util.logging.LogRecord;
24: import java.sql.Driver;
25:
26: // Geotools dependencies
27: import org.geotools.resources.i18n.Logging;
28: import org.geotools.resources.i18n.LoggingKeys;
29:
30: /**
31: * A set of utilities methods related to JDBC (<cite>Java Database Connectivity</cite>).
32: *
33: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/resources/JDBC.java $
34: * @version $Id: JDBC.java 22443 2006-10-27 20:47:22Z desruisseaux $
35: * @author Martin Desruisseaux
36: *
37: * @todo This class may be removed when we will be allowed to compile for J2SE 1.6.
38: */
39: public final class JDBC {
40: /**
41: * Lists of JDBC drivers already loaded.
42: */
43: private static final Set DRIVERS = new HashSet();
44:
45: /**
46: * Do not allow instantiation of this class.
47: */
48: private JDBC() {
49: }
50:
51: /**
52: * Attempts to load the specified JDBC driver, if not already done. If this method has already
53: * been invoked for the specified driver, then it does nothing and returns {@code null}.
54: * Otherwise, it attempts to load the specified driver and returns a log record initialized
55: * with a message at the {@link Level#CONFIG CONFIG} level on success, or at the
56: * {@link Level#WARNING WARNING} level on failure.
57: *
58: * @param driver The JDBC driver to load, as a fully qualified Java class name.
59: * @return A log message with driver information, or {@code null} if the driver was already
60: * loaded.
61: */
62: public static LogRecord loadDriver(final String driver) {
63: LogRecord log = null;
64: if (driver != null) {
65: synchronized (DRIVERS) {
66: if (!DRIVERS.contains(driver)) {
67: try {
68: final Driver d = (Driver) Class.forName(driver)
69: .newInstance();
70: log = Logging.format(Level.CONFIG,
71: LoggingKeys.LOADED_JDBC_DRIVER_$3,
72: driver,
73: new Integer(d.getMajorVersion()),
74: new Integer(d.getMinorVersion()));
75: DRIVERS.add(driver);
76: } catch (Exception exception) {
77: log = Utilities.getLogRecord(exception);
78: }
79: }
80: }
81: }
82: return log;
83: }
84: }
|