OSDN Git Service

Start of AWT merge with Classpath:
[pf3gnuchains/gcc-fork.git] / libjava / java / sql / DriverManager.java
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 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
26
27
28 package java.sql;
29
30 import java.io.PrintStream;
31 import java.io.PrintWriter;
32 import java.util.Enumeration;
33 import java.util.Properties;
34 import java.util.StringTokenizer;
35 import java.util.Vector;
36
37 /**
38   * This class manages the JDBC drivers in the system. It maintains a
39   * registry of drivers and locates the appropriate driver to handle a
40   * JDBC database URL.
41   * <p>
42   * On startup, <code>DriverManager</code> loads all the managers specified
43   * by the system property <code>jdbc.drivers</code>.  The value of this
44   * property should be a colon separated list of fully qualified driver
45   * class names.  Additional drivers can be loaded at any time by
46   * simply loading the driver class with <code>class.forName(String)</code>.
47   * The driver should automatically register itself in a static 
48   * initializer.
49   * <p>
50   * The methods in this class are all <code>static</code>. This class
51   * cannot be instantiated.
52   *
53   * @author Aaron M. Renn (arenn@urbanophile.com)
54   */
55 public class DriverManager 
56 {
57
58 /*
59  * Class Variables
60  */
61
62 /**
63   * This is the log stream for JDBC drivers.
64   */
65 private static PrintStream log_stream;
66
67 /**
68   * This is the log writer for JDBC drivers.
69   */
70 private static PrintWriter log_writer;
71
72 /**
73   * This is the login timeout used by JDBC drivers.
74   */
75 private static int login_timeout;
76
77 /**
78   * This is the list of JDBC drivers that are loaded.
79   */
80 private static Vector drivers;
81  // Hmm, seems like we might want to do a Hashtable and lookup by something,
82  // but what would it be?
83
84 // Load all drivers on startup
85 static
86 {
87   drivers = new Vector();
88
89   String driver_string = System.getProperty("jdbc.drivers");
90   if (driver_string != null)
91     {
92       StringTokenizer st = new StringTokenizer(driver_string);
93       while (st.hasMoreTokens())
94         {
95           String driver_classname = st.nextToken();
96
97           try
98             {
99               Class.forName(driver_classname); // The driver registers itself
100             }
101           catch (Exception e) { ; } // Ignore not founds
102         }
103     }
104
105 }
106   
107 /*************************************************************************/
108
109 /*
110  * Class Methods
111  */
112
113 /**
114   * This method returns the login timeout in use by JDBC drivers systemwide.
115   *
116   * @return The login timeout.
117   */
118 public static int
119 getLoginTimeout()
120 {
121   return(login_timeout);
122 }
123
124 /*************************************************************************/
125
126 /**
127   * This method set the login timeout used by JDBC drivers.  This is a
128   * system-wide parameter that applies to all drivers.
129   *
130   * @param login_timeout The new login timeout value.
131   */
132 public static void
133 setLoginTimeout(int login_timeout)
134 {
135   DriverManager.login_timeout = login_timeout;
136 }
137
138 /*************************************************************************/
139
140 /**
141   * This method returns the log writer being used by all JDBC drivers.
142   * This method should be used in place of the deprecated
143   * <code>getLogStream</code> method.
144   *
145   * @return The log writer in use by JDBC drivers.
146   */
147 public static PrintWriter
148 getLogWriter()
149 {
150   return(log_writer);
151 }
152
153 /*************************************************************************/
154
155 /**
156   * This method sets the log writer being used by JDBC drivers.  This is a
157   * system-wide parameter that affects all drivers.  Note that since there
158   * is no way to retrieve a <code>PrintStream</code> from a 
159   * <code>PrintWriter</code>, this method cannot set the log stream in
160   * use by JDBC.  Thus any older drivers may not see this setting.
161   *
162   * @param log_writer The new log writer for JDBC.
163   */
164 public static void
165 setLogWriter(PrintWriter log_writer)
166 {
167   DriverManager.log_writer = log_writer;
168 }
169
170 /*************************************************************************/
171
172 /**
173   * This method returns the log stream in use by JDBC.
174   *
175   * @return The log stream in use by JDBC.
176   *
177   * @deprecated Use <code>getLogWriter()</code> instead.
178   */
179 public static PrintStream
180 getLogStream()
181 {
182   return(log_stream);
183 }
184
185 /*************************************************************************/
186
187 /**
188   * This method sets the log stream in use by JDBC.
189   *
190   * @param log_stream The log stream in use by JDBC.
191   *
192   * @deprecated Use <code>setLogWriter</code> instead.
193   */
194 public static void
195 setLogStream(PrintStream log_stream)
196 {
197   DriverManager.log_stream = log_stream;
198 }
199
200 /*************************************************************************/
201
202 /**
203   * This method prints the specified line to the log stream.
204   *
205   * @param str The string to write to the log stream.
206   */
207 public static void
208 println(String str)
209 {
210   if (log_stream != null) // Watch for user not using logging
211     log_stream.println(str);
212 }
213
214 /*************************************************************************/
215
216 /**
217   * This method registers a new driver with the manager.  This is normally
218   * called by the driver itself in a static initializer.
219   *
220   * @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   if (!drivers.contains(driver))
228     drivers.addElement(driver);
229 }
230
231 /*************************************************************************/
232
233 /**
234   * This method de-registers a driver from the manager.
235   *
236   * @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   if (drivers.contains(driver))
244     drivers.removeElement(driver);
245 }
246
247 /*************************************************************************/
248
249 /**
250   * This method returns a list of all the currently registered JDBC drivers
251   * that were loaded by the current <code>ClassLoader</code>.
252   *
253   * @return An <code>Enumeration</code> of all currently loaded JDBC drivers.
254   */
255 public static Enumeration
256 getDrivers()
257 {
258   Vector v = new Vector();
259   Enumeration e = drivers.elements();
260
261   // Is this right?
262   ClassLoader cl = Thread.currentThread().getContextClassLoader();
263
264   while(e.hasMoreElements())
265     {
266       Object obj = e.nextElement();
267
268       ClassLoader loader = obj.getClass().getClassLoader();
269
270       if (loader == null)
271         loader = ClassLoader.getSystemClassLoader();
272       if (!loader.equals(cl))
273         continue;
274
275       v.addElement(obj);
276     } 
277
278   return(v.elements());
279 }
280
281 /*************************************************************************/
282
283 /**
284   * This method returns a driver that can connect to the specified
285   * JDBC URL string.  This will be selected from among drivers loaded
286   * at initialization time and those drivers manually loaded by the
287   * same class loader as the caller.
288   *
289   * @param url The JDBC URL string to find a driver for.
290   *
291   * @return A <code>Driver</code> that can connect to the specified
292   * URL, or <code>null</code> if a suitable driver cannot be found.
293   *
294   * @exception SQLException If an error occurs.
295   */
296 public static Driver
297 getDriver(String url) throws SQLException
298 {
299   // FIXME: Limit driver search to the appropriate subset of loaded drivers.
300
301   Enumeration e = drivers.elements();
302   while(e.hasMoreElements())
303     {
304       Driver d = (Driver)e.nextElement();
305       if (d.acceptsURL(url))
306         return(d);
307     }
308
309   return(null);
310 }
311
312 /*************************************************************************/
313
314 /**
315   * This method attempts to return a connection to the specified
316   * JDBC URL string.
317   *
318   * @param url The JDBC URL string to connect to.
319   *
320   * @return A <code>Connection</code> to that URL.
321   *
322   * @exception SQLException If an error occurs.
323   */
324 public static Connection
325 getConnection(String url) throws SQLException
326 {
327   return(getConnection(url, new Properties()));
328 }
329
330 /*************************************************************************/
331
332 /**
333   * This method attempts to return a connection to the specified
334   * JDBC URL string using the specified username and password.
335   *
336   * @param url The JDBC URL string to connect to.
337   * @param user The username to connect with.
338   * @param password The password to connect with.
339   *
340   * @return A <code>Connection</code> to that URL.
341   *
342   * @exception SQLException If an error occurs.
343   */
344 public static Connection
345 getConnection(String url, String user, String password) throws SQLException
346 {
347   Properties p = new Properties();
348
349   if (user != null)
350     p.setProperty("user", user);
351   if (password != null)
352     p.setProperty("password", password);
353
354   return(getConnection(url, p));
355 }
356
357 /*************************************************************************/
358
359 /**
360   * This method attempts to return a connection to the specified
361   * JDBC URL string using the specified connection properties.
362   *
363   * @param url The JDBC URL string to connect to.
364   * @param properties The connection properties.
365   *
366   * @return A <code>Connection</code> to that URL.
367   *
368   * @exception SQLException If an error occurs.
369   */
370 public static Connection
371 getConnection(String url, Properties properties) throws SQLException
372 {
373   Driver d = getDriver(url);
374   if (d == null)
375     throw new SQLException("Driver not found for URL: " + url);
376
377   return(d.connect(url, properties));
378 }
379
380 /*************************************************************************/
381
382 /*
383  * Constructors
384  */
385
386 // Keep bozos from trying to instantiate us.
387 private
388 DriverManager()
389 {
390   ;
391 }
392
393 } // class DriverManager 
394