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