Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

org.objectweb.cjdbc.controller.connection.DriverManager Class Reference

List of all members.

Static Public Member Functions

Connection getConnection (String url, String user, String password, String driverPathName, String driverClassName) throws SQLException
void loadDriverClass (String driverClassName) throws ClassNotFoundException
File convertToAbsolutePath (String pathName) throws IOException

Static Package Attributes

Trace logger

Detailed Description

This class defines a DriverManager. In contrast to java.sql.DriverManager this class allows to use Drivers with the same name but with different versions, if no drivername is used it is a wrapper around java.sql.DriverManager.

Author:
Emmanuel Cecchet
Version:
1.0

Definition at line 52 of file DriverManager.java.


Member Function Documentation

File org.objectweb.cjdbc.controller.connection.DriverManager.convertToAbsolutePath String  pathName  )  throws IOException [static]
 

convert a path into an absolute path if the path is already an absolute path, it is just returned otherwise a relative path is considered to be relative to the drivers directory

Parameters:
pathName the relativ or absolute path
Returns:
the converted path
Exceptions:
IOException if the converted path does not exist

Definition at line 208 of file DriverManager.java.

00209   {
00210     File dir = null;
00211 
00212     if (pathName != null)
00213     {
00214       File path = new File(pathName);
00215       if (path.canRead())
00216         return path;
00217       else
00218         throw new IOException("Invalid path name " + pathName);
00219     }
00220     else
00221     {
00222       dir = getDriversDir();
00223     }
00224 
00225     if (!dir.canRead())
00226     {
00227       String msg = Translate.get("controller.driver.dir.not.found");
00228       logger.error(msg);
00229       throw new IOException(msg);
00230     }
00231 
00232     return dir;
00233   }

Connection org.objectweb.cjdbc.controller.connection.DriverManager.getConnection String  url,
String  user,
String  password,
String  driverPathName,
String  driverClassName
throws SQLException [static]
 

Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.

Parameters:
url a database url of the form jdbc:subprotocol:subname
user the database user on whose behalf the connection is being made
password the user's password
driverPathName the path where the driver classes are located, null if default directory
driverClassName the class name of the driver
Returns:
a connection to the URL
Exceptions:
SQLException if a database access error occurs

Definition at line 85 of file DriverManager.java.

00088   {
00089     Driver driver = null;
00090     boolean isDefaultPath = false;
00091 
00092     if (driverPathName == null)
00093     {
00094       // no path specified
00095       // have we already loaded this driver
00096       driver = (Driver) namedDrivers.get(driverClassName);
00097       if (driver == null)
00098       {
00099         // the driver has not yet been loaded
00100         // first we try to load class from classpath
00101         try
00102         {
00103           if (driverClassName != null)
00104           {
00105             loadDriverClass(driverClassName);
00106           }
00107           return java.sql.DriverManager.getConnection(url, user, password);
00108         }
00109         catch (ClassNotFoundException e)
00110         {
00111           if (driverClassName == null)
00112           {
00113             throw new SQLException(
00114                 "could not load driver as no class name is specified ");
00115           }
00116           try
00117           {
00118             driverPathName = getDriversDir().getAbsolutePath();
00119             isDefaultPath = true;
00120           }
00121           catch (IOException ioExc)
00122           {
00123             throw new SQLException("could not find default drivers directory");
00124           }
00125         }
00126       }
00127     }
00128 
00129     if (driver == null)
00130     {
00131       // have we already loaded this named driver ?
00132       driver = (Driver) namedDrivers.get(driverPathName);
00133     }
00134 
00135     if (driver == null)
00136     {
00137       // no driver with this name has been loaded so far
00138       try
00139       {
00140         File path = convertToAbsolutePath(driverPathName);
00141         // we load the driver now
00142         if (logger.isDebugEnabled())
00143         {
00144           logger.debug("loading driver with name " + driverPathName
00145               + " for class " + driverClassName);
00146         }
00147         driver = loadDriver(path, driverClassName);
00148       }
00149       catch (Exception e)
00150       {
00151         if (logger.isDebugEnabled())
00152         {
00153           logger.debug("could not load driver for class " + driverClassName, e);
00154         }
00155         throw new SQLException("could not load driver for class name "
00156             + driverClassName + " and driverPath " + driverPathName);
00157       }
00158 
00159       // driver has been loaded successfully, we cache it for
00160       // further use
00161       if (isDefaultPath)
00162       {// we cache it with the class name
00163         namedDrivers.put(driverClassName, driver);
00164       }
00165       else
00166       {
00167         // we cache it with the pathName
00168         namedDrivers.put(driverPathName, driver);
00169       }
00170     }
00171 
00172     return getConnectionForDriver(url, user, password, driver);
00173   }

void org.objectweb.cjdbc.controller.connection.DriverManager.loadDriverClass String  driverClassName  )  throws ClassNotFoundException [static]
 

Load the driver class

Parameters:
driverClassName the class name of the driver
Exceptions:
ClassNotFoundException if the class could not be found

Definition at line 181 of file DriverManager.java.

00183   {
00184     if (!defaultDrivers.contains(driverClassName))
00185     {
00186       if (logger.isDebugEnabled())
00187       {
00188         logger.debug("we are using default classloader and driverClassName ="
00189             + driverClassName);
00190       }
00191       Class.forName(driverClassName);
00192       if (logger.isDebugEnabled())
00193         logger.debug(Translate.get("backend.driver.loaded", driverClassName));
00194       // the driver was successfully loaded
00195       defaultDrivers.add(driverClassName);
00196     }
00197   }


Member Data Documentation

Trace org.objectweb.cjdbc.controller.connection.DriverManager.logger [static, package]
 

Initial value:

 Trace
                                        .getLogger("org.objectweb.cjdbc.controller.connection.DriverManager")
Logger instance.

Definition at line 56 of file DriverManager.java.


The documentation for this class was generated from the following file:
Generated on Mon Apr 11 22:03:40 2005 for C-JDBC by  doxygen 1.3.9.1