Methods Summary |
---|
public org.apache.tools.ant.types.Path | createClasspath()Add a path to the classpath for loading the driver.
if (this.classpath == null) {
this.classpath = new Path(getProject());
}
return this.classpath.createPath();
|
public org.apache.tools.ant.types.Path | getClasspath()Gets the classpath.
return classpath;
|
protected java.sql.Connection | getConnection()Creates a new Connection as using the driver, url, userid and password
specified.
The calling method is responsible for closing the connection.
if (userId == null) {
throw new BuildException("UserId attribute must be set!", getLocation());
}
if (password == null) {
throw new BuildException("Password attribute must be set!", getLocation());
}
if (url == null) {
throw new BuildException("Url attribute must be set!", getLocation());
}
try {
log("connecting to " + getUrl(), Project.MSG_VERBOSE);
Properties info = new Properties();
info.put("user", getUserId());
info.put("password", getPassword());
Connection conn = getDriver().connect(getUrl(), info);
if (conn == null) {
// Driver doesn't understand the URL
throw new SQLException("No suitable Driver for " + url);
}
conn.setAutoCommit(autocommit);
return conn;
} catch (SQLException e) {
throw new BuildException(e, getLocation());
}
|
private java.sql.Driver | getDriver()Gets an instance of the required driver.
Uses the ant class loader and the optionally the provided classpath.
if (driver == null) {
throw new BuildException("Driver attribute must be set!", getLocation());
}
Driver driverInstance = null;
try {
Class dc;
if (classpath != null) {
// check first that it is not already loaded otherwise
// consecutive runs seems to end into an OutOfMemoryError
// or it fails when there is a native library to load
// several times.
// this is far from being perfect but should work
// in most cases.
synchronized (loaderMap) {
if (caching) {
loader = (AntClassLoader) loaderMap.get(driver);
}
if (loader == null) {
log("Loading " + driver
+ " using AntClassLoader with classpath "
+ classpath, Project.MSG_VERBOSE);
loader = getProject().createClassLoader(classpath);
if (caching) {
loaderMap.put(driver, loader);
}
} else {
log("Loading " + driver
+ " using a cached AntClassLoader.",
Project.MSG_VERBOSE);
}
}
dc = loader.loadClass(driver);
} else {
log("Loading " + driver + " using system loader.",
Project.MSG_VERBOSE);
dc = Class.forName(driver);
}
driverInstance = (Driver) dc.newInstance();
} catch (ClassNotFoundException e) {
throw new BuildException(
"Class Not Found: JDBC driver " + driver + " could not be loaded",
e,
getLocation());
} catch (IllegalAccessException e) {
throw new BuildException(
"Illegal Access: JDBC driver " + driver + " could not be loaded",
e,
getLocation());
} catch (InstantiationException e) {
throw new BuildException(
"Instantiation Exception: JDBC driver " + driver + " could not be loaded",
e,
getLocation());
}
return driverInstance;
|
protected org.apache.tools.ant.AntClassLoader | getLoader()Get the classloader used to create a driver.
return loader;
|
protected static java.util.Hashtable | getLoaderMap()Get the cache of loaders and drivers.
return loaderMap;
|
public java.lang.String | getPassword()Gets the password.
return password;
|
public java.lang.String | getRdbms()Gets the rdbms.
return rdbms;
|
public java.lang.String | getUrl()Gets the url.
return url;
|
public java.lang.String | getUserId()Gets the userId.
return userId;
|
public java.lang.String | getVersion()Gets the version.
return version;
|
public boolean | isAutocommit()Gets the autocommit.
return autocommit;
|
public void | isCaching(boolean value)Set the caching attribute.
caching = value;
|
protected boolean | isValidRdbms(java.sql.Connection conn)Verify we are connected to the correct RDBMS
if (rdbms == null && version == null) {
return true;
}
try {
DatabaseMetaData dmd = conn.getMetaData();
if (rdbms != null) {
String theVendor = dmd.getDatabaseProductName().toLowerCase();
log("RDBMS = " + theVendor, Project.MSG_VERBOSE);
if (theVendor == null || theVendor.indexOf(rdbms) < 0) {
log("Not the required RDBMS: " + rdbms, Project.MSG_VERBOSE);
return false;
}
}
if (version != null) {
String theVersion = dmd.getDatabaseProductVersion().toLowerCase(Locale.ENGLISH);
log("Version = " + theVersion, Project.MSG_VERBOSE);
if (theVersion == null
|| !(theVersion.startsWith(version)
|| theVersion.indexOf(" " + version) >= 0)) {
log("Not the required version: \"" + version + "\"", Project.MSG_VERBOSE);
return false;
}
}
} catch (SQLException e) {
// Could not get the required information
log("Failed to obtain required RDBMS information", Project.MSG_ERR);
return false;
}
return true;
|
public void | setAutocommit(boolean autocommit)Auto commit flag for database connection;
optional, default false.
this.autocommit = autocommit;
|
public void | setCaching(boolean enable)Caching loaders / driver. This is to avoid
getting an OutOfMemoryError when calling this task
multiple times in a row; default: true
caching = enable;
|
public void | setClasspath(org.apache.tools.ant.types.Path classpath)Sets the classpath for loading the driver.
this.classpath = classpath;
|
public void | setClasspathRef(org.apache.tools.ant.types.Reference r)Set the classpath for loading the driver
using the classpath reference.
createClasspath().setRefid(r);
|
public void | setDriver(java.lang.String driver)Class name of the JDBC driver; required.
this.driver = driver.trim();
|
public void | setPassword(java.lang.String password)Sets the password; required.
this.password = password;
|
public void | setRdbms(java.lang.String rdbms)Execute task only if the lower case product name
of the DB matches this
this.rdbms = rdbms;
|
public void | setUrl(java.lang.String url)Sets the database connection URL; required.
this.url = url;
|
public void | setUserid(java.lang.String userId)Set the user name for the connection; required.
this.userId = userId;
|
public void | setVersion(java.lang.String version)Sets the version string, execute task only if
rdbms version match; optional.
this.version = version;
|