| 1 | /* DriverManager.java -- Manage JDBC drivers
|
|---|
| 2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.sql;
|
|---|
| 40 |
|
|---|
| 41 | import java.io.PrintStream;
|
|---|
| 42 | import java.io.PrintWriter;
|
|---|
| 43 | import java.util.Enumeration;
|
|---|
| 44 | import java.util.Properties;
|
|---|
| 45 | import java.util.StringTokenizer;
|
|---|
| 46 | import java.util.Vector;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * This class manages the JDBC drivers in the system. It maintains a
|
|---|
| 50 | * registry of drivers and locates the appropriate driver to handle a
|
|---|
| 51 | * JDBC database URL.
|
|---|
| 52 | * <p>
|
|---|
| 53 | * On startup, <code>DriverManager</code> loads all the managers specified
|
|---|
| 54 | * by the system property <code>jdbc.drivers</code>. The value of this
|
|---|
| 55 | * property should be a colon separated list of fully qualified driver
|
|---|
| 56 | * class names. Additional drivers can be loaded at any time by
|
|---|
| 57 | * simply loading the driver class with <code>class.forName(String)</code>.
|
|---|
| 58 | * The driver should automatically register itself in a static
|
|---|
| 59 | * initializer.
|
|---|
| 60 | * <p>
|
|---|
| 61 | * The methods in this class are all <code>static</code>. This class
|
|---|
| 62 | * cannot be instantiated.
|
|---|
| 63 | *
|
|---|
| 64 | * @author Aaron M. Renn ([email protected])
|
|---|
| 65 | */
|
|---|
| 66 | public class DriverManager
|
|---|
| 67 | {
|
|---|
| 68 |
|
|---|
| 69 | /*
|
|---|
| 70 | * Class Variables
|
|---|
| 71 | */
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * This is the log stream for JDBC drivers.
|
|---|
| 75 | */
|
|---|
| 76 | private static PrintStream log_stream;
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * This is the log writer for JDBC drivers.
|
|---|
| 80 | */
|
|---|
| 81 | private static PrintWriter log_writer;
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * This is the login timeout used by JDBC drivers.
|
|---|
| 85 | */
|
|---|
| 86 | private static int login_timeout;
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * This is the list of JDBC drivers that are loaded.
|
|---|
| 90 | */
|
|---|
| 91 | private static Vector drivers;
|
|---|
| 92 | // Hmm, seems like we might want to do a Hashtable and lookup by something,
|
|---|
| 93 | // but what would it be?
|
|---|
| 94 |
|
|---|
| 95 | // Load all drivers on startup
|
|---|
| 96 | static
|
|---|
| 97 | {
|
|---|
| 98 | drivers = new Vector();
|
|---|
| 99 |
|
|---|
| 100 | String driver_string = System.getProperty("jdbc.drivers");
|
|---|
| 101 | if (driver_string != null)
|
|---|
| 102 | {
|
|---|
| 103 | StringTokenizer st = new StringTokenizer(driver_string);
|
|---|
| 104 | while (st.hasMoreTokens())
|
|---|
| 105 | {
|
|---|
| 106 | String driver_classname = st.nextToken();
|
|---|
| 107 |
|
|---|
| 108 | try
|
|---|
| 109 | {
|
|---|
| 110 | Class.forName(driver_classname); // The driver registers itself
|
|---|
| 111 | }
|
|---|
| 112 | catch (Exception e) { ; } // Ignore not founds
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /*************************************************************************/
|
|---|
| 119 |
|
|---|
| 120 | /*
|
|---|
| 121 | * Class Methods
|
|---|
| 122 | */
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * This method returns the login timeout in use by JDBC drivers systemwide.
|
|---|
| 126 | *
|
|---|
| 127 | * @return The login timeout.
|
|---|
| 128 | */
|
|---|
| 129 | public static int
|
|---|
| 130 | getLoginTimeout()
|
|---|
| 131 | {
|
|---|
| 132 | return(login_timeout);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /*************************************************************************/
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * This method set the login timeout used by JDBC drivers. This is a
|
|---|
| 139 | * system-wide parameter that applies to all drivers.
|
|---|
| 140 | *
|
|---|
| 141 | * @param login_timeout The new login timeout value.
|
|---|
| 142 | */
|
|---|
| 143 | public static void
|
|---|
| 144 | setLoginTimeout(int login_timeout)
|
|---|
| 145 | {
|
|---|
| 146 | DriverManager.login_timeout = login_timeout;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /*************************************************************************/
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * This method returns the log writer being used by all JDBC drivers.
|
|---|
| 153 | * This method should be used in place of the deprecated
|
|---|
| 154 | * <code>getLogStream</code> method.
|
|---|
| 155 | *
|
|---|
| 156 | * @return The log writer in use by JDBC drivers.
|
|---|
| 157 | */
|
|---|
| 158 | public static PrintWriter
|
|---|
| 159 | getLogWriter()
|
|---|
| 160 | {
|
|---|
| 161 | return(log_writer);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /*************************************************************************/
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * This method sets the log writer being used by JDBC drivers. This is a
|
|---|
| 168 | * system-wide parameter that affects all drivers. Note that since there
|
|---|
| 169 | * is no way to retrieve a <code>PrintStream</code> from a
|
|---|
| 170 | * <code>PrintWriter</code>, this method cannot set the log stream in
|
|---|
| 171 | * use by JDBC. Thus any older drivers may not see this setting.
|
|---|
| 172 | *
|
|---|
| 173 | * @param log_writer The new log writer for JDBC.
|
|---|
| 174 | */
|
|---|
| 175 | public static void
|
|---|
| 176 | setLogWriter(PrintWriter log_writer)
|
|---|
| 177 | {
|
|---|
| 178 | DriverManager.log_writer = log_writer;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /*************************************************************************/
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * This method returns the log stream in use by JDBC.
|
|---|
| 185 | *
|
|---|
| 186 | * @return The log stream in use by JDBC.
|
|---|
| 187 | *
|
|---|
| 188 | * @deprecated Use <code>getLogWriter()</code> instead.
|
|---|
| 189 | */
|
|---|
| 190 | public static PrintStream
|
|---|
| 191 | getLogStream()
|
|---|
| 192 | {
|
|---|
| 193 | return(log_stream);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /*************************************************************************/
|
|---|
| 197 |
|
|---|
| 198 | /**
|
|---|
| 199 | * This method sets the log stream in use by JDBC.
|
|---|
| 200 | *
|
|---|
| 201 | * @param log_stream The log stream in use by JDBC.
|
|---|
| 202 | *
|
|---|
| 203 | * @deprecated Use <code>setLogWriter</code> instead.
|
|---|
| 204 | */
|
|---|
| 205 | public static void
|
|---|
| 206 | setLogStream(PrintStream log_stream)
|
|---|
| 207 | {
|
|---|
| 208 | DriverManager.log_stream = log_stream;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /*************************************************************************/
|
|---|
| 212 |
|
|---|
| 213 | /**
|
|---|
| 214 | * This method prints the specified line to the log stream.
|
|---|
| 215 | *
|
|---|
| 216 | * @param str The string to write to the log stream.
|
|---|
| 217 | */
|
|---|
| 218 | public static void
|
|---|
| 219 | println(String str)
|
|---|
| 220 | {
|
|---|
| 221 | if (log_stream != null) // Watch for user not using logging
|
|---|
| 222 | log_stream.println(str);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /*************************************************************************/
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| 228 | * This method registers a new driver with the manager. This is normally
|
|---|
| 229 | * called by the driver itself in a static initializer.
|
|---|
| 230 | *
|
|---|
| 231 | * @param driver The new <code>Driver</code> to add.
|
|---|
| 232 | *
|
|---|
| 233 | * @exception SQLException If an error occurs.
|
|---|
| 234 | */
|
|---|
| 235 | public static void
|
|---|
| 236 | registerDriver(Driver driver) throws SQLException
|
|---|
| 237 | {
|
|---|
| 238 | if (!drivers.contains(driver))
|
|---|
| 239 | drivers.addElement(driver);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /*************************************************************************/
|
|---|
| 243 |
|
|---|
| 244 | /**
|
|---|
| 245 | * This method de-registers a driver from the manager.
|
|---|
| 246 | *
|
|---|
| 247 | * @param driver The <code>Driver</code> to unregister.
|
|---|
| 248 | *
|
|---|
| 249 | * @exception SQLException If an error occurs.
|
|---|
| 250 | */
|
|---|
| 251 | public static void
|
|---|
| 252 | deregisterDriver(Driver driver) throws SQLException
|
|---|
| 253 | {
|
|---|
| 254 | if (drivers.contains(driver))
|
|---|
| 255 | drivers.removeElement(driver);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /*************************************************************************/
|
|---|
| 259 |
|
|---|
| 260 | /**
|
|---|
| 261 | * This method returns a list of all the currently registered JDBC drivers
|
|---|
| 262 | * that were loaded by the current <code>ClassLoader</code>.
|
|---|
| 263 | *
|
|---|
| 264 | * @return An <code>Enumeration</code> of all currently loaded JDBC drivers.
|
|---|
| 265 | */
|
|---|
| 266 | public static Enumeration
|
|---|
| 267 | getDrivers()
|
|---|
| 268 | {
|
|---|
| 269 | Vector v = new Vector();
|
|---|
| 270 | Enumeration e = drivers.elements();
|
|---|
| 271 |
|
|---|
| 272 | // Is this right?
|
|---|
| 273 | ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
|---|
| 274 |
|
|---|
| 275 | while(e.hasMoreElements())
|
|---|
| 276 | {
|
|---|
| 277 | Object obj = e.nextElement();
|
|---|
| 278 |
|
|---|
| 279 | ClassLoader loader = obj.getClass().getClassLoader();
|
|---|
| 280 |
|
|---|
| 281 | if (loader == null)
|
|---|
| 282 | loader = ClassLoader.getSystemClassLoader();
|
|---|
| 283 | if (!loader.equals(cl))
|
|---|
| 284 | continue;
|
|---|
| 285 |
|
|---|
| 286 | v.addElement(obj);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | return(v.elements());
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /*************************************************************************/
|
|---|
| 293 |
|
|---|
| 294 | /**
|
|---|
| 295 | * This method returns a driver that can connect to the specified
|
|---|
| 296 | * JDBC URL string. This will be selected from among drivers loaded
|
|---|
| 297 | * at initialization time and those drivers manually loaded by the
|
|---|
| 298 | * same class loader as the caller.
|
|---|
| 299 | *
|
|---|
| 300 | * @param url The JDBC URL string to find a driver for.
|
|---|
| 301 | *
|
|---|
| 302 | * @return A <code>Driver</code> that can connect to the specified
|
|---|
| 303 | * URL, or <code>null</code> if a suitable driver cannot be found.
|
|---|
| 304 | *
|
|---|
| 305 | * @exception SQLException If an error occurs.
|
|---|
| 306 | */
|
|---|
| 307 | public static Driver
|
|---|
| 308 | getDriver(String url) throws SQLException
|
|---|
| 309 | {
|
|---|
| 310 | // FIXME: Limit driver search to the appropriate subset of loaded drivers.
|
|---|
| 311 |
|
|---|
| 312 | Enumeration e = drivers.elements();
|
|---|
| 313 | while(e.hasMoreElements())
|
|---|
| 314 | {
|
|---|
| 315 | Driver d = (Driver)e.nextElement();
|
|---|
| 316 | if (d.acceptsURL(url))
|
|---|
| 317 | return(d);
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | return(null);
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /*************************************************************************/
|
|---|
| 324 |
|
|---|
| 325 | /**
|
|---|
| 326 | * This method attempts to return a connection to the specified
|
|---|
| 327 | * JDBC URL string.
|
|---|
| 328 | *
|
|---|
| 329 | * @param url The JDBC URL string to connect to.
|
|---|
| 330 | *
|
|---|
| 331 | * @return A <code>Connection</code> to that URL.
|
|---|
| 332 | *
|
|---|
| 333 | * @exception SQLException If an error occurs.
|
|---|
| 334 | */
|
|---|
| 335 | public static Connection
|
|---|
| 336 | getConnection(String url) throws SQLException
|
|---|
| 337 | {
|
|---|
| 338 | return(getConnection(url, new Properties()));
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | /*************************************************************************/
|
|---|
| 342 |
|
|---|
| 343 | /**
|
|---|
| 344 | * This method attempts to return a connection to the specified
|
|---|
| 345 | * JDBC URL string using the specified username and password.
|
|---|
| 346 | *
|
|---|
| 347 | * @param url The JDBC URL string to connect to.
|
|---|
| 348 | * @param user The username to connect with.
|
|---|
| 349 | * @param password The password to connect with.
|
|---|
| 350 | *
|
|---|
| 351 | * @return A <code>Connection</code> to that URL.
|
|---|
| 352 | *
|
|---|
| 353 | * @exception SQLException If an error occurs.
|
|---|
| 354 | */
|
|---|
| 355 | public static Connection
|
|---|
| 356 | getConnection(String url, String user, String password) throws SQLException
|
|---|
| 357 | {
|
|---|
| 358 | Properties p = new Properties();
|
|---|
| 359 |
|
|---|
| 360 | if (user != null)
|
|---|
| 361 | p.setProperty("user", user);
|
|---|
| 362 | if (password != null)
|
|---|
| 363 | p.setProperty("password", password);
|
|---|
| 364 |
|
|---|
| 365 | return(getConnection(url, p));
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | /*************************************************************************/
|
|---|
| 369 |
|
|---|
| 370 | /**
|
|---|
| 371 | * This method attempts to return a connection to the specified
|
|---|
| 372 | * JDBC URL string using the specified connection properties.
|
|---|
| 373 | *
|
|---|
| 374 | * @param url The JDBC URL string to connect to.
|
|---|
| 375 | * @param properties The connection properties.
|
|---|
| 376 | *
|
|---|
| 377 | * @return A <code>Connection</code> to that URL.
|
|---|
| 378 | *
|
|---|
| 379 | * @exception SQLException If an error occurs.
|
|---|
| 380 | */
|
|---|
| 381 | public static Connection
|
|---|
| 382 | getConnection(String url, Properties properties) throws SQLException
|
|---|
| 383 | {
|
|---|
| 384 | Driver d = getDriver(url);
|
|---|
| 385 | if (d == null)
|
|---|
| 386 | throw new SQLException("Driver not found for URL: " + url);
|
|---|
| 387 |
|
|---|
| 388 | return(d.connect(url, properties));
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | /*************************************************************************/
|
|---|
| 392 |
|
|---|
| 393 | /*
|
|---|
| 394 | * Constructors
|
|---|
| 395 | */
|
|---|
| 396 |
|
|---|
| 397 | // Keep bozos from trying to instantiate us.
|
|---|
| 398 | private
|
|---|
| 399 | DriverManager()
|
|---|
| 400 | {
|
|---|
| 401 | ;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | } // class DriverManager
|
|---|
| 405 |
|
|---|