· 7 years ago · Dec 24, 2018, 12:42 PM
1package net.craftersland.customenderchest.storage;
2
3import net.craftersland.customenderchest.*;
4import org.bukkit.*;
5import org.bukkit.plugin.*;
6import java.util.*;
7import java.sql.*;
8
9public class MysqlSetup
10{
11 private Connection conn;
12 private EnderChest enderchest;
13 private boolean tablesChecked;
14
15 public MysqlSetup(final EnderChest enderchest) {
16 this.conn = null;
17 this.tablesChecked = false;
18 this.enderchest = enderchest;
19 this.setupDatabase();
20 this.updateTables();
21 }
22
23 public void setupDatabase() {
24 this.connectToDatabase();
25 this.databaseMaintenanceTask();
26 }
27
28 private void tableMaintenance(final long inactiveTime, final Connection conn, final String tableName) {
29 PreparedStatement preparedStatement = null;
30 try {
31 final String sql = "DELETE FROM `" + tableName + "` WHERE `last_seen` < ?";
32 preparedStatement = conn.prepareStatement(sql);
33 preparedStatement.setString(1, String.valueOf(inactiveTime));
34 preparedStatement.execute();
35 }
36 catch (Exception e) {
37 e.printStackTrace();
38 try {
39 if (preparedStatement != null) {
40 preparedStatement.close();
41 }
42 }
43 catch (Exception e2) {
44 e2.printStackTrace();
45 }
46 return;
47 }
48 finally {
49 try {
50 if (preparedStatement != null) {
51 preparedStatement.close();
52 }
53 }
54 catch (Exception e2) {
55 e2.printStackTrace();
56 }
57 }
58 try {
59 if (preparedStatement != null) {
60 preparedStatement.close();
61 }
62 }
63 catch (Exception e2) {
64 e2.printStackTrace();
65 }
66 }
67
68 private void databaseMaintenanceTask() {
69 if (this.enderchest.getConfigHandler().getBoolean("database.mysql.removeOldUsers.enabled")) {
70 Bukkit.getScheduler().runTaskLaterAsynchronously((Plugin)this.enderchest, (Runnable)new Runnable() {
71 @Override
72 public void run() {
73 if (MysqlSetup.this.conn != null) {
74 final long inactivityDays = Long.parseLong(MysqlSetup.this.enderchest.getConfigHandler().getString("database.mysql.removeOldUsers.inactive"));
75 final long inactivityMils = inactivityDays * 24L * 60L * 60L * 1000L;
76 final long curentTime = System.currentTimeMillis();
77 final long inactiveTime = curentTime - inactivityMils;
78 EnderChest.log.info("Database maintenance task started...");
79 MysqlSetup.this.tableMaintenance(inactiveTime, MysqlSetup.this.getConnection(), MysqlSetup.this.enderchest.getConfigHandler().getString("database.mysql.tableName"));
80 EnderChest.log.info("Database maintenance complete!");
81 }
82 }
83 }, 2000L);
84 }
85 }
86
87 private void connectToDatabase() {
88 try {
89 Class.forName("com.mysql.jdbc.Driver");
90 final Properties properties = new Properties();
91 properties.setProperty("user", this.enderchest.getConfigHandler().getString("database.mysql.user"));
92 properties.setProperty("password", this.enderchest.getConfigHandler().getString("database.mysql.password"));
93 properties.setProperty("autoReconnect", "true");
94 properties.setProperty("verifyServerCertificate", "false");
95 properties.setProperty("useSSL", this.enderchest.getConfigHandler().getString("database.mysql.ssl"));
96 properties.setProperty("requireSSL", this.enderchest.getConfigHandler().getString("database.mysql.ssl"));
97 this.conn = DriverManager.getConnection("jdbc:mysql://" + this.enderchest.getConfigHandler().getString("database.mysql.host") + ":" + this.enderchest.getConfigHandler().getString("database.mysql.port") + "/" + this.enderchest.getConfigHandler().getString("database.mysql.databaseName"), properties);
98 EnderChest.log.info("Database connection established!");
99 if (!this.tablesChecked) {
100 this.setupTables();
101 }
102 }
103 catch (ClassNotFoundException e) {
104 EnderChest.log.severe("Could not locate drivers for mysql! Error: " + e.getMessage());
105 }
106 catch (SQLException e2) {
107 EnderChest.log.severe("Could not connect to mysql database! Error: " + e2.getMessage());
108 }
109 catch (Exception ex) {
110 EnderChest.log.severe("Could not connect to mysql database! Error: " + ex.getMessage());
111 ex.printStackTrace();
112 }
113 }
114
115 public void setupTables() {
116 if (this.conn != null) {
117 PreparedStatement query1 = null;
118 try {
119 final String data = "CREATE TABLE IF NOT EXISTS `" + this.enderchest.getConfigHandler().getString("database.mysql.tableName") + "` (id int(10) AUTO_INCREMENT, player_uuid varchar(50) NOT NULL UNIQUE, player_name varchar(50) NOT NULL, enderchest_data LONGTEXT NOT NULL, size int(3) NOT NULL, last_seen varchar(30) NOT NULL, PRIMARY KEY(id));";
120 query1 = this.conn.prepareStatement(data);
121 query1.execute();
122 this.tablesChecked = true;
123 }
124 catch (Exception e) {
125 EnderChest.log.severe("Error creating tables! Error: " + e.getMessage());
126 e.printStackTrace();
127 try {
128 if (query1 != null) {
129 query1.close();
130 }
131 }
132 catch (Exception e2) {
133 e2.printStackTrace();
134 }
135 return;
136 }
137 finally {
138 try {
139 if (query1 != null) {
140 query1.close();
141 }
142 }
143 catch (Exception e2) {
144 e2.printStackTrace();
145 }
146 }
147 try {
148 if (query1 != null) {
149 query1.close();
150 }
151 }
152 catch (Exception e2) {
153 e2.printStackTrace();
154 }
155 }
156 }
157
158 public Connection getConnection() {
159 this.checkConnection();
160 return this.conn;
161 }
162
163 public void closeConnection() {
164 try {
165 EnderChest.log.info("Closing database connection...");
166 if (this.conn != null) {
167 this.conn.close();
168 this.conn = null;
169 }
170 }
171 catch (SQLException e) {
172 e.printStackTrace();
173 }
174 }
175
176 private void checkConnection() {
177 try {
178 if (this.conn == null) {
179 EnderChest.log.warning("Database connection failed. Reconnecting...");
180 this.conn = null;
181 this.connectToDatabase();
182 }
183 else if (!this.conn.isValid(3)) {
184 EnderChest.log.warning("Database connection failed. Reconnecting...");
185 this.conn = null;
186 this.connectToDatabase();
187 }
188 else if (this.conn.isClosed()) {
189 EnderChest.log.warning("Database connection failed. Reconnecting...");
190 this.conn = null;
191 this.connectToDatabase();
192 }
193 }
194 catch (Exception e) {
195 EnderChest.log.severe("Error re-connecting to the database! Error: " + e.getMessage());
196 }
197 }
198
199 public boolean closeDatabase() {
200 try {
201 this.conn.close();
202 this.conn = null;
203 return true;
204 }
205 catch (SQLException e) {
206 e.printStackTrace();
207 return false;
208 }
209 }
210
211 private void updateTables() {
212 if (this.conn != null) {
213 DatabaseMetaData md = null;
214 ResultSet rs1 = null;
215 ResultSet rs2 = null;
216 PreparedStatement query1 = null;
217 PreparedStatement query2 = null;
218 try {
219 md = this.conn.getMetaData();
220 rs1 = md.getColumns(null, null, this.enderchest.getConfigHandler().getString("database.mysql.tableName"), "enderchest");
221 if (rs1.next()) {
222 final String data1 = "ALTER TABLE `" + this.enderchest.getConfigHandler().getString("database.mysql.tableName") + "` CHANGE COLUMN enderchest enderchest_data LONGTEXT NOT NULL;";
223 query1 = this.conn.prepareStatement(data1);
224 query1.execute();
225 }
226 else {
227 rs2 = md.getColumns(null, null, this.enderchest.getConfigHandler().getString("database.mysql.tableName"), "enderchest_data");
228 if (rs2.next() && rs2.getString("TYPE_NAME").matches("VARCHAR")) {
229 final String data2 = "ALTER TABLE `" + this.enderchest.getConfigHandler().getString("database.mysql.tableName") + "` MODIFY enderchest_data LONGTEXT NOT NULL;";
230 query2 = this.conn.prepareStatement(data2);
231 query2.execute();
232 }
233 }
234 }
235 catch (Exception e) {
236 EnderChest.log.warning("Error on table update! Error: " + e.getMessage());
237 e.printStackTrace();
238 try {
239 if (query1 != null) {
240 query1.close();
241 }
242 if (query2 != null) {
243 query2.close();
244 }
245 if (rs1 != null) {
246 rs1.close();
247 }
248 if (rs2 != null) {
249 rs2.close();
250 }
251 }
252 catch (Exception e2) {
253 e2.printStackTrace();
254 }
255 return;
256 }
257 finally {
258 try {
259 if (query1 != null) {
260 query1.close();
261 }
262 if (query2 != null) {
263 query2.close();
264 }
265 if (rs1 != null) {
266 rs1.close();
267 }
268 if (rs2 != null) {
269 rs2.close();
270 }
271 }
272 catch (Exception e2) {
273 e2.printStackTrace();
274 }
275 }
276 try {
277 if (query1 != null) {
278 query1.close();
279 }
280 if (query2 != null) {
281 query2.close();
282 }
283 if (rs1 != null) {
284 rs1.close();
285 }
286 if (rs2 != null) {
287 rs2.close();
288 }
289 }
290 catch (Exception e2) {
291 e2.printStackTrace();
292 }
293 }
294 }
295}