1 54 55 package org.tigris.scarab.migration; 56 57 import java.sql.Connection ; 58 import java.sql.DatabaseMetaData ; 59 import java.sql.Driver ; 60 import java.sql.SQLException ; 61 import java.util.Hashtable ; 62 import java.util.Properties ; 63 import org.apache.tools.ant.AntClassLoader; 64 import org.apache.tools.ant.BuildException; 65 import org.apache.tools.ant.Project; 66 import org.apache.tools.ant.Task; 67 import org.apache.tools.ant.types.Path; 68 import org.apache.tools.ant.types.Reference; 69 70 75 public abstract class JDBCTask extends Task { 76 77 78 83 private static Hashtable loaderMap = new Hashtable (3); 84 85 private boolean caching = true; 86 87 private Path classpath; 88 89 private AntClassLoader loader; 90 91 94 private boolean autocommit = false; 95 96 99 private String driver = null; 100 101 104 private String url = null; 105 106 109 private String userId = null; 110 111 114 private String password = null; 115 116 119 private String rdbms = null; 120 121 124 private String version = null; 125 126 130 public void setClasspath(Path classpath) { 131 this.classpath = classpath; 132 } 133 134 140 public void setCaching(boolean enable) { 141 caching = enable; 142 } 143 144 147 public Path createClasspath() { 148 if (this.classpath == null) { 149 this.classpath = new Path(getProject()); 150 } 151 return this.classpath.createPath(); 152 } 153 154 158 public void setClasspathRef(Reference r) { 159 createClasspath().setRefid(r); 160 } 161 162 166 public void setDriver(String driver) { 167 this.driver = driver; 168 } 169 170 174 public void setUrl(String url) { 175 this.url = url; 176 } 177 178 182 public void setPassword(String password) { 183 this.password = password; 184 } 185 186 191 public void setAutocommit(boolean autocommit) { 192 this.autocommit = autocommit; 193 } 194 195 200 public void setRdbms(String rdbms) { 201 this.rdbms = rdbms; 202 } 203 204 209 public void setVersion(String version) { 210 this.version = version; 211 } 212 213 216 protected boolean isValidRdbms(Connection conn) { 217 if (rdbms == null && version == null) { 218 return true; 219 } 220 221 try { 222 DatabaseMetaData dmd = conn.getMetaData(); 223 224 if (rdbms != null) { 225 String theVendor = dmd.getDatabaseProductName().toLowerCase(); 226 227 log("RDBMS = " + theVendor, Project.MSG_VERBOSE); 228 if (theVendor == null || theVendor.indexOf(rdbms) < 0) { 229 log("Not the required RDBMS: " + rdbms, Project.MSG_VERBOSE); 230 return false; 231 } 232 } 233 234 if (version != null) { 235 String theVersion = dmd.getDatabaseProductVersion().toLowerCase(); 237 238 log("Version = " + theVersion, Project.MSG_VERBOSE); 239 if (theVersion == null 240 || !(theVersion.startsWith(version) || theVersion.indexOf(" " + version) >= 0)) { 241 log("Not the required version: \"" + version + "\"", Project.MSG_VERBOSE); 242 return false; 243 } 244 } 245 } catch (SQLException e) { 246 log("Failed to obtain required RDBMS information", Project.MSG_ERR); 248 return false; 249 } 250 251 return true; 252 } 253 254 protected static Hashtable getLoaderMap() { 255 return loaderMap; 256 } 257 258 protected AntClassLoader getLoader() { 259 return loader; 260 } 261 262 268 protected Connection getConnection() throws BuildException { 269 if (userId == null) { 270 throw new BuildException("User Id attribute must be set!", getLocation()); 271 } 272 if (password == null) { 273 throw new BuildException("Password attribute must be set!", getLocation()); 274 } 275 if (url == null) { 276 throw new BuildException("Url attribute must be set!", getLocation()); 277 } 278 try { 279 280 log("connecting to " + getUrl(), Project.MSG_VERBOSE); 281 Properties info = new Properties (); 282 info.put("user", getUserId()); 283 info.put("password", getPassword()); 284 Connection conn = getDriver().connect(getUrl(), info); 285 286 if (conn == null) { 287 throw new SQLException ("No suitable Driver for " + url); 289 } 290 291 conn.setAutoCommit(autocommit); 292 return conn; 293 } catch (SQLException e) { 294 throw new BuildException(e, getLocation()); 295 } 296 297 } 298 299 305 private Driver getDriver() throws BuildException { 306 if (driver == null) { 307 throw new BuildException("Driver attribute must be set!", getLocation()); 308 } 309 310 Driver driverInstance = null; 311 try { 312 Class dc; 313 if (classpath != null) { 314 synchronized (loaderMap) { 321 if (caching) { 322 loader = (AntClassLoader) loaderMap.get(driver); 323 } 324 if (loader == null) { 325 log( 326 "Loading " + driver + " using AntClassLoader with classpath " + classpath, 327 Project.MSG_VERBOSE); 328 loader = new AntClassLoader(project, classpath); 331 if (caching) { 332 loaderMap.put(driver, loader); 333 } 334 } else { 335 log( 336 "Loading " + driver + " using a cached AntClassLoader.", 337 Project.MSG_VERBOSE); 338 } 339 } 340 dc = loader.loadClass(driver); 341 } else { 342 log("Loading " + driver + " using system loader.", Project.MSG_VERBOSE); 343 dc = Class.forName(driver); 344 } 345 driverInstance = (Driver ) dc.newInstance(); 346 } catch (ClassNotFoundException e) { 347 throw new BuildException( 348 "Class Not Found: JDBC driver " + driver + " could not be loaded", 349 getLocation()); 350 } catch (IllegalAccessException e) { 351 throw new BuildException( 352 "Illegal Access: JDBC driver " + driver + " could not be loaded", 353 getLocation()); 354 } catch (InstantiationException e) { 355 throw new BuildException( 356 "Instantiation Exception: JDBC driver " + driver + " could not be loaded", 357 getLocation()); 358 } 359 return driverInstance; 360 } 361 362 363 public void isCaching(boolean value) { 364 caching = value; 365 } 366 367 371 public Path getClasspath() { 372 return classpath; 373 } 374 375 379 public boolean isAutocommit() { 380 return autocommit; 381 } 382 383 387 public String getUrl() { 388 return url; 389 } 390 391 395 public String getUserId() { 396 return userId; 397 } 398 399 403 public void setUserid(String userId) { 404 this.userId = userId; 405 } 406 407 411 public String getPassword() { 412 return password; 413 } 414 415 419 public String getRdbms() { 420 return rdbms; 421 } 422 423 427 public String getVersion() { 428 return version; 429 } 430 431 } 432 | Popular Tags |