/[classpath]/classpath/java/sql/DriverManager.java
ViewVC logotype

Diff of /classpath/java/sql/DriverManager.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by ericb, Fri Mar 22 21:25:20 2002 UTC revision 1.8 by bryce, Fri Jun 21 05:34:12 2002 UTC
# Line 1  Line 1 
1  /* DriverManager.java -- Manage JDBC drivers  /* DriverManager.java -- Manage JDBC drivers
2     Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.     Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 35  this exception to your version of the li Line 35  this exception to your version of the li
35  obligated to do so.  If you do not wish to do so, delete this  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */  exception statement from your version. */
37    
   
38  package java.sql;  package java.sql;
39    
40  import java.io.PrintStream;  import java.io.PrintStream;
41  import java.io.PrintWriter;  import java.io.PrintWriter;
42  import java.util.Enumeration;  import java.util.Enumeration;
43  import java.util.Properties;  import java.util.Properties;
 import java.util.StringTokenizer;  
44  import java.util.Vector;  import java.util.Vector;
45    import java.util.StringTokenizer;
46    
47  /**  /**
48    * This class manages the JDBC drivers in the system. It maintains a    * This class manages the JDBC drivers in the system. It maintains a
# Line 65  import java.util.Vector; Line 64  import java.util.Vector;
64    */    */
65  public class DriverManager  public class DriverManager
66  {  {
67      /**
68  /*     * This is the log stream for JDBC drivers.
69   * Class Variables     */
70   */    private static PrintStream log_stream;
71    
72  /**    /**
73    * This is the log stream for JDBC drivers.     * This is the log writer for JDBC drivers.
74    */     */
75  private static PrintStream log_stream;    private static PrintWriter log_writer;
76    
77  /**    /**
78    * This is the log writer for JDBC drivers.     * This is the login timeout used by JDBC drivers.
79    */     */
80  private static PrintWriter log_writer;    private static int login_timeout;
81    
82  /**    /**
83    * This is the login timeout used by JDBC drivers.     * This is the list of JDBC drivers that are loaded.
84    */     */
85  private static int login_timeout;    private static Vector drivers;
86       // Hmm, seems like we might want to do a Hashtable and lookup by something,
87  /**     // but what would it be?
88    * This is the list of JDBC drivers that are loaded.  
89    */    // Load all drivers on startup
90  private static Vector drivers;    static
91   // Hmm, seems like we might want to do a Hashtable and lookup by something,    {
92   // but what would it be?      drivers = new Vector();
93    
94  // Load all drivers on startup      String driver_string = System.getProperty("jdbc.drivers");
95  static      if (driver_string != null)
96  {        {
97    drivers = new Vector();          StringTokenizer st = new StringTokenizer(driver_string);
98            while (st.hasMoreTokens())
99    String driver_string = System.getProperty("jdbc.drivers");            {
100    if (driver_string != null)              String driver_classname = st.nextToken();
101      {  
102        StringTokenizer st = new StringTokenizer(driver_string);              try
103        while (st.hasMoreTokens())                {
104          {                  Class.forName(driver_classname); // The driver registers itself
105            String driver_classname = st.nextToken();                }
106                catch (Exception e) { ; } // Ignore not founds
107            try            }
108              {        }
109                Class.forName(driver_classname); // The driver registers itself  
110              }    }
111            catch (Exception e) { ; } // Ignore not founds  
112          }    /** Can't be instantiated. */
113      }    private DriverManager()
114      {
115  }    }
116    
117      /**
118       * This method returns the log writer being used by all JDBC drivers.
119       * This method should be used in place of the deprecated
120       * <code>getLogStream</code> method.
121       *
122       * @return The log writer in use by JDBC drivers.
123       */
124      public static PrintWriter getLogWriter()
125      {
126        return log_writer;
127      }
128        
129  /*************************************************************************/    /**
130       * This method sets the log writer being used by JDBC drivers.  This is a
131  /*     * system-wide parameter that affects all drivers.  Note that since there
132   * Class Methods     * is no way to retrieve a <code>PrintStream</code> from a
133   */     * <code>PrintWriter</code>, this method cannot set the log stream in
134       * use by JDBC.  Thus any older drivers may not see this setting.
135  /**     *
136    * This method returns the login timeout in use by JDBC drivers systemwide.     * @param out The new log writer for JDBC.
137    *     */
138    * @return The login timeout.    public static void setLogWriter(PrintWriter out)
139    */    {
140  public static int      DriverManager.log_writer = out;
141  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;  
 }  
   
 /*************************************************************************/  
   
 /**  
   * 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;  
 }  
   
 /*************************************************************************/  
   
 /**  
   * 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.  
   *  
   * @exception SQLException If an error occurs.  
   */  
 public static void  
 registerDriver(Driver driver) throws SQLException  
 {  
   if (!drivers.contains(driver))  
     drivers.addElement(driver);  
 }  
   
 /*************************************************************************/  
   
 /**  
   * This method de-registers a driver from the manager.  
   *  
   * @param driver The <code>Driver</code> to unregister.  
   *  
   * @exception SQLException If an error occurs.  
   */  
 public static void  
 deregisterDriver(Driver driver) throws SQLException  
 {  
   if (drivers.contains(driver))  
     drivers.removeElement(driver);  
 }  
   
 /*************************************************************************/  
   
 /**  
   * This method returns a list of all the currently registered JDBC drivers  
   * that were loaded by the current <code>ClassLoader</code>.  
   *  
   * @return An <code>Enumeration</code> of all currently loaded JDBC drivers.  
   */  
 public static Enumeration  
 getDrivers()  
 {  
   Vector v = new Vector();  
   Enumeration e = drivers.elements();  
   
   // Is this right?  
   ClassLoader cl = Thread.currentThread().getContextClassLoader();  
   
   while(e.hasMoreElements())  
     {  
       Object obj = e.nextElement();  
   
       ClassLoader loader = obj.getClass().getClassLoader();  
   
       if (loader == null)  
         loader = ClassLoader.getSystemClassLoader();  
       if (!loader.equals(cl))  
         continue;  
   
       v.addElement(obj);  
     }  
   
   return(v.elements());  
 }  
   
 /*************************************************************************/  
   
 /**  
   * 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);  
     }  
   throw new SQLException("No driver for " + url);  
 }  
   
 /*************************************************************************/  
142    
143  /**  /**
144    * This method attempts to return a connection to the specified    * This method attempts to return a connection to the specified
145    * JDBC URL string.    * JDBC URL string using the specified connection properties.
   *  
   * @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.  
146    *    *
147    * @param url The JDBC URL string to connect to.    * @param url The JDBC URL string to connect to.
148    * @param user The username to connect with.    * @param properties The connection properties.
   * @param password The password to connect with.  
149    *    *
150    * @return A <code>Connection</code> to that URL.    * @return A <code>Connection</code> to that URL.
151    *    *
152    * @exception SQLException If an error occurs.    * @exception SQLException If an error occurs.
153    */    */
154  public static Connection    public static Connection getConnection(String url, Properties properties)
155  getConnection(String url, String user, String password) throws SQLException      throws SQLException
156  {    {
157    Properties p = new Properties();      Driver d = getDriver(url);
158        if (d == null)
159    if (user != null)        throw new SQLException("Driver not found for URL: " + url);
160      p.setProperty("user", user);  
161    if (password != null)      return d.connect(url, properties);
162      p.setProperty("password", password);    }
163    
164    return(getConnection(url, p));  
165  }    /**
166       * This method attempts to return a connection to the specified
167  /*************************************************************************/     * JDBC URL string using the specified username and password.
168       *
169       * @param url The JDBC URL string to connect to.
170       * @param user The username to connect with.
171       * @param password The password to connect with.
172       * @return A <code>Connection</code> to that URL.
173       * @exception SQLException If an error occurs.
174       */
175      public static Connection getConnection(String url, String user,
176        String password) throws SQLException
177      {
178        Properties p = new Properties();
179    
180        if (user != null)
181          p.setProperty("user", user);
182        if (password != null)
183          p.setProperty("password", password);
184    
185        return getConnection(url, p);
186      }
187    
188      /**
189       * This method attempts to return a connection to the specified
190       * JDBC URL string.
191       *
192       * @param url The JDBC URL string to connect to.
193       *
194       * @return A <code>Connection</code> to that URL.
195       *
196       * @exception SQLException If an error occurs.
197       */
198      public static Connection getConnection(String url) throws SQLException
199      {
200        return getConnection(url, new Properties());
201      }
202    
203      /**
204       * This method returns a driver that can connect to the specified
205       * JDBC URL string.  This will be selected from among drivers loaded
206       * at initialization time and those drivers manually loaded by the
207       * same class loader as the caller.
208       *
209       * @param url The JDBC URL string to find a driver for.
210       *
211       * @return A <code>Driver</code> that can connect to the specified
212       * URL, or <code>null</code> if a suitable driver cannot be found.
213       *
214       * @exception SQLException If an error occurs.
215       */
216      public static Driver getDriver(String url) throws SQLException
217      {
218        // FIXME: Limit driver search to the appropriate subset of loaded drivers.
219        Enumeration e = drivers.elements();
220        while(e.hasMoreElements())
221          {
222            Driver d = (Driver)e.nextElement();
223            if (d.acceptsURL(url))
224              return d;
225          }
226    
227        return null;
228      }
229    
230      /**
231       * This method registers a new driver with the manager.  This is normally
232       * called by the driver itself in a static initializer.
233       *
234       * @param driver The new <code>Driver</code> to add.
235       *
236       * @exception SQLException If an error occurs.
237       */
238      public static void registerDriver(Driver driver) throws SQLException
239      {
240        if (! drivers.contains(driver))
241          drivers.addElement(driver);  
242      }
243    
244  /**  /**
245    * This method attempts to return a connection to the specified    * This method de-registers a driver from the manager.
   * JDBC URL string using the specified connection properties.  
   *  
   * @param url The JDBC URL string to connect to.  
   * @param properties The connection properties.  
246    *    *
247    * @return A <code>Connection</code> to that URL.    * @param driver The <code>Driver</code> to unregister.
248    *    *
249    * @exception SQLException If an error occurs.    * @exception SQLException If an error occurs.
250    */    */
251  public static Connection    public static void deregisterDriver(Driver driver) throws SQLException
252  getConnection(String url, Properties properties) throws SQLException    {
253  {      if (drivers.contains(driver))
254    Driver d = getDriver(url);        drivers.removeElement(driver);
255    if (d == null)    }
256      throw new SQLException("Driver not found for URL: " + url);  
257      /**
258    return(d.connect(url, properties));     * This method returns a list of all the currently registered JDBC drivers
259       * that were loaded by the current <code>ClassLoader</code>.
260       *
261       * @return An <code>Enumeration</code> of all currently loaded JDBC drivers.
262       */
263      public static Enumeration getDrivers()
264      {
265        Vector v = new Vector();
266        Enumeration e = drivers.elements();
267    
268        // Is this right?
269        ClassLoader cl = Thread.currentThread().getContextClassLoader();
270    
271        while(e.hasMoreElements())
272          {
273            Object obj = e.nextElement();
274    
275            ClassLoader loader = obj.getClass().getClassLoader();
276    
277            if (loader == null)
278              loader = ClassLoader.getSystemClassLoader();
279            if (! loader.equals(cl))
280              continue;
281    
282            v.addElement(obj);
283          }
284    
285        return v.elements();
286      }
287    
288      /**
289       * This method set the login timeout used by JDBC drivers.  This is a
290       * system-wide parameter that applies to all drivers.
291       *
292       * @param login_timeout The new login timeout value.
293       */
294      public static void setLoginTimeout(int seconds)
295      {
296        DriverManager.login_timeout = login_timeout;  
297      }
298    
299      /**
300       * This method returns the login timeout in use by JDBC drivers systemwide.
301       *
302       * @return The login timeout.
303       */
304      public static int getLoginTimeout()
305      {
306        return login_timeout;
307      }
308    
309      /**
310       * This method sets the log stream in use by JDBC.
311       *
312       * @param log_stream The log stream in use by JDBC.
313       *
314       * @deprecated Use <code>setLogWriter</code> instead.
315       */
316      public static void setLogStream(PrintStream out)
317      {
318        DriverManager.log_stream = log_stream;
319      }
320    
321      /**
322       * This method returns the log stream in use by JDBC.
323       *
324       * @return The log stream in use by JDBC.
325       *
326       * @deprecated Use <code>getLogWriter()</code> instead.
327       */
328      public static PrintStream getLogStream()
329      {
330        return log_stream;
331      }
332    
333      /**
334       * This method prints the specified line to the log stream.
335       *
336       * @param str The string to write to the log stream.
337       */
338      public static void println(String message)
339      {
340        if (log_stream != null) // Watch for user not using logging
341          log_stream.println(message);
342      }
343  }  }
   
 /*************************************************************************/  
   
 /*  
  * Constructors  
  */  
   
 // Keep bozos from trying to instantiate us.  
 private  
 DriverManager()  
 {  
   ;  
 }  
   
 } // class DriverManager  
   

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26