Subversion Repositories XServices

Rev

Rev 198 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 198 Rev 199
Line 28... Line 28...
28
import java.sql.SQLException;
28
import java.sql.SQLException;
29
import java.sql.Statement;
29
import java.sql.Statement;
30
import java.util.ArrayList;
30
import java.util.ArrayList;
31
import java.util.List;
31
import java.util.List;
Line -... Line 32...
-
 
32
 
32
 
33
 
33
import org.apache.logging.log4j.Level;
-
 
34
import org.apache.logging.log4j.LogManager;
-
 
35
import org.apache.logging.log4j.Logger;
34
import lombok.extern.slf4j.Slf4j;
Line 36... Line 35...
36
import org.quartz.utils.ConnectionProvider;
35
import org.quartz.utils.ConnectionProvider;
37
 
36
 
38
/**
37
/**
39
 * @author Brian Rosenberger
38
 * @author Brian Rosenberger
-
 
39
 * 
40
 * 
40
 */
Line 41... Line 41...
41
 */
41
@Slf4j
42
public class BrutexHSQLQuartzConnectionProvider implements ConnectionProvider {
-
 
-
 
42
public class BrutexHSQLQuartzConnectionProvider implements ConnectionProvider {
Line 43... Line 43...
43
	
43
	
44
	private Connection conn = null;
44
	private Connection conn = null;
45
	private static final Logger logger = LogManager.getLogger();
45
 
46
	
46
	
47
 
47
 
48
	public Connection getConnection() throws SQLException {
48
	public Connection getConnection() throws SQLException {
49
		if( conn!= null ) { // Todo: && conn.conn.isValid(5)) {
49
		if( conn!= null ) { // Todo: && conn.conn.isValid(5)) {
50
			logger.debug("Checking tables on pre-exisiting database connection.");
50
			log.debug("Checking tables on pre-exisiting database connection.");
51
			checkTables();
51
			checkTables();
52
			return conn;
52
			return conn;
53
		}
53
		}
54
		try {
54
		try {
55
			// Class.forName("org.hsqldb.jdbc.JDBCDriver" );
55
			// Class.forName("org.hsqldb.jdbc.JDBCDriver" );
56
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
56
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Line 57... Line 57...
57
		} catch (Exception e) {
57
		} catch (Exception e) {
Line 104... Line 104...
104
		}
104
		}
105
		dbDir.delete();
105
		dbDir.delete();
106
	}
106
	}
Line 107... Line 107...
107
	
107
	
108
	private synchronized void checkTables() throws SQLException {
108
	private synchronized void checkTables() throws SQLException {
109
		logger.debug("Checking QUARTZ database schema.");
109
		log.debug("Checking QUARTZ database schema.");
110
		if(!isConnected(false)) {
110
		if(!isConnected(false)) {
111
			logger.error("Failed to validate QUARTZ database schema.");
111
			log.error("Failed to validate QUARTZ database schema.");
112
			return;
112
			return;
113
		}
113
		}
114
		List<String> ddl_list = new ArrayList<String>(11);
114
		List<String> ddl_list = new ArrayList<String>(11);
115
		ddl_list.add("QRTZ_JOB_DETAILS");
115
		ddl_list.add("QRTZ_JOB_DETAILS");
Line 129... Line 129...
129
 
129
 
130
		DatabaseMetaData dmd = conn.getMetaData();
130
		DatabaseMetaData dmd = conn.getMetaData();
131
		for (String tbl : ddl_list) {
131
		for (String tbl : ddl_list) {
132
			ResultSet rs = dmd.getTables(null, "APP", tbl, null);
132
			ResultSet rs = dmd.getTables(null, "APP", tbl, null);
133
			if (!rs.next()) {
133
			if (!rs.next()) {
134
				logger.log(Level.INFO, "Adding DDL for table "+ tbl);
134
				log.debug("Adding DDL for table {}", tbl);
135
				Statement st = conn.createStatement();
135
				Statement st = conn.createStatement();
136
				File ddlFile = new File(ddl + tbl + ".ddl");
136
				File ddlFile = new File(ddl + tbl + ".ddl");
137
				String create = "";
137
				String create = "";
138
				try {
138
				try {
139
						BufferedReader r = new BufferedReader(new FileReader(ddlFile));
139
						BufferedReader r = new BufferedReader(new FileReader(ddlFile));
140
						while (r.ready()) {
140
						while (r.ready()) {
141
							create += r.readLine() + "\n";
141
							create += r.readLine() + "\n";
142
						}
142
						}
143
						create.trim();
143
						create.trim();
144
						if( st.execute(create)) {
144
						if( st.execute(create)) {
145
							logger.log(Level.INFO, "Table " + tbl + " created.");
145
							log.debug("Table {} created.", tbl);
146
						} 
-
 
147
					} catch (FileNotFoundException ex) {
-
 
148
						ex.printStackTrace();
146
						} 
149
					} catch (IOException ex) {
147
					} catch (IOException ex) {
150
						ex.printStackTrace();
148
						ex.printStackTrace();
151
					} catch (SQLException ex) {
149
					} catch (SQLException ex) {
152
						logger.log(Level.ERROR, "Error executing statement "+ create );
-
 
153
						System.out.println(ex.getMessage());
150
						log.error("Error executing statement {}.",create, ex );
154
					}
151
					}
155
				} else {
152
				} else {
156
					logger.trace("Table "+tbl+" exists.");
153
					log.trace("Table {} exists.", tbl);
157
				}
154
				}
158
			}
155
			}
Line 159... Line 156...
159
		}
156
		}
160
	
157
	
161
	private synchronized boolean isConnected(boolean fail) throws SQLException {
158
	private synchronized boolean isConnected(boolean fail) throws SQLException {
162
		if(conn!=null ) { // Todo: && conn.conn.isValid(5)) {) {
159
		if(conn!=null ) { // Todo: && conn.conn.isValid(5)) {) {
163
			return true;
160
			return true;
164
		} else {
161
		} else {
165
			String t = this.getClass().getClassLoader().getResource("/").toString().substring(6); // WEB-INF/classes
162
			String t = this.getClass().getClassLoader().getResource("/").toString().substring(6); // WEB-INF/classes
166
			t += "../data/db";
163
			t += "../data/db";
167
			logger.debug("Database directory is set to '" + t + "'");
164
			log.debug("Database directory is set to '{}", t);
168
			try {
165
			try {
169
				this.conn = DriverManager.getConnection("jdbc:derby:" + t + ";create=true;");
166
				this.conn = DriverManager.getConnection("jdbc:derby:" + t + ";create=true;");
170
			} catch (SQLException ex) {
167
			} catch (SQLException ex) {
171
				logger.error(ex.getMessage(), ex);
168
				log.error(ex.getMessage(), ex);
172
				if(!fail) {
169
				if(!fail) {
173
					logger.warn("Deleting database directory.");
170
					log.warn("Deleting database directory.");
174
					recursiveDelete(new File(t));
171
					recursiveDelete(new File(t));
175
					logger.warn("Retrying to connect to database.");
172
					log.warn("Retrying to connect to database.");
176
					return isConnected(true);
173
					return isConnected(true);
177
				} else {
174
				} else {
178
					return false;
175
					return false;