C:\cvs\SqlDeployerMbean\src\com\redneck\tools\SqlFileScanner.java
package com.redneck.tools;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class SqlFileScanner extends Thread {

        /** directory to scan */
        private java.io.File scanDirectory;

        private boolean isRunning = true;

        private long sleepMillis = 20L * 1000L; // 20 seconds

        private static SqlFileFilter filter;

        private static java.util.Map<String, Long> processedFiles;

        static {
                filter = new SqlFileFilter();
                processedFiles = new java.util.HashMap<String, Long>();
        }

        public SqlFileScanner() {
                super();
        }

        public void run() {

                while (isRunning) {
                        java.io.File[] files = null;
                        files = scanDirectory.listFiles(filter);

                        //System.out.println("SqlFileScanner Found " + files.length + " SQL files.");
                        for (int i = 0; i < files.length; i++) {

                                String newFileName = null;
                                Long newTimestamp = null;
                                Long oldTimestamp = null;

                                try {
                                        newFileName = files[i].getCanonicalPath();
                                        newTimestamp = new Long(files[i].lastModified());

                                        synchronized (this) {
                                                oldTimestamp = (Long) processedFiles.get(newFileName);
                                        }

                                        //System.out.println(newFileName + ", Old TS:" + oldTimestamp   + ", New TS: " + newTimestamp);
                                        if (!newTimestamp.equals(oldTimestamp)) {
                                                // add it to map so we don't keep processing same file
                                                synchronized (this) {
                                                        processedFiles.put(newFileName, newTimestamp);
                                                }

                                                String dsName = this.getDataSource(newFileName);
                                                String query = this.getQuery(newFileName);

                                                runQuery(dsName, query);
                                        }
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                        sleep();
                }
        }

        private void runQuery(String dsName, String query) {
                InitialContext ctx;
                DataSource dataSource = null;
                Properties props = null;
                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                ResultSetMetaData rsmd = null;
                StringBuffer out = new StringBuffer();

                try {
                        props = new Properties();
                        props.put("jnp.disableDiscovery", "TRUE");
                        ctx = new InitialContext(props);
                        dataSource = (DataSource) ctx.lookup(dsName);
                        conn = dataSource.getConnection();
                        ps = conn.prepareStatement(query);
                        long start = System.currentTimeMillis();
                        rs = ps.executeQuery();
                        long end = System.currentTimeMillis();
                        rsmd = rs.getMetaData();

                        out.append("DataSource: ").append(dsName);
                        out.append("\nQuery:\n======\n").append(query);
                        out.append("\nQUERY RESULTS\n==============\n");
                        for (int ii = 1; ii <= rsmd.getColumnCount(); ii++) {
                                out.append(rsmd.getColumnName(ii)).append("\t");
                        }
                        out.append("\n");

                        int recordCount = 0;
                        while (rs.next()) {
                                recordCount++;
                                for (int jj = 1; jj <= rsmd.getColumnCount(); jj++) {
                                        out.append(rs.getObject(jj)).append("\t");
                                }
                                out.append("\n");
                        }
                        out.append(recordCount).append(" rows found in ").append(end - start).append(" ms.");
                        System.out.println(out.toString());

                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        try {
                                rs.close();
                        } catch (Exception e) {
                        }
                        try {
                                ps.close();
                        } catch (Exception e) {
                        }
                        try {
                                conn.close();
                        } catch (Exception e) {
                        }
                }
        }

        /**
         * @param newFileName
         * @param query
         * @throws FileNotFoundException
         * @throws IOException
         */
        private String getDataSource(String newFileName)
                        throws FileNotFoundException, IOException {

                BufferedReader is = null;
                String line = "";
                String dsName = null;

                try {
                        is = new BufferedReader(new FileReader(newFileName));

                        line = is.readLine();
                        if (line != null) {
                                // first line, look for DS
                                // we will assume first line of file has a
                                // DSNAME=DatasourceName in it somewhere
                                String parts[] = line.split("=");
                                if (parts.length > 1)
                                        dsName = parts[1].trim();
                        }
                } finally {
                        try {
                                is.close();
                        } catch (Exception e) {
                        }
                }
                //System.out.println("Datasource found in file: " + dsName);
                return dsName;
        }

        private String getQuery(String newFileName) throws FileNotFoundException,
                        IOException {
                BufferedReader is = null;
                String line = "";
                StringBuffer q = new StringBuffer();
                try {
                        is = new BufferedReader(new FileReader(newFileName));

                        while (line != null) {
                                line = is.readLine();
                                if (line != null && !line.trim().equals("")
                                                && !line.startsWith("--"))
                                        q.append(line);
                        }
                } finally {
                        try {
                                is.close();
                        } catch (Exception e) {
                        }
                }
                //System.out.println("Query found in file: " + q.toString());
                return q.toString();
        }

        /**
         * 
         */
        private void sleep() {
                //System.out.println("About to sleep...");
                try {
                        Thread.sleep(sleepMillis);
                } catch (Exception e) {
                }
                //System.out.println("Waking up.");
        }

        public void setScanDirectory(java.io.File scanDirectory) {
                this.scanDirectory = scanDirectory;
        }

        public java.io.File getScanDirectory() {
                return scanDirectory;
        }

        /**
         * @return the isRunning
         */
        public boolean isRunning() {
                return isRunning;
        }

        /**
         * @param isRunning
         *            the isRunning to set
         */
        public void setRunning(boolean isRunning) {
                this.isRunning = isRunning;
        }

        /**
         * @return the sleepMillis
         */
        public long getSleepMillis() {
                return sleepMillis;
        }

        /**
         * @param sleepMillis
         *            the sleepMillis to set
         */
        public void setSleepMillis(long sleepMillis) {
                this.sleepMillis = sleepMillis;
        }

        /**
         * @return the processedFiles
         */
        public static java.util.Map<String, Long> getProcessedFiles() {
                return processedFiles;
        }

}