1 24 25 package org.objectweb.cjdbc.controller.connection; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.net.URL ; 30 import java.sql.Connection ; 31 import java.sql.Driver ; 32 import java.sql.SQLException ; 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.Map ; 36 import java.util.Set ; 37 38 import org.objectweb.cjdbc.common.i18n.Translate; 39 import org.objectweb.cjdbc.common.log.Trace; 40 import org.objectweb.cjdbc.controller.core.Controller; 41 import org.objectweb.cjdbc.controller.core.ControllerConstants; 42 43 52 public class DriverManager 53 { 54 55 56 static Trace logger = Trace 57 .getLogger("org.objectweb.cjdbc.controller.connection.DriverManager"); 58 59 62 private static Set defaultDrivers = new HashSet (); 63 64 68 private static Map namedDrivers = new HashMap (); 69 70 85 public static Connection getConnection(String url, String user, 86 String password, String driverPathName, String driverClassName) 87 throws SQLException 88 { 89 Driver driver = null; 90 boolean isDefaultPath = false; 91 92 if (driverPathName == null) 93 { 94 driver = (Driver ) namedDrivers.get(driverClassName); 97 if (driver == null) 98 { 99 try 102 { 103 if (driverClassName != null) 104 { 105 loadDriverClass(driverClassName); 106 } 107 return java.sql.DriverManager.getConnection(url, user, password); 108 } 109 catch (ClassNotFoundException e) 110 { 111 if (driverClassName == null) 112 { 113 throw new SQLException ( 114 "could not load driver as no class name is specified "); 115 } 116 try 117 { 118 driverPathName = getDriversDir().getAbsolutePath(); 119 isDefaultPath = true; 120 } 121 catch (IOException ioExc) 122 { 123 throw new SQLException ("could not find default drivers directory"); 124 } 125 } 126 } 127 } 128 129 if (driver == null) 130 { 131 driver = (Driver ) namedDrivers.get(driverPathName); 133 } 134 135 if (driver == null) 136 { 137 try 139 { 140 File path = convertToAbsolutePath(driverPathName); 141 if (logger.isDebugEnabled()) 143 { 144 logger.debug("loading driver with name " + driverPathName 145 + " for class " + driverClassName); 146 } 147 driver = loadDriver(path, driverClassName); 148 } 149 catch (Exception e) 150 { 151 logger.error("Could not load driver for class " + driverClassName, e); 152 throw new SQLException ("could not load driver for class name " 153 + driverClassName + " and driverPath " + driverPathName); 154 } 155 156 if (isDefaultPath) 159 { namedDrivers.put(driverClassName, driver); 161 } 162 else 163 { 164 namedDrivers.put(driverPathName, driver); 166 } 167 } 168 169 return getConnectionForDriver(url, user, password, driver); 170 } 171 172 178 public static void loadDriverClass(String driverClassName) 179 throws ClassNotFoundException 180 { 181 if (!defaultDrivers.contains(driverClassName)) 182 { 183 if (logger.isDebugEnabled()) 184 { 185 logger.debug("we are using default classloader and driverClassName =" 186 + driverClassName); 187 } 188 Class.forName(driverClassName); 189 if (logger.isDebugEnabled()) 190 logger.debug(Translate.get("backend.driver.loaded", driverClassName)); 191 defaultDrivers.add(driverClassName); 193 } 194 } 195 196 205 public static File convertToAbsolutePath(String pathName) throws IOException 206 { 207 File dir = null; 208 209 if (pathName != null) 210 { 211 File path = new File (pathName); 212 if (path.canRead()) 213 return path; 214 else 215 throw new IOException ("Invalid path name " + pathName); 216 } 217 else 218 { 219 dir = getDriversDir(); 220 } 221 222 if (!dir.canRead()) 223 { 224 String msg = Translate.get("controller.driver.dir.not.found"); 225 logger.error(msg); 226 throw new IOException (msg); 227 } 228 229 return dir; 230 } 231 232 private static File getDriversDir() throws IOException 233 { 234 URL url = Controller.class 235 .getResource(ControllerConstants.C_JDBC_DRIVER_JAR_FILE); 236 if (url == null) 237 { 238 String msg = Translate.get("controller.driver.dir.not.found"); 239 logger.error(msg); 240 throw new IOException (msg); 241 } 242 243 File driversDir = new File (url.getFile()).getParentFile(); 244 245 if (!driversDir.exists()) 246 { 247 String msg = Translate.get("controller.driver.dir.not.found"); 248 logger.error(msg); 249 throw new IOException (msg); 250 } 251 return driversDir; 252 } 253 254 private static Connection getConnectionForDriver(String url, String user, 255 String password, Driver driver) throws SQLException 256 { 257 java.util.Properties info = new java.util.Properties (); 258 if (user != null) 259 { 260 info.put("user", user); 261 } 262 if (password != null) 263 { 264 info.put("password", password); 265 } 266 267 return driver.connect(url, info); 268 } 269 270 private static Driver loadDriver(File path, String driverClassName) 271 throws ClassNotFoundException , InstantiationException , 272 IllegalAccessException 273 { 274 ClassLoader loader = new DriverClassLoader(null, path); 275 276 Class.forName(java.sql.DriverManager .class.getName(), true, loader); 282 283 Class driverClass = Class.forName(driverClassName, true, loader); 285 286 if (logger.isDebugEnabled()) 287 logger.debug(Translate.get("backend.driver.loaded", driverClassName)); 288 289 return (Driver ) driverClass.newInstance(); 291 292 } 293 294 } 295 | Popular Tags |