001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.anttasks;
018:
019: import java.sql.Connection;
020: import java.sql.SQLException;
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.taskdefs.SQLExec;
023: import java.util.Properties;
024: import org.apache.tools.ant.Project;
025:
026: ;
027:
028: public class ExecuteSQL extends SQLExec {
029:
030: public void execute() throws BuildException {
031:
032: try {
033: System.out.println("Executing SQL statement");
034: super .execute();
035: } catch (Exception e) {
036: throw new BuildException(e, getLocation());
037: } finally {
038: if (StartDerby.getConnection(getProject()) != null) {
039: try {
040: if (!(StartDerby.getConnection(getProject())
041: .isClosed()))
042: StartDerby.getConnection(getProject()).close();
043: } catch (Exception e1) {
044: // just a safetyguard
045: }
046: StartDerby.setConnection(getProject(), null);
047: }
048: }
049: }
050:
051: /**
052: * Creates a new Connection as using the driver, url, userid and password
053: * specified.
054: *
055: * The calling method is responsible for closing the connection.
056: *
057: * @return Connection the newly created connection.
058: * @throws BuildException if the UserId/Password/Url is not set or there
059: * is no suitable driver or the driver fails to load.
060: */
061: protected Connection getConnection() throws BuildException {
062: if (StartDerby.getDriver(getProject()) == null)
063: throw new BuildException("Derby driver not established!",
064: getLocation());
065: // reuse excisting connection:
066: if (StartDerby.getConnection(getProject()) != null) {
067: System.out.println("Connection already established");
068: return StartDerby.getConnection(getProject());
069: }
070: // do almost the same as in the orignial JDBC tasks:
071:
072: if (getUrl() == null) {
073: throw new BuildException("Url attribute must be set!",
074: getLocation());
075: }
076: try {
077:
078: log("connecting to " + getUrl(), Project.MSG_VERBOSE);
079: Properties info = new Properties();
080: if (getUserId() != null)
081: info.put("user", getUserId());
082: if (getPassword() != null)
083: info.put("password", getPassword());
084:
085: Connection conn = StartDerby.getDriver(getProject())
086: .connect(getUrl(), info);
087: if (conn == null) {
088: // Driver doesn't understand the URL
089: throw new SQLException("No suitable Driver for "
090: + getUrl());
091: }
092:
093: conn.setAutoCommit(isAutocommit());
094: System.out.println("Connection to " + getUrl()
095: + " established");
096: StartDerby.setConnection(getProject(), conn);
097: return conn;
098: } catch (SQLException e) {
099: throw new BuildException(e, getLocation());
100: }
101:
102: }
103:
104: }
|