· 7 years ago · May 04, 2019, 12:28 PM
1/*
2 * Copyright 2019 Patrick Zwick <patri9ck at gmail.com>.
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 */
16package de.patri9ck.bridge.setup;
17
18import de.patri9ck.bridge.Bridge;
19import java.sql.Connection;
20import java.sql.DriverManager;
21import java.sql.PreparedStatement;
22import java.sql.SQLException;
23import java.util.logging.Logger;
24import org.bukkit.Bukkit;
25import org.bukkit.configuration.file.FileConfiguration;
26import org.bukkit.scheduler.BukkitRunnable;
27
28/**
29 * Opens and closes connection to database
30 *
31 * @author Patrick Zwick <patri9ck at gmail.com>
32 */
33public class Setup {
34
35 private final Bridge bridge;
36
37 /* Login data */
38 private final String host, database, user, password, table;
39 private final int port;
40
41 private final Logger logger;
42
43 public Setup(final Bridge bridge) {
44 this.bridge = bridge;
45
46 final FileConfiguration config = bridge.getConfig();
47
48 this.host = config.getString("Host");
49 this.database = config.getString("Database");
50 this.user = config.getString("User");
51 this.password = config.getString("Password");
52 this.table = config.getString("Table");
53 this.port = config.getInt("Port");
54
55 this.logger = Bukkit.getLogger();
56 }
57
58 private Connection connection;
59
60 /**
61 * Opens connection to database
62 */
63 public void open() {
64 final String[] columns = new String[19];
65
66 new BukkitRunnable() {
67 int attempts = 1;
68
69 @Override
70 public void run() {
71
72 /* Tries three times to open connection to database */
73 try {
74 Class.forName("com.mysql.jdbc.Driver");
75 connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
76
77 /* Creates table */
78 try (final PreparedStatement statement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS " + table + "(" + String.join(", ", columns) + ")")) {
79 statement.execute();
80 }
81
82 if (this.attempts == 1) {
83 logger.info("Opened connection to database after one attempt.");
84 } else {
85 logger.info("Oepend connection to database after " + this.attempts + " attempts.");
86 }
87
88 this.cancel();
89
90 } catch (final SQLException | ClassNotFoundException exception) {
91 logger.info("Did not open connection to database (" + this.attempts + "/3): " + exception.getMessage());
92
93 /* Disables plugin if three attempts passed */
94 if (this.attempts == 3) {
95 new BukkitRunnable() {
96 @Override
97 public void run() {
98 Bukkit.getPluginManager().disablePlugin(bridge);
99 }
100 }.runTask(bridge);
101 }
102
103 this.attempts++;
104 }
105 }
106 }.runTaskTimerAsynchronously(this.bridge, 0L, 60L);
107 }
108
109 /**
110 * Cloes connection to database
111 */
112 public void close() {
113 try {
114
115 /* Checks if connection is valid */
116 if ((connection == null) || (!connection.isValid(2))) {
117 return;
118 }
119
120 connection.close();
121 logger.info("Closed connection to database.");
122
123 } catch (final SQLException exception) {
124 logger.warning("Did not close connection to database: " + exception.getMessage());
125 }
126 }
127
128 /**
129 * @return Table name
130 */
131 public String getTable() {
132 return this.table;
133 }
134
135 /**
136 * @return Connection to database
137 */
138 public Connection getConnection() {
139 return this.connection;
140 }
141}