Skip to content

Commit

Permalink
refactor: (ported from 0.2.32) allow custom jdbc drivers and custom p…
Browse files Browse the repository at this point in the history
…roperties for mysql connection
  • Loading branch information
Misat11 committed Jan 24, 2024
1 parent 995639f commit fc33641
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,33 @@ public void load() {
.key("user").defValue("root")
.key("password").defValue("secret")
.key("table-prefix").defValue("bw_")
.key("useSSL").defValue(false)
.key("add-timezone-to-connection-string").defValue(true)
.key("timezone-id").defValue(TimeZone.getDefault().getID())
.key("type").defValue("mysql")
.key("driver").defValue("default")
.key("params").defValue(() -> {
var map = new HashMap<String, Object>();
map.put("useSSL", configurationNode.node("database", "useSSL").getBoolean(true));
if (configurationNode.node("database", "add-timezone-to-connection-string").getBoolean(true)) {
map.put("serverTimezone", configurationNode.node("database", "timezone-id").getString(TimeZone.getDefault().getID()));
}
map.put("autoReconnect", true);
map.put("cachePrepStmts", true);
map.put("prepStmtCacheSize", 250);
map.put("prepStmtCacheSqlLimit", 2048);

if (!configurationNode.node("database", "useSSL").virtual()) {
configurationNode.node("database").removeChild("useSSL");
}

if (!configurationNode.node("database", "add-timezone-to-connection-string").virtual()) {
configurationNode.node("database").removeChild("add-timezone-to-connection-string");
}

if (!configurationNode.node("database", "timezone-id").virtual()) {
configurationNode.node("database").removeChild("timezone-id");
}

return map;
})
.back()
.section("bossbar")
.key("use-xp-bar").defValue(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@
import org.screamingsandals.lib.plugin.ServiceManager;
import org.screamingsandals.lib.utils.annotations.Service;
import org.screamingsandals.lib.utils.annotations.methods.OnEnable;
import org.screamingsandals.lib.utils.annotations.parameters.DataFolder;
import org.spongepowered.configurate.ConfigurationNode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.TimeZone;

@Service
@RequiredArgsConstructor
public class DatabaseManager {
private final MainConfig mainConfig;
@DataFolder
private final Path dataFolder;

private String tablePrefix;
private String database;
Expand All @@ -43,9 +53,9 @@ public class DatabaseManager {
private String password;
private int port;
private String user;
private boolean useSSL;
private boolean addTimezone;
private String timezoneID;
private ConfigurationNode params;
private String type;
private String driver;

public static DatabaseManager getInstance() {
return ServiceManager.get(DatabaseManager.class);
Expand All @@ -59,22 +69,74 @@ public void onEnable() {
this.password = mainConfig.node("database", "password").getString();
this.database = mainConfig.node("database", "db").getString();
this.tablePrefix = mainConfig.node("database", "table-prefix").getString();
this.useSSL = mainConfig.node("database", "useSSL").getBoolean();
this.addTimezone = mainConfig.node("database", "add-timezone-to-connection-string").getBoolean();
this.timezoneID = mainConfig.node("database", "timezone-id").getString();
this.params = mainConfig.node("database", "params");
this.type = mainConfig.node("database", "type").getString();
this.driver = mainConfig.node("database", "driver").getString();
}

public void initialize() {
var config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database
+ "?autoReconnect=true" + (addTimezone && timezoneID != null ? "&serverTimezone=" + timezoneID : "") + "&useSSL=" + useSSL);
config.setJdbcUrl("jdbc:" + this.type + "://" + this.host + ":" + this.port + "/" + this.database);
config.setUsername(this.user);
config.setPassword(this.password);
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

params.childrenMap().forEach((key, node) -> {
config.addDataSourceProperty(key.toString(), node.getString());
});

ClassLoader contextCl = null;
Class<?> driverClazz = null;

if (driver != null && !"default".equalsIgnoreCase(driver)) {
try {
var cl = new URLClassLoader(new URL[] {dataFolder.resolve(driver).toAbsolutePath().toUri().toURL()}, ClassLoader.getSystemClassLoader().getParent());
try (var is = cl.getResourceAsStream("META-INF/services/java.sql.Driver")) {
if (is == null) {
throw new RuntimeException("Database driver JAR does not contain JDBC 4 compatible driver");
}
String driverClassName = null;
try (var isr = new InputStreamReader(is);
var reader = new BufferedReader(isr)) {
do {
driverClassName = reader.readLine();
if (driverClassName != null) {
// All characters after '#' should be ignored, whitespaces around the qualified name are also ignored
driverClassName = driver.split("#", 2)[0].trim();
}
} while (driverClassName != null && driverClassName.isEmpty());
}
if (driverClassName == null) {
throw new RuntimeException("Database driver JAR does not contain JDBC 4 compatible driver");
}
driverClazz = cl.loadClass(driverClassName);
contextCl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(cl);
config.setDriverClassName(driverClassName);
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

this.dataSource = new HikariDataSource(config);

if (contextCl != null) {
Thread.currentThread().setContextClassLoader(contextCl);
}

if (driverClazz != null) {
var drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
var driver = drivers.nextElement();
if (driver.getClass().equals(driverClazz)) {
try {
DriverManager.deregisterDriver(driver);
} catch (SQLException e) {
// ignore
}
}
}
}
}

public Connection getConnection() {
Expand Down

0 comments on commit fc33641

Please sign in to comment.