Fix loading of shaded SQL drivers

This commit is contained in:
Jason Penilla committed 2023-06-24 18:04:59 -07:00
1 parent 8e59a14fdb
commit 051f58bdf1
3 files changed
+49 -19

No files matched your search

@@ -24,9 +24,7 @@ import com.google.inject.MembersInjector;
import com.google.inject.Provider;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Driver;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.UUID;
import javax.sql.DataSource;
import net.draycia.carbon.api.CarbonChat;
@@ -45,6 +43,7 @@ import net.draycia.carbon.common.users.db.KeyArgumentFactory;
import net.draycia.carbon.common.users.db.KeyColumnMapper;
import net.draycia.carbon.common.users.db.QueriesLocator;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.draycia.carbon.common.util.SQLDrivers;
import net.kyori.adventure.key.Key;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -155,8 +154,7 @@ public final class MySQLUserManager extends DatabaseUserManager {
}
public MySQLUserManager create() {
ServiceLoader.load(Driver.class).stream()
.forEach(provider -> forceInit(provider.type()));
SQLDrivers.loadFrom(this.getClass().getClassLoader());
final HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setMaximumPoolSize(20);
@@ -193,13 +191,4 @@ public final class MySQLUserManager extends DatabaseUserManager {
}
public static <T> Class<T> forceInit(final Class<T> klass) {
try {
Class.forName(klass.getName(), true, klass.getClassLoader());
} catch (final ClassNotFoundException e) {
throw new AssertionError(e);
}
return klass;
}
}
@@ -42,6 +42,7 @@ import net.draycia.carbon.common.users.db.KeyArgumentFactory;
import net.draycia.carbon.common.users.db.KeyColumnMapper;
import net.draycia.carbon.common.users.db.QueriesLocator;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.draycia.carbon.common.util.SQLDrivers;
import net.kyori.adventure.key.Key;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -156,12 +157,8 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
}
public PostgreSQLUserManager create() {
try {
Class.forName("org.postgresql.Driver");
PluginRegister.REGISTERED_PLUGINS.add(new PostgreSQLDatabaseType());
} catch (final ClassNotFoundException exception) {
throw new RuntimeException("Could not find required class", exception);
}
SQLDrivers.loadFrom(this.getClass().getClassLoader());
PluginRegister.REGISTERED_PLUGINS.add(new PostgreSQLDatabaseType());
final HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setMaximumPoolSize(20);
@@ -0,0 +1,44 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.util;
import java.sql.Driver;
import java.util.ServiceLoader;
public final class SQLDrivers {
private SQLDrivers() {
}
public static void loadFrom(final ClassLoader loader) {
ServiceLoader.load(Driver.class, loader).stream()
.forEach(provider -> forceInit(provider.type()));
}
private static <T> Class<T> forceInit(final Class<T> klass) {
try {
Class.forName(klass.getName(), true, klass.getClassLoader());
} catch (final ClassNotFoundException e) {
throw new AssertionError(e);
}
return klass;
}
}