001: package org.contineo.util.dbinit;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.net.URLDecoder;
006: import java.sql.Connection;
007: import java.sql.DriverManager;
008: import java.sql.SQLException;
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.hsqldb.util.SqlFile;
015: import org.hsqldb.util.SqlToolError;
016:
017: /**
018: * Database initialisation utility
019: *
020: * @author Michael Scholz
021: * @author Marco Meschieri
022: */
023: public class DBInit {
024:
025: protected static Log log = LogFactory.getLog(DBInit.class);
026:
027: private String driver = "";
028:
029: private String url = "";
030:
031: private String username = "";
032:
033: private String password = "";
034:
035: private Connection con;
036:
037: // List of sql files to be executed
038: private List<String> sqlList = new ArrayList<String>();
039:
040: /**
041: * A list of sql files to execute
042: *
043: * @param sqlList The list of sql files
044: */
045: public DBInit(List<String> sqlList) {
046: this .sqlList = sqlList;
047: }
048:
049: public DBInit() {
050: }
051:
052: /**
053: * Executes all the sql files defined in the constructor
054: */
055: public void execute() {
056: try {
057: doConnection();
058: for (String sql : sqlList) {
059: try {
060: execute(sql);
061: } catch (Exception e) {
062: log.error("Failed to execute " + sql, e);
063: }
064: }
065: } catch (Throwable e) {
066: log.error(e.getMessage(), e);
067: try {
068: if (con != null)
069: con.close();
070: } catch (SQLException e1) {
071: e1.printStackTrace();
072: }
073: }
074: }
075:
076: /**
077: * Executes a single sql file
078: *
079: * @param sqlFile Path of the file to execute(it can be a classpath
080: * resource)
081: * @throws IOException
082: * @throws SqlToolError
083: * @throws SQLException
084: */
085: private void execute(String sqlFile) throws IOException,
086: SQLException {
087: log.debug("Execute " + sqlFile);
088: System.out.println("Execute " + sqlFile);
089: File file = new File(sqlFile);
090: if (!file.exists() || !file.canRead()) {
091: // Try to interpret the path as a classpath path
092: ClassLoader loader = Thread.currentThread()
093: .getContextClassLoader();
094: try {
095: file = new File(URLDecoder.decode(loader.getResource(
096: sqlFile).getPath(), "UTF-8"));
097: } catch (Exception e) {
098: e.printStackTrace();
099: }
100: }
101:
102: SqlFile sFile = new SqlFile(file, false, null);
103: try {
104: sFile.execute(con, new Boolean(true));
105: } catch (SqlToolError e) {
106: throw new SQLException(e.getMessage());
107: }
108: }
109:
110: protected void doConnection() {
111: try {
112: Class.forName(driver);
113: con = DriverManager.getConnection(url, username, password);
114: con.setAutoCommit(true);
115: } catch (Exception ex) {
116: log.error(ex.getMessage(), ex);
117: }
118: }
119:
120: protected void rollback() {
121: try {
122: con.rollback();
123: } catch (Exception ex) {
124: log.error("can't rollback", ex);
125: }
126: }
127:
128: /**
129: * This method returns the state of the connection.
130: */
131: public boolean isConnected() {
132: try {
133: return !con.isClosed();
134: } catch (Exception ex) {
135: log.debug("db-connection is open:" + ex.getMessage(), ex);
136: return false;
137: }
138: }
139:
140: /**
141: * This method tests a connection.
142: */
143: public boolean testConnection() {
144: boolean result = false;
145:
146: try {
147: Class.forName(driver);
148: con = DriverManager.getConnection(url, username, password);
149: result = true;
150: con.close();
151: } catch (Exception ex) {
152: log.error(ex.getMessage(), ex);
153: }
154:
155: return result;
156: }
157:
158: public void setDriver(String driver) {
159: this .driver = driver;
160: }
161:
162: public void setUrl(String url) {
163: this .url = url;
164: }
165:
166: public void setUsername(String username) {
167: this .username = username;
168: }
169:
170: public void setPassword(String password) {
171: this .password = password;
172: }
173:
174: public List<String> getSqlList() {
175: return sqlList;
176: }
177: }
|