ASF Bugzilla – Attachment 3825 Details for
Bug 14512
Allow creating database connection similar to <path>, <database> condition
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Java source file - place in org/apache/tools/ant/types
JDBCConnection.java (text/plain), 14.03 KB, created by
thierry lach
on 2002-11-13 16:50:22 UTC
(
hide
)
Description:
Java source file - place in org/apache/tools/ant/types
Filename:
MIME Type:
Creator:
thierry lach
Created:
2002-11-13 16:50:22 UTC
Size:
14.03 KB
patch
obsolete
>/* > * The Apache Software License, Version 1.1 > * > * Copyright (c) 2000-2002 The Apache Software Foundation. All rights > * reserved. > * > * Redistribution and use in source and binary forms, with or without > * modification, are permitted provided that the following conditions > * are met: > * > * 1. Redistributions of source code must retain the above copyright > * notice, this list of conditions and the following disclaimer. > * > * 2. Redistributions in binary form must reproduce the above copyright > * notice, this list of conditions and the following disclaimer in > * the documentation and/or other materials provided with the > * distribution. > * > * 3. The end-user documentation included with the redistribution, if > * any, must include the following acknowlegement: > * "This product includes software developed by the > * Apache Software Foundation (http://www.apache.org/)." > * Alternately, this acknowlegement may appear in the software itself, > * if and wherever such third-party acknowlegements normally appear. > * > * 4. The names "The Jakarta Project", "Ant", and "Apache Software > * Foundation" must not be used to endorse or promote products derived > * from this software without prior written permission. For written > * permission, please contact apache@apache.org. > * > * 5. Products derived from this software may not be called "Apache" > * nor may "Apache" appear in their names without prior written > * permission of the Apache Group. > * > * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED > * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES > * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE > * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR > * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, > * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT > * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF > * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND > * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, > * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT > * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF > * SUCH DAMAGE. > * ==================================================================== > * > * This software consists of voluntary contributions made by many > * individuals on behalf of the Apache Software Foundation. For more > * information on the Apache Software Foundation, please see > * <http://www.apache.org/>. > */ > >package org.apache.tools.ant.types; > >import java.io.File; >import java.sql.Connection; >import java.sql.Driver; >import java.sql.SQLException; >import java.util.Enumeration; >import java.util.Hashtable; >import java.util.Properties; >import java.util.Vector; >import org.apache.tools.ant.AntClassLoader; >import org.apache.tools.ant.BuildException; >import org.apache.tools.ant.Project; > > > >/** > * This object represents a java.sql.Connection usable by the <sql> task > * and the <database> condition. > * <p> > * > * Much of the original code has been moved directly from JDBCTask > * with little modification. > * > * @author <a href="mailto:nick@chalko.com">Nick Chalko</a> > * @author <a href="mailto:jeff@custommonkey.org">Jeff Martin</a> > * @author <A href="mailto:gholam@xtra.co.nz">Michael McCallum</A> > * @author <A href="mailto:tim.stephenson@sybase.com">Tim Stephenson</A> > * @author <a href="mailto:thierry.lacht@bbdodetroit.com">Thierry Lach</a> > * > * @since Ant 1.6 > */ > >public class JDBCConnection extends DataType implements Cloneable { > > /** > * Used for caching loaders / driver. This is to avoid > * getting an OutOfMemoryError when calling this task > * multiple times in a row. > */ > private static Hashtable loaderMap = new Hashtable(3); > > private boolean caching = true; > > private boolean autocommit = JDBCConnection.getAutocommitDefault(); > > /** Holds references. */ > private Vector elements = new Vector(); > > /** Connection */ > private Connection connection = null; > > /** Checks for any connection argument */ > private boolean connectionArgument = false; > > /** Connection reference */ > private Reference connectionRef = null; > > /** Driver class name */ > private String driver = null; > > /** Connection URL */ > private String url = null; > > /** Connection user name */ > private String userId = null; > > /** Connection password */ > private String password = null; > > /** Classpath */ > private Path classpath = null; > > /** Classpath reference */ > private Reference classpathRef = null; > > private AntClassLoader loader; > > public JDBCConnection(Project project) { > setProject(project); > } > > /** > * Class name of the JDBC driver; required. > * @param driver The driver to set > */ > public void setDriver(String driver) { > if (this.connectionRef != null) { > throw tooManyAttributes(); > } > this.driver = driver; > } > > /** > * Gets the driver class string. > * @return Returns a String > */ > public String getDriver() { > return this.driver; > } > > /** > * Gets the url. > * @return Returns a String > */ > public void setUrl(String url) { > if (this.connectionRef != null) { > throw tooManyAttributes(); > } > this.url = url; > } > > /** > * Sets the database connection URL; required. > * @param url The url to set > */ > public String getUrl() { > return this.url; > } > > /** > * Set the user name for the connection; required. > * @param userId The userId to set > */ > public void setUserId(String userId) { > if (this.connectionRef != null) { > throw tooManyAttributes(); > } > this.userId = userId; > } > > /** > * Gets the userId. > * @return Returns a String > */ > public String getUserId() { > return this.userId; > } > > /** > * Sets the password; required. > * @param password The password to set > */ > public void setPassword(String password) { > if (this.connectionRef != null) { > throw tooManyAttributes(); > } > this.password = password; > } > > /** > * Gets the password. > * @return Returns a String > */ > public String getPassword() { > return this.password; > } > > /** > * Auto commit flag for database connection; > * optional, default false. > * @param autocommit The autocommit to set > */ > public void setAutocommit(boolean autocommit) { > this.autocommit = autocommit; > } > > /** > * Gets the autocommit. > * @return Returns a boolean > */ > public boolean getAutocommit() { > return this.autocommit; > } > > /** > * Caching loaders / driver. This is to avoid > * getting an OutOfMemoryError when calling this task > * multiple times in a row; default: true > * @param enable > */ > public void setCaching(boolean enable) { > // TODO: Perhaps by using connection references > // this will no longer be necessary. > this.caching = enable; > } > > /** > * Gets the caching state. > * @return Returns a boolean > */ > public boolean getCaching() { > return this.caching; > } > > /** Returns the current classpath. */ > public Path getClasspath() { > return this.classpath; > } > > /** > * Allows simple classpath string. Not allowed if this catalog is > * itself a reference to another catalog -- that is, a catalog > * cannot both refer to another <em>and</em> contain elements or > * other attributes. > */ > public void setClasspath(Path classpath) { > if (this.classpathRef != null) { > throw tooManyAttributes(); > } > if (this.classpath == null) { > this.classpath = classpath; > } else { > this.classpath.append(classpath); > } > // TODO: This was done in other classes when handling classpath. > // Is it necessary in this context? > // If so, what else needs to be done? > setChecked(false); > } > > /** > * Allows connection reference. > */ > public void setConnectionRef(Reference r) { > if (this.connectionArgument) { > throw tooManyAttributes(); > } > if (this.connectionRef != null) { > throw tooManyAttributes(); > } > Object o = r.getReferencedObject(getProject()); > if (o instanceof JDBCConnection) { > this.connectionRef = r; > } > else { > throw new BuildException("Reference must be a connection!"); > } > } > > /** > * Allows classpath reference. Not allowed if this catalog is > * itself a reference to another catalog -- that is, a catalog > * cannot both refer to another <em>and</em> contain elements or > * other attributes. > */ > public void setClasspathRef(Reference r) { > if (this.classpath != null) { > throw tooManyAttributes(); > } > if (this.classpathRef != null) { > throw tooManyAttributes(); > } > this.classpath = new Path(getProject()); > this.classpath.setRefid(r); > // TODO: This was done in other classes when handling classpath. > // Is it necessary in this context? > // If so, what else needs to be done? > setChecked(false); > } > > /** > * Gets an instance of the required driver. > * Uses the ant class loader and the optionally the provided classpath. > * @return Driver > * @throws BuildException > */ > public Driver getDriverInstance() throws BuildException { > if (this.connectionRef != null) { > JDBCConnection ref = (JDBCConnection)connectionRef. > getReferencedObject(getProject()); > return ref.getDriverInstance(); > } > > if (driver == null) { > throw new BuildException("Driver attribute must be set!"); > } > > 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 = new AntClassLoader(getProject(), 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"); > } catch (IllegalAccessException e) { > throw new BuildException( > "Illegal Access: JDBC driver " + driver + " could not be loaded"); > } catch (InstantiationException e) { > throw new BuildException( > "Instantiation Exception: JDBC driver " + driver + " could not be loaded"); > } > return driverInstance; > } > > /** > * Creates a new Connection as using the driver, url, userid and password specified. > * The calling method is responsible for closing the connection. > * @return Connection the newly created connection. > * @throws BuildException if the UserId/Password/Url is not set or there is no suitable driver or the driver fails to load. > */ > public Connection getConnectionInstance() throws BuildException { > if (this.connection != null) { > return this.connection; > } > if (this.connectionRef != null) { > JDBCConnection ref = (JDBCConnection)connectionRef. > getReferencedObject(getProject()); > return ref.getConnectionInstance(); > } > > if (this.userId == null) { > throw new BuildException("User Id attribute must be set!"); > } > if (this.password == null) { > throw new BuildException("Password attribute must be set!"); > } > if (this.url == null) { > throw new BuildException("Url attribute must be set!"); > } > try { > > log("connecting to " + getUrl(), Project.MSG_VERBOSE); > Properties info = new Properties(); > info.put("user", getUserId()); > info.put("password", getPassword()); > this.connection = getDriverInstance().connect(getUrl(), info); > > if (this.connection == null) { > // Driver doesn't understand the URL > throw new SQLException("No suitable Driver for " + url); > } > this.connection.setAutoCommit(this.autocommit); > return this.connection; > } catch (SQLException e) { > throw new BuildException(e); > } > > } > > /** > * Default value for the auto commit flag for database connection. > * @return false > */ > public static boolean getAutocommitDefault() { > return false; > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 14512
:
3824
| 3825 |
3826