Use driver class instead of datasource for Postgres (#3705)
This commit is contained in:
parent
b8d1f52d7d
commit
71416baff2
|
@ -0,0 +1,80 @@
|
||||||
|
/*
|
||||||
|
* This file is part of LuckPerms, licensed under the MIT License.
|
||||||
|
*
|
||||||
|
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||||
|
* Copyright (c) contributors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
|
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
||||||
|
|
||||||
|
import java.sql.Driver;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extension of {@link HikariConnectionFactory} that uses the driver class name to configure Hikari.
|
||||||
|
*/
|
||||||
|
public abstract class DriverBasedHikariConnectionFactory extends HikariConnectionFactory {
|
||||||
|
protected DriverBasedHikariConnectionFactory(StorageCredentials configuration) {
|
||||||
|
super(configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract String driverClassName();
|
||||||
|
|
||||||
|
protected abstract String driverJdbcIdentifier();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) {
|
||||||
|
config.setDriverClassName(driverClassName());
|
||||||
|
config.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s", driverJdbcIdentifier(), address, port, databaseName));
|
||||||
|
config.setUsername(username);
|
||||||
|
config.setPassword(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void postInitialize() {
|
||||||
|
super.postInitialize();
|
||||||
|
|
||||||
|
// Calling Class.forName("<driver class name>") is enough to call the static initializer
|
||||||
|
// which makes our driver available in DriverManager. We don't want that, so unregister it after
|
||||||
|
// the pool has been setup.
|
||||||
|
deregisterDriver(driverClassName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void deregisterDriver(String driverClassName) {
|
||||||
|
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
||||||
|
while (drivers.hasMoreElements()) {
|
||||||
|
Driver driver = drivers.nextElement();
|
||||||
|
if (driver.getClass().getName().equals(driverClassName)) {
|
||||||
|
try {
|
||||||
|
DriverManager.deregisterDriver(driver);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -37,11 +37,8 @@ import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.Driver;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -245,18 +242,4 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
|
||||||
logger.warn("Class " + className + " has been loaded by: " + loaderName);
|
logger.warn("Class " + className + " has been loaded by: " + loaderName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void deregisterDriver(String driverClassName) {
|
|
||||||
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
|
||||||
while (drivers.hasMoreElements()) {
|
|
||||||
Driver driver = drivers.nextElement();
|
|
||||||
if (driver.getClass().getName().equals(driverClassName)) {
|
|
||||||
try {
|
|
||||||
DriverManager.deregisterDriver(driver);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,12 +25,11 @@
|
||||||
|
|
||||||
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
|
||||||
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class MariaDbConnectionFactory extends HikariConnectionFactory {
|
public class MariaDbConnectionFactory extends DriverBasedHikariConnectionFactory {
|
||||||
public MariaDbConnectionFactory(StorageCredentials configuration) {
|
public MariaDbConnectionFactory(StorageCredentials configuration) {
|
||||||
super(configuration);
|
super(configuration);
|
||||||
}
|
}
|
||||||
|
@ -46,21 +45,13 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) {
|
protected String driverClassName() {
|
||||||
config.setDriverClassName("org.mariadb.jdbc.Driver");
|
return "org.mariadb.jdbc.Driver";
|
||||||
config.setJdbcUrl("jdbc:mariadb://" + address + ":" + port + "/" + databaseName);
|
|
||||||
config.setUsername(username);
|
|
||||||
config.setPassword(password);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void postInitialize() {
|
protected String driverJdbcIdentifier() {
|
||||||
super.postInitialize();
|
return "mariadb";
|
||||||
|
|
||||||
// Calling Class.forName("org.mariadb.jdbc.Driver") is enough to call the static initializer
|
|
||||||
// which makes our driver available in DriverManager. We don't want that, so unregister it after
|
|
||||||
// the pool has been setup.
|
|
||||||
deregisterDriver("org.mariadb.jdbc.Driver");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -25,13 +25,12 @@
|
||||||
|
|
||||||
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
|
||||||
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class MySqlConnectionFactory extends HikariConnectionFactory {
|
public class MySqlConnectionFactory extends DriverBasedHikariConnectionFactory {
|
||||||
public MySqlConnectionFactory(StorageCredentials configuration) {
|
public MySqlConnectionFactory(StorageCredentials configuration) {
|
||||||
super(configuration);
|
super(configuration);
|
||||||
}
|
}
|
||||||
|
@ -47,21 +46,13 @@ public class MySqlConnectionFactory extends HikariConnectionFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) {
|
protected String driverClassName() {
|
||||||
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
return "com.mysql.cj.jdbc.Driver";
|
||||||
config.setJdbcUrl("jdbc:mysql://" + address + ":" + port + "/" + databaseName);
|
|
||||||
config.setUsername(username);
|
|
||||||
config.setPassword(password);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void postInitialize() {
|
protected String driverJdbcIdentifier() {
|
||||||
super.postInitialize();
|
return "mysql";
|
||||||
|
|
||||||
// Calling Class.forName("com.mysql.cj.jdbc.Driver") is enough to call the static initializer
|
|
||||||
// which makes our driver available in DriverManager. We don't want that, so unregister it after
|
|
||||||
// the pool has been setup.
|
|
||||||
deregisterDriver("com.mysql.cj.jdbc.Driver");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -25,13 +25,12 @@
|
||||||
|
|
||||||
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
|
||||||
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class PostgresConnectionFactory extends HikariConnectionFactory {
|
public class PostgresConnectionFactory extends DriverBasedHikariConnectionFactory {
|
||||||
public PostgresConnectionFactory(StorageCredentials configuration) {
|
public PostgresConnectionFactory(StorageCredentials configuration) {
|
||||||
super(configuration);
|
super(configuration);
|
||||||
}
|
}
|
||||||
|
@ -47,13 +46,13 @@ public class PostgresConnectionFactory extends HikariConnectionFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) {
|
protected String driverClassName() {
|
||||||
config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
|
return "org.postgresql.Driver";
|
||||||
config.addDataSourceProperty("serverName", address);
|
}
|
||||||
config.addDataSourceProperty("portNumber", port);
|
|
||||||
config.addDataSourceProperty("databaseName", databaseName);
|
@Override
|
||||||
config.addDataSourceProperty("user", username);
|
protected String driverJdbcIdentifier() {
|
||||||
config.addDataSourceProperty("password", password);
|
return "postgresql";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue