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 {
private java.io.File scanDirectory;
private boolean isRunning = true;
private long sleepMillis = 20L * 1000L;
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);
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);
}
if (!newTimestamp.equals(oldTimestamp)) {
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
@param
@throws
@throws
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) {
String parts[] = line.split("=");
if (parts.length > 1)
dsName = parts[1].trim();
}
} finally {
try {
is.close();
} catch (Exception e) {
}
}
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) {
}
}
return q.toString();
}
private void sleep() {
try {
Thread.sleep(sleepMillis);
} catch (Exception e) {
}
}
public void setScanDirectory(java.io.File scanDirectory) {
this.scanDirectory = scanDirectory;
}
public java.io.File getScanDirectory() {
return scanDirectory;
}
@return
public boolean isRunning() {
return isRunning;
}
@param
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}
@return
public long getSleepMillis() {
return sleepMillis;
}
@param
public void setSleepMillis(long sleepMillis) {
this.sleepMillis = sleepMillis;
}
@return
public static java.util.Map<String, Long> getProcessedFiles() {
return processedFiles;
}
}