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;
}
}
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();
}
Aggregations