1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.sql.Connection ; 22 import java.sql.DatabaseMetaData ; 23 import java.sql.Driver ; 24 import java.sql.SQLException ; 25 import java.util.Hashtable ; 26 import java.util.Properties ; 27 import java.util.Locale ; 28 29 import org.apache.tools.ant.AntClassLoader; 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.Task; 33 import org.apache.tools.ant.types.Path; 34 import org.apache.tools.ant.types.Reference; 35 36 93 94 public abstract class JDBCTask extends Task { 95 96 97 102 private static Hashtable loaderMap = new Hashtable (3); 103 104 private boolean caching = true; 105 106 private Path classpath; 107 108 private AntClassLoader loader; 109 110 113 private boolean autocommit = false; 114 115 118 private String driver = null; 119 120 123 private String url = null; 124 125 128 private String userId = null; 129 130 133 private String password = null; 134 135 138 private String rdbms = null; 139 140 143 private String version = null; 144 145 149 public void setClasspath(Path classpath) { 150 this.classpath = classpath; 151 } 152 153 159 public void setCaching(boolean enable) { 160 caching = enable; 161 } 162 163 167 public Path createClasspath() { 168 if (this.classpath == null) { 169 this.classpath = new Path(getProject()); 170 } 171 return this.classpath.createPath(); 172 } 173 174 179 public void setClasspathRef(Reference r) { 180 createClasspath().setRefid(r); 181 } 182 183 187 public void setDriver(String driver) { 188 this.driver = driver.trim(); 189 } 190 191 195 public void setUrl(String url) { 196 this.url = url; 197 } 198 199 203 public void setPassword(String password) { 204 this.password = password; 205 } 206 207 212 public void setAutocommit(boolean autocommit) { 213 this.autocommit = autocommit; 214 } 215 216 221 public void setRdbms(String rdbms) { 222 this.rdbms = rdbms; 223 } 224 225 230 public void setVersion(String version) { 231 this.version = version; 232 } 233 234 239 protected boolean isValidRdbms(Connection conn) { 240 if (rdbms == null && version == null) { 241 return true; 242 } 243 244 try { 245 DatabaseMetaData dmd = conn.getMetaData(); 246 247 if (rdbms != null) { 248 String theVendor = dmd.getDatabaseProductName().toLowerCase(); 249 250 log("RDBMS = " + theVendor, Project.MSG_VERBOSE); 251 if (theVendor == null || theVendor.indexOf(rdbms) < 0) { 252 log("Not the required RDBMS: " + rdbms, Project.MSG_VERBOSE); 253 return false; 254 } 255 } 256 257 if (version != null) { 258 String theVersion = dmd.getDatabaseProductVersion().toLowerCase(Locale.ENGLISH); 259 260 log("Version = " + theVersion, Project.MSG_VERBOSE); 261 if (theVersion == null 262 || !(theVersion.startsWith(version) 263 || theVersion.indexOf(" " + version) >= 0)) { 264 log("Not the required version: \"" + version + "\"", Project.MSG_VERBOSE); 265 return false; 266 } 267 } 268 } catch (SQLException e) { 269 log("Failed to obtain required RDBMS information", Project.MSG_ERR); 271 return false; 272 } 273 274 return true; 275 } 276 277 281 protected static Hashtable getLoaderMap() { 282 return loaderMap; 283 } 284 285 289 protected AntClassLoader getLoader() { 290 return loader; 291 } 292 293 303 protected Connection getConnection() throws BuildException { 304 if (userId == null) { 305 throw new BuildException("UserId attribute must be set!", getLocation()); 306 } 307 if (password == null) { 308 throw new BuildException("Password attribute must be set!", getLocation()); 309 } 310 if (url == null) { 311 throw new BuildException("Url attribute must be set!", getLocation()); 312 } 313 try { 314 315 log("connecting to " + getUrl(), Project.MSG_VERBOSE); 316 Properties info = new Properties (); 317 info.put("user", getUserId()); 318 info.put("password", getPassword()); 319 Connection conn = getDriver().connect(getUrl(), info); 320 321 if (conn == null) { 322 throw new SQLException ("No suitable Driver for " + url); 324 } 325 326 conn.setAutoCommit(autocommit); 327 return conn; 328 } catch (SQLException e) { 329 throw new BuildException(e, getLocation()); 330 } 331 332 } 333 334 340 private Driver getDriver() throws BuildException { 341 if (driver == null) { 342 throw new BuildException("Driver attribute must be set!", getLocation()); 343 } 344 345 Driver driverInstance = null; 346 try { 347 Class dc; 348 if (classpath != null) { 349 synchronized (loaderMap) { 356 if (caching) { 357 loader = (AntClassLoader) loaderMap.get(driver); 358 } 359 if (loader == null) { 360 log("Loading " + driver 361 + " using AntClassLoader with classpath " 362 + classpath, Project.MSG_VERBOSE); 363 loader = getProject().createClassLoader(classpath); 364 if (caching) { 365 loaderMap.put(driver, loader); 366 } 367 } else { 368 log("Loading " + driver 369 + " using a cached AntClassLoader.", 370 Project.MSG_VERBOSE); 371 } 372 } 373 dc = loader.loadClass(driver); 374 } else { 375 log("Loading " + driver + " using system loader.", 376 Project.MSG_VERBOSE); 377 dc = Class.forName(driver); 378 } 379 driverInstance = (Driver ) dc.newInstance(); 380 } catch (ClassNotFoundException e) { 381 throw new BuildException( 382 "Class Not Found: JDBC driver " + driver + " could not be loaded", 383 e, 384 getLocation()); 385 } catch (IllegalAccessException e) { 386 throw new BuildException( 387 "Illegal Access: JDBC driver " + driver + " could not be loaded", 388 e, 389 getLocation()); 390 } catch (InstantiationException e) { 391 throw new BuildException( 392 "Instantiation Exception: JDBC driver " + driver + " could not be loaded", 393 e, 394 getLocation()); 395 } 396 return driverInstance; 397 } 398 399 400 404 public void isCaching(boolean value) { 405 caching = value; 406 } 407 408 412 public Path getClasspath() { 413 return classpath; 414 } 415 416 420 public boolean isAutocommit() { 421 return autocommit; 422 } 423 424 428 public String getUrl() { 429 return url; 430 } 431 432 436 public String getUserId() { 437 return userId; 438 } 439 440 444 public void setUserid(String userId) { 445 this.userId = userId; 446 } 447 448 452 public String getPassword() { 453 return password; 454 } 455 456 460 public String getRdbms() { 461 return rdbms; 462 } 463 464 468 public String getVersion() { 469 return version; 470 } 471 472 } 473 | Popular Tags |