DriverManager.java 10.3 KB
Newer Older
1
/* DriverManager.java -- Manage JDBC drivers
2
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */


package java.sql;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

/**
  * This class manages the JDBC drivers in the system. It maintains a
  * registry of drivers and locates the appropriate driver to handle a
  * JDBC database URL.
  * <p>
  * On startup, <code>DriverManager</code> loads all the managers specified
  * by the system property <code>jdbc.drivers</code>.  The value of this
  * property should be a colon separated list of fully qualified driver
  * class names.  Additional drivers can be loaded at any time by
  * simply loading the driver class with <code>class.forName(String)</code>.
  * The driver should automatically register itself in a static 
  * initializer.
  * <p>
  * The methods in this class are all <code>static</code>. This class
  * cannot be instantiated.
  *
  * @author Aaron M. Renn (arenn@urbanophile.com)
  */
public class DriverManager 
{

/*
 * Class Variables
 */

/**
  * This is the log stream for JDBC drivers.
  */
private static PrintStream log_stream;

/**
  * This is the log writer for JDBC drivers.
  */
private static PrintWriter log_writer;

/**
  * This is the login timeout used by JDBC drivers.
  */
private static int login_timeout;

/**
  * This is the list of JDBC drivers that are loaded.
  */
private static Vector drivers;
 // Hmm, seems like we might want to do a Hashtable and lookup by something,
 // but what would it be?

// Load all drivers on startup
static
{
  drivers = new Vector();

  String driver_string = System.getProperty("jdbc.drivers");
  if (driver_string != null)
    {
      StringTokenizer st = new StringTokenizer(driver_string);
      while (st.hasMoreTokens())
        {
          String driver_classname = st.nextToken();

          try
            {
              Class.forName(driver_classname); // The driver registers itself
            }
          catch (Exception e) { ; } // Ignore not founds
        }
    }

}
  
/*************************************************************************/

/*
 * Class Methods
 */

/**
  * This method returns the login timeout in use by JDBC drivers systemwide.
  *
  * @return The login timeout.
  */
public static int
getLoginTimeout()
{
  return(login_timeout);
}

/*************************************************************************/

/**
  * This method set the login timeout used by JDBC drivers.  This is a
  * system-wide parameter that applies to all drivers.
  *
  * @param login_timeout The new login timeout value.
  */
public static void
setLoginTimeout(int login_timeout)
{
  DriverManager.login_timeout = login_timeout;
}

/*************************************************************************/

/**
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  * This method returns the log writer being used by all JDBC drivers.
  * This method should be used in place of the deprecated
  * <code>getLogStream</code> method.
  *
  * @return The log writer in use by JDBC drivers.
  */
public static PrintWriter
getLogWriter()
{
  return(log_writer);
}

/*************************************************************************/

/**
  * This method sets the log writer being used by JDBC drivers.  This is a
  * system-wide parameter that affects all drivers.  Note that since there
  * is no way to retrieve a <code>PrintStream</code> from a 
  * <code>PrintWriter</code>, this method cannot set the log stream in
  * use by JDBC.  Thus any older drivers may not see this setting.
  *
  * @param log_writer The new log writer for JDBC.
  */
public static void
setLogWriter(PrintWriter log_writer)
{
  DriverManager.log_writer = log_writer;
}

/*************************************************************************/

/**
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  * This method returns the log stream in use by JDBC.
  *
  * @return The log stream in use by JDBC.
  *
  * @deprecated Use <code>getLogWriter()</code> instead.
  */
public static PrintStream
getLogStream()
{
  return(log_stream);
}

/*************************************************************************/

/**
  * This method sets the log stream in use by JDBC.
  *
  * @param log_stream The log stream in use by JDBC.
  *
  * @deprecated Use <code>setLogWriter</code> instead.
  */
public static void
setLogStream(PrintStream log_stream)
{
  DriverManager.log_stream = log_stream;
}

/*************************************************************************/

/**
  * This method prints the specified line to the log stream.
  *
  * @param str The string to write to the log stream.
  */
public static void
println(String str)
{
  if (log_stream != null) // Watch for user not using logging
    log_stream.println(str);
}

/*************************************************************************/

/**
  * This method registers a new driver with the manager.  This is normally
  * called by the driver itself in a static initializer.
  *
  * @param driver The new <code>Driver</code> to add.
221 222
  *
  * @exception SQLException If an error occurs.
223 224
  */
public static void
225
registerDriver(Driver driver) throws SQLException
226 227 228 229 230 231 232 233 234 235 236
{
  if (!drivers.contains(driver))
    drivers.addElement(driver);
}

/*************************************************************************/

/**
  * This method de-registers a driver from the manager.
  *
  * @param driver The <code>Driver</code> to unregister.
237 238
  *
  * @exception SQLException If an error occurs.
239 240
  */
public static void
241
deregisterDriver(Driver driver) throws SQLException
242 243 244 245 246 247 248 249
{
  if (drivers.contains(driver))
    drivers.removeElement(driver);
}

/*************************************************************************/

/**
250 251
  * This method returns a list of all the currently registered JDBC drivers
  * that were loaded by the current <code>ClassLoader</code>.
252 253 254 255 256 257
  *
  * @return An <code>Enumeration</code> of all currently loaded JDBC drivers.
  */
public static Enumeration
getDrivers()
{
258 259 260 261 262 263 264 265 266
  Vector v = new Vector();
  Enumeration e = drivers.elements();

  // Is this right?
  ClassLoader cl = Thread.currentThread().getContextClassLoader();

  while(e.hasMoreElements())
    {
      Object obj = e.nextElement();
267 268 269 270 271 272 273

      ClassLoader loader = obj.getClass().getClassLoader();

      if (loader == null)
	loader = ClassLoader.getSystemClassLoader();
      if (!loader.equals(cl))
	continue;
274 275 276 277 278

      v.addElement(obj);
    } 

  return(v.elements());
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
}

/*************************************************************************/

/**
  * This method returns a driver that can connect to the specified
  * JDBC URL string.  This will be selected from among drivers loaded
  * at initialization time and those drivers manually loaded by the
  * same class loader as the caller.
  *
  * @param url The JDBC URL string to find a driver for.
  *
  * @return A <code>Driver</code> that can connect to the specified
  * URL, or <code>null</code> if a suitable driver cannot be found.
  *
  * @exception SQLException If an error occurs.
  */
public static Driver
getDriver(String url) throws SQLException
{
  // FIXME: Limit driver search to the appropriate subset of loaded drivers.

  Enumeration e = drivers.elements();
  while(e.hasMoreElements())
    {
      Driver d = (Driver)e.nextElement();
      if (d.acceptsURL(url))
        return(d);
    }

  return(null);
}

/*************************************************************************/

/**
  * This method attempts to return a connection to the specified
  * JDBC URL string.
  *
  * @param url The JDBC URL string to connect to.
  *
  * @return A <code>Connection</code> to that URL.
  *
  * @exception SQLException If an error occurs.
  */
public static Connection
getConnection(String url) throws SQLException
{
  return(getConnection(url, new Properties()));
}

/*************************************************************************/

/**
  * This method attempts to return a connection to the specified
  * JDBC URL string using the specified username and password.
  *
  * @param url The JDBC URL string to connect to.
  * @param user The username to connect with.
  * @param password The password to connect with.
  *
  * @return A <code>Connection</code> to that URL.
  *
  * @exception SQLException If an error occurs.
  */
public static Connection
getConnection(String url, String user, String password) throws SQLException
{
  Properties p = new Properties();

349 350 351 352
  if (user != null)
    p.setProperty("user", user);
  if (password != null)
    p.setProperty("password", password);
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

  return(getConnection(url, p));
}

/*************************************************************************/

/**
  * This method attempts to return a connection to the specified
  * JDBC URL string using the specified connection properties.
  *
  * @param url The JDBC URL string to connect to.
  * @param properties The connection properties.
  *
  * @return A <code>Connection</code> to that URL.
  *
  * @exception SQLException If an error occurs.
  */
public static Connection
getConnection(String url, Properties properties) throws SQLException
{
  Driver d = getDriver(url);
  if (d == null)
    throw new SQLException("Driver not found for URL: " + url);

  return(d.connect(url, properties));
}

/*************************************************************************/

/*
 * Constructors
 */

// Keep bozos from trying to instantiate us.
private
DriverManager()
{
  ;
}

} // class DriverManager