00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 public class DriverManager
00053 {
00054
00055
00056 static Trace logger = Trace
00057 .getLogger("org.objectweb.cjdbc.controller.connection.DriverManager");
00058
00059
00060
00061
00062 private static Set defaultDrivers = new HashSet();
00063
00064
00065
00066
00067
00068 private static Map namedDrivers = new HashMap();
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
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
00095
00096 driver = (Driver) namedDrivers.get(driverClassName);
00097 if (driver == null)
00098 {
00099
00100
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
00132 driver = (Driver) namedDrivers.get(driverPathName);
00133 }
00134
00135 if (driver == null)
00136 {
00137
00138 try
00139 {
00140 File path = convertToAbsolutePath(driverPathName);
00141
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
00160
00161 if (isDefaultPath)
00162 {
00163 namedDrivers.put(driverClassName, driver);
00164 }
00165 else
00166 {
00167
00168 namedDrivers.put(driverPathName, driver);
00169 }
00170 }
00171
00172 return getConnectionForDriver(url, user, password, driver);
00173 }
00174
00175
00176
00177
00178
00179
00180
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
00195 defaultDrivers.add(driverClassName);
00196 }
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
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
00273
00274
00275
00276
00277 Class.forName(java.sql.DriverManager.class.getName(), true, loader);
00278
00279
00280 Class driverClass = Class.forName(driverClassName, true, loader);
00281
00282 if (logger.isDebugEnabled())
00283 logger.debug(Translate.get("backend.driver.loaded", driverClassName));
00284
00285
00286 return (Driver) driverClass.newInstance();
00287
00288 }
00289
00290 }