70 |
brianR |
1 |
/*
|
|
|
2 |
* Copyright 2011 Brian Rosenberger (Brutex Network)
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package net.brutex.xservices.util;
|
|
|
18 |
|
|
|
19 |
import java.io.BufferedReader;
|
|
|
20 |
import java.io.File;
|
|
|
21 |
import java.io.FileNotFoundException;
|
|
|
22 |
import java.io.FileReader;
|
|
|
23 |
import java.io.IOException;
|
|
|
24 |
import java.sql.Connection;
|
|
|
25 |
import java.sql.DatabaseMetaData;
|
|
|
26 |
import java.sql.DriverManager;
|
|
|
27 |
import java.sql.ResultSet;
|
|
|
28 |
import java.sql.SQLException;
|
|
|
29 |
import java.sql.Statement;
|
|
|
30 |
import java.util.ArrayList;
|
|
|
31 |
import java.util.List;
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
import org.apache.log4j.Level;
|
|
|
35 |
import org.apache.log4j.Logger;
|
|
|
36 |
import org.quartz.utils.ConnectionProvider;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* @author Brian Rosenberger
|
|
|
40 |
*
|
|
|
41 |
*/
|
|
|
42 |
public class BrutexQuartzConnectionProvider implements ConnectionProvider {
|
|
|
43 |
|
|
|
44 |
private Connection conn = null;
|
|
|
45 |
private final Logger logger = Logger.getLogger(this.getClass().getCanonicalName());
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
public Connection getConnection() throws SQLException {
|
|
|
49 |
if( conn!= null && conn.isValid(5)) {
|
|
|
50 |
logger.debug("Checking tables on pre-exisiting database connection.");
|
|
|
51 |
checkTables();
|
|
|
52 |
return conn;
|
|
|
53 |
}
|
|
|
54 |
try {
|
|
|
55 |
// Class.forName("org.hsqldb.jdbc.JDBCDriver" );
|
|
|
56 |
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
|
|
|
57 |
} catch (Exception e) {
|
|
|
58 |
logger.fatal("Failed to load Derby JDBC driver.");
|
|
|
59 |
e.printStackTrace();
|
|
|
60 |
return null;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if(isConnected(false)) {
|
|
|
64 |
checkTables();
|
|
|
65 |
} else {
|
|
|
66 |
return null;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
return conn;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public void shutdown() throws SQLException {
|
|
|
73 |
try {
|
|
|
74 |
// Class.forName("org.hsqldb.jdbc.JDBCDriver" );
|
|
|
75 |
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
|
|
|
76 |
} catch (Exception e) {
|
|
|
77 |
System.err.println("ERROR: failed to load Derby JDBC driver.");
|
|
|
78 |
e.printStackTrace();
|
|
|
79 |
return;
|
|
|
80 |
}
|
|
|
81 |
String t = this.getClass().getClassLoader().getResource("/").toString()
|
|
|
82 |
.substring(6);
|
|
|
83 |
t += "../data/db";
|
|
|
84 |
System.out.println("Shut down embedded database now.");
|
|
|
85 |
Connection c = DriverManager.getConnection("jdbc:derby:" + t
|
|
|
86 |
+ ";shutdown=true;");
|
|
|
87 |
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
private synchronized void recursiveDelete(File dbDir) {
|
|
|
91 |
File[] files = dbDir.listFiles();
|
|
|
92 |
for (int i = 0; i < files.length; i++) {
|
|
|
93 |
if (files[i].isFile()) {
|
|
|
94 |
files[i].delete();
|
|
|
95 |
} else {
|
|
|
96 |
recursiveDelete(files[i]);
|
|
|
97 |
files[i].delete();
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
dbDir.delete();
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
private synchronized void checkTables() throws SQLException {
|
|
|
104 |
logger.debug("Checking QUARTZ database schema.");
|
|
|
105 |
if(!isConnected(false)) {
|
|
|
106 |
logger.error("Failed to validate QUARTZ database schema.");
|
|
|
107 |
return;
|
|
|
108 |
}
|
|
|
109 |
List<String> ddl_list = new ArrayList<String>(11);
|
|
|
110 |
ddl_list.add("QRTZ_JOB_DETAILS");
|
|
|
111 |
ddl_list.add("QRTZ_TRIGGERS");
|
|
|
112 |
ddl_list.add("QRTZ_SIMPLE_TRIGGERS");
|
|
|
113 |
ddl_list.add("QRTZ_CRON_TRIGGERS");
|
|
|
114 |
ddl_list.add("QRTZ_SIMPROP_TRIGGERS");
|
|
|
115 |
ddl_list.add("QRTZ_BLOB_TRIGGERS");
|
|
|
116 |
ddl_list.add("QRTZ_CALENDARS");
|
|
|
117 |
ddl_list.add("QRTZ_PAUSED_TRIGGER_GRPS");
|
|
|
118 |
ddl_list.add("QRTZ_FIRED_TRIGGERS");
|
|
|
119 |
ddl_list.add("QRTZ_SCHEDULER_STATE");
|
|
|
120 |
ddl_list.add("QRTZ_LOCKS");
|
|
|
121 |
|
|
|
122 |
String ddl = this.getClass().getClassLoader().getResource("/").toString()
|
|
|
123 |
.substring(6)+ "../data/";
|
|
|
124 |
|
|
|
125 |
DatabaseMetaData dmd = conn.getMetaData();
|
|
|
126 |
for (String tbl : ddl_list) {
|
|
|
127 |
ResultSet rs = dmd.getTables(null, "APP", tbl, null);
|
|
|
128 |
if (!rs.next()) {
|
|
|
129 |
logger.log(Level.INFO, "Adding DDL for table "+ tbl);
|
|
|
130 |
Statement st = conn.createStatement();
|
|
|
131 |
File ddlFile = new File(ddl + tbl + ".ddl");
|
|
|
132 |
String create = "";
|
|
|
133 |
try {
|
|
|
134 |
BufferedReader r = new BufferedReader(new FileReader(ddlFile));
|
|
|
135 |
while (r.ready()) {
|
|
|
136 |
create += r.readLine() + "\n";
|
|
|
137 |
}
|
|
|
138 |
create.trim();
|
|
|
139 |
if( st.execute(create)) {
|
|
|
140 |
logger.log(Level.INFO, "Table " + tbl + " created.");
|
|
|
141 |
}
|
|
|
142 |
} catch (FileNotFoundException ex) {
|
|
|
143 |
ex.printStackTrace();
|
|
|
144 |
} catch (IOException ex) {
|
|
|
145 |
ex.printStackTrace();
|
|
|
146 |
} catch (SQLException ex) {
|
|
|
147 |
logger.log(Level.ERROR, "Error executing statement "+ create );
|
|
|
148 |
System.out.println(ex.getMessage());
|
|
|
149 |
}
|
|
|
150 |
} else {
|
|
|
151 |
logger.trace("Table "+tbl+" exists.");
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
private synchronized boolean isConnected(boolean fail) throws SQLException {
|
|
|
157 |
if(conn!=null && conn.isValid(5)) {
|
|
|
158 |
return true;
|
|
|
159 |
} else {
|
|
|
160 |
String t = this.getClass().getClassLoader().getResource("/").toString().substring(6); // WEB-INF/classes
|
|
|
161 |
t += "../data/db";
|
|
|
162 |
logger.debug("Database directory is set to '" + t + "'");
|
|
|
163 |
try {
|
|
|
164 |
this.conn = DriverManager.getConnection("jdbc:derby:" + t + ";create=true;");
|
|
|
165 |
} catch (SQLException ex) {
|
|
|
166 |
logger.error(ex.getMessage(), ex);
|
|
|
167 |
if(!fail) {
|
|
|
168 |
logger.warn("Deleting database directory.");
|
|
|
169 |
recursiveDelete(new File(t));
|
|
|
170 |
logger.warn("Retrying to connect to database.");
|
|
|
171 |
return isConnected(true);
|
|
|
172 |
} else {
|
|
|
173 |
return false;
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
return false;
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|