001: package com.bostechcorp.cbesb.tools.ant;
002:
003: import java.io.FileInputStream;
004: import java.sql.Connection;
005:
006: import java.sql.DriverManager;
007:
008: import java.sql.SQLException;
009: import java.sql.Statement;
010:
011: import java.util.Properties;
012:
013: import org.apache.tools.ant.BuildException;
014: import org.apache.tools.ant.Task;
015:
016: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
017:
018: public class ErrorDBClean extends Task {
019:
020: protected Connection conn = null;
021: private String url = "";
022: private String driver = "";
023: private String username = "";
024: private String password = "";
025: private String connectUrl = "";
026:
027: private String deleteCommand = "delete * from StringContent;"
028: + "delete * from ByteContent;"
029: + "delete * from Attachment;"
030: + "delete * from MessagePropert;"
031: + "delete * from NormalizedMessage;"
032: + "delete * from ExchangeProperty;"
033: + "delete * from Exchange;" + "delete * from Error;";
034:
035: @Override
036: public void execute() throws BuildException {
037: super .execute();
038: connect(null);//read connection string from properties file
039: executeCleanDB();
040: disconnect();
041: }
042:
043: private void connect(String connStr) {
044: String cs = connStr;
045: if (cs != null)
046: cs = handleEnvForString(connStr);
047: try {
048: conn = connectToDatabase(cs);
049: conn.setAutoCommit(false);
050: } catch (Exception e) {
051: System.err.println("Database Connection Error:"
052: + e.getMessage());
053: System.err.println("Error Stacktrace " + e.getStackTrace());
054: }
055: }
056:
057: private void disconnect() {
058: try {
059: conn.close();
060: } catch (SQLException e) {
061: System.err.println("Database Connection Error:"
062: + e.getMessage());
063: System.err.println("Error Stacktrace " + e.getStackTrace());
064: }
065: }
066:
067: private void executeCleanDB() {
068: try {
069: Statement s = conn.createStatement();
070: s.executeUpdate(deleteCommand);
071: conn.commit();
072: } catch (SQLException e) {
073: System.err.println("Database Execution Error:"
074: + e.getMessage());
075: System.err.println("Error Stacktrace " + e.getStackTrace());
076: }
077: }
078:
079: private String handleEnvForString(String connectString) {
080: int pos = connectString.indexOf("%");
081: if (pos == -1)
082: return connectString;
083: String[] arr = connectString.split("%");
084: if (arr.length < 3)
085: return connectString;
086: for (int i = 0; i < arr.length / 2; i++) {
087: String envPara = arr[i * 2 + 1];
088: String envString = System.getenv(envPara);
089: connectString = connectString.replace("%" + envPara + "%",
090: envString);
091: }
092: return connectString;
093: }
094:
095: private Connection connectToDatabase(String connectString)
096: throws Exception {
097:
098: Properties props = new Properties();
099: String home = EsbPathHelper.getCbesbHomeDir();
100: FileInputStream is = new FileInputStream(home
101: + "/config/errordb/errordb.properties");
102: props.load(is);
103: url = props.getProperty("url");
104: driver = props.getProperty("driver");
105: username = props.getProperty("username");
106: password = props.getProperty("password");
107:
108: Connection connection;
109: Class.forName(driver).newInstance();
110: if (connectString == null)
111: connectString = url;
112: this.connectUrl = connectString;
113: connection = DriverManager.getConnection(connectString,
114: username, password);
115: return connection;
116: }
117: }
|