src/org/objectweb/cjdbc/controller/connection/DriverManager.java

説明を見る。
00001 00025 package org.objectweb.cjdbc.controller.connection; 00026 00027 import java.io.File; 00028 import java.io.IOException; 00029 import java.net.URL; 00030 import java.sql.Connection; 00031 import java.sql.Driver; 00032 import java.sql.SQLException; 00033 import java.util.HashMap; 00034 import java.util.HashSet; 00035 import java.util.Map; 00036 import java.util.Set; 00037 00038 import org.objectweb.cjdbc.common.i18n.Translate; 00039 import org.objectweb.cjdbc.common.log.Trace; 00040 import org.objectweb.cjdbc.controller.core.Controller; 00041 import org.objectweb.cjdbc.controller.core.ControllerConstants; 00042 00052 public class DriverManager 00053 { 00054 00056 static Trace logger = Trace 00057 .getLogger("org.objectweb.cjdbc.controller.connection.DriverManager"); 00058 00062 private static Set defaultDrivers = new HashSet(); 00063 00068 private static Map namedDrivers = new HashMap(); 00069 00085 public static Connection getConnection(String url, String user, 00086 String password, String driverPathName, String driverClassName) 00087 throws SQLException 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 } 00174 00181 public static void loadDriverClass(String driverClassName) 00182 throws ClassNotFoundException 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 } 00198 00208 public static File convertToAbsolutePath(String pathName) throws IOException 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 } 00234 00235 private static File getDriversDir() throws IOException 00236 { 00237 URL url = Controller.class 00238 .getResource(ControllerConstants.C_JDBC_DRIVER_JAR_FILE); 00239 File driversDir = new File(url.getFile()).getParentFile(); 00240 00241 if (!driversDir.exists()) 00242 { 00243 String msg = Translate.get("controller.driver.dir.not.found"); 00244 logger.error(msg); 00245 throw new IOException(msg); 00246 } 00247 return driversDir; 00248 } 00249 00250 private static Connection getConnectionForDriver(String url, String user, 00251 String password, Driver driver) throws SQLException 00252 { 00253 java.util.Properties info = new java.util.Properties(); 00254 if (user != null) 00255 { 00256 info.put("user", user); 00257 } 00258 if (password != null) 00259 { 00260 info.put("password", password); 00261 } 00262 00263 return driver.connect(url, info); 00264 } 00265 00266 private static Driver loadDriver(File path, String driverClassName) 00267 throws ClassNotFoundException, InstantiationException, 00268 IllegalAccessException 00269 { 00270 ClassLoader loader = new DriverClassLoader(null, path); 00271 00272 // load java.sql.DriverManager with the new classloader 00273 // Driver instances register with the DriverManager and we want the new 00274 // Driver to register with its own DriverManager 00275 // Otherwise the new Driver would register with the default DriverManager 00276 // and possibly overwrite an other driver with the same name 00277 Class.forName(java.sql.DriverManager.class.getName(), true, loader); 00278 00279 // load class 00280 Class driverClass = Class.forName(driverClassName, true, loader); 00281 00282 if (logger.isDebugEnabled()) 00283 logger.debug(Translate.get("backend.driver.loaded", driverClassName)); 00284 00285 // get an instance of the class and return it 00286 return (Driver) driverClass.newInstance(); 00287 00288 } 00289 00290 }

CJDBCversion1.0.4に対してTue Oct 12 15:16:01 2004に生成されました。 doxygen 1.3.8