/*
 * The Spring Framework is published under the terms
 * of the Apache Software License.
 */

package org.springframework.autobuilds.ant;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;


/**
 * HsqlShutdown
 * 
 * Custom ANT task to perform a clean shutdown of an hsql db instance.  Only
 * handles HSQL Server mode.
 * 
 * @author Darren Davison
 */
public class HsqlShutdown extends Task {

	private static final String HSQL_JDBC = "jdbc:hsqldb:hsql://";
	
	// task attribs & defaults
	private String user = "sa";
	private String password = "";
	private String host;
	private int port = -1;	
	private boolean compact = false; 
	 
	
    /**
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute() throws BuildException {
        super.execute();
        
		Connection con = null;
		Statement stmt = null;
        try {
            Class.forName("org.hsqldb.jdbcDriver");
            con = DriverManager.getConnection(HSQL_JDBC + host + ":" + port, user, password);
            stmt = con.createStatement();
            stmt.execute("SHUTDOWN" + (compact ? " COMPACT" : ""));
            
        } catch (ClassNotFoundException e) {
            throw new BuildException("Failed to load HSQL driver - ensure hsqldb.jar is on the classpath");
            
        } catch (SQLException e) {
            throw new BuildException("Failed to connect to HSQL server instance: " + e.getMessage());
        }
        
        finally {
        	try {
        		if (stmt != null) stmt.close();
				if (con != null) con.close();
        	} catch (SQLException sqle_) {}
        }       	
        
    }

    /**
     * @see org.apache.tools.ant.Task#getDescription()
     */
    public String getDescription() {
        return "Shutdown a running hsqldb instance cleanly";
    }
    
    /**
     * @param b
     */
    public void setCompact(boolean b) {
        compact = b;
    }

    /**
     * @param string
     */
    public void setHost(String string) {
        host = string;
    }

    /**
     * @param string
     */
    public void setPassword(String string) {
        password = string;
    }

    /**
     * @param i
     */
    public void setPort(int i) {
        port = i;
    }

    /**
     * @param string
     */
    public void setUser(String string) {
        user = string;
    }

}

@@ -533,72 +533,8 @@
  %7B%0A%0A
-%09private static final String HSQL_JDBC = %22jdbc:hsqldb:hsql://%22;%0A
 %09%0A%09/
@@ -580,17 +580,47 @@
 ng u
-se
 r
+l
  = %22
-sa
+jdbc:hsqldb:hsql://localhost:9001
 %22;%0A%09
@@ -638,20 +638,18 @@
 ing 
-password
+user
  = %22
+sa
 %22;%0A%09
@@ -667,38 +667,22 @@
 ing 
-host;%0A%09private int port = -1;%09
+password = %22%22;
 %0A%09pr
@@ -1032,37 +1032,11 @@
 ion(
-HSQL_JDBC + host + %22:%22 + port
+url
 , us
@@ -2019,12 +2019,16 @@
  set
-Host
+Password
 (Str
@@ -2049,20 +2049,24 @@
         
-host
+password
  = strin
@@ -2127,32 +2127,28 @@
 lic void set
-Password
+User
 (String stri
@@ -2157,32 +2157,28 @@
 ) %7B%0A        
-password
+user
  = string;%0A 
@@ -2187,98 +2187,8 @@
  %7D%0A%0A
-    /**%0A     * @param i%0A     */%0A    public void setPort(int i) %7B%0A        port = i;%0A    %7D%0A%0A
     
@@ -2232,35 +2232,34 @@
 public void setU
-se
 r
+l
 (String string) 
@@ -2261,35 +2261,34 @@
 ing) %7B%0A        u
-se
 r
+l
  = string;%0A    %7D

set props from build.xml