Search in sources :

Example 1 with Appendable

use of org.apache.tools.ant.types.resources.Appendable in project ant by apache.

the class SQLExec method execute.

/**
 * Load the sql file and then execute it
 * @throws BuildException on error.
 */
@Override
public void execute() throws BuildException {
    List<Transaction> savedTransaction = new Vector<>(transactions);
    String savedSqlCommand = sqlCommand;
    sqlCommand = sqlCommand.trim();
    try {
        if (srcFile == null && sqlCommand.isEmpty() && resources == null) {
            if (transactions.isEmpty()) {
                throw new BuildException("Source file or resource collection, transactions or sql statement must be set!", getLocation());
            }
        }
        if (srcFile != null && !srcFile.isFile()) {
            throw new BuildException("Source file " + srcFile + " is not a file!", getLocation());
        }
        if (resources != null) {
            // deal with the resources
            for (Resource r : resources) {
                // Make a transaction for each resource
                Transaction t = createTransaction();
                t.setSrcResource(r);
            }
        }
        // Make a transaction group for the outer command
        Transaction t = createTransaction();
        t.setSrc(srcFile);
        t.addText(sqlCommand);
        if (getConnection() == null) {
            // not a valid rdbms
            return;
        }
        try {
            PrintStream out = KeepAliveOutputStream.wrapSystemOut();
            try {
                if (output != null) {
                    log("Opening PrintStream to output Resource " + output, Project.MSG_VERBOSE);
                    OutputStream os = null;
                    FileProvider fp = output.as(FileProvider.class);
                    if (fp != null) {
                        os = FileUtils.newOutputStream(fp.getFile().toPath(), append);
                    } else {
                        if (append) {
                            Appendable a = output.as(Appendable.class);
                            if (a != null) {
                                os = a.getAppendOutputStream();
                            }
                        }
                        if (os == null) {
                            os = output.getOutputStream();
                            if (append) {
                                log("Ignoring append=true for non-appendable" + " resource " + output, Project.MSG_WARN);
                            }
                        }
                    }
                    if (outputEncoding != null) {
                        out = new PrintStream(new BufferedOutputStream(os), false, outputEncoding);
                    } else {
                        out = new PrintStream(new BufferedOutputStream(os));
                    }
                }
                // Process all transactions
                for (Transaction txn : transactions) {
                    txn.runTransaction(out);
                    if (!isAutocommit()) {
                        log("Committing transaction", Project.MSG_VERBOSE);
                        getConnection().commit();
                    }
                }
            } finally {
                FileUtils.close(out);
            }
        } catch (IOException | SQLException e) {
            closeQuietly();
            setErrorProperty();
            if ("abort".equals(onError)) {
                throw new BuildException(e, getLocation());
            }
        } finally {
            try {
                FileUtils.close(getStatement());
            } catch (SQLException ex) {
            // ignore
            }
            FileUtils.close(getConnection());
        }
        log(goodSql + " of " + totalSql + " SQL statements executed successfully");
    } finally {
        transactions = savedTransaction;
        sqlCommand = savedSqlCommand;
    }
}
Also used : PrintStream(java.io.PrintStream) SQLException(java.sql.SQLException) KeepAliveOutputStream(org.apache.tools.ant.util.KeepAliveOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) Appendable(org.apache.tools.ant.types.resources.Appendable) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) IOException(java.io.IOException) FileProvider(org.apache.tools.ant.types.resources.FileProvider) BuildException(org.apache.tools.ant.BuildException) Vector(java.util.Vector) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with Appendable

use of org.apache.tools.ant.types.resources.Appendable in project ant by apache.

the class ResourceUtils method getOutputStream.

private static OutputStream getOutputStream(final Resource resource, final boolean append, final Project project) throws IOException {
    if (append) {
        final Appendable a = resource.as(Appendable.class);
        if (a != null) {
            return a.getAppendOutputStream();
        }
        String msg = "Appendable OutputStream not available for non-appendable resource " + resource + "; using plain OutputStream";
        if (project != null) {
            project.log(msg, Project.MSG_VERBOSE);
        } else {
            System.out.println(msg);
        }
    }
    return resource.getOutputStream();
}
Also used : Appendable(org.apache.tools.ant.types.resources.Appendable)

Aggregations

Appendable (org.apache.tools.ant.types.resources.Appendable)2 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 SQLException (java.sql.SQLException)1 Vector (java.util.Vector)1 BuildException (org.apache.tools.ant.BuildException)1 Resource (org.apache.tools.ant.types.Resource)1 FileProvider (org.apache.tools.ant.types.resources.FileProvider)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1 KeepAliveOutputStream (org.apache.tools.ant.util.KeepAliveOutputStream)1