Skip to content

Commit

Permalink
Fix loading of shaded SQL drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jun 25, 2023
1 parent 8e59a14 commit 051f58b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
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;
Expand All @@ -45,6 +43,7 @@
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;
Expand Down Expand Up @@ -155,8 +154,7 @@ private Factory(
}

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);
Expand Down Expand Up @@ -193,13 +191,4 @@ public MySQLUserManager create() {

}

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand Down Expand Up @@ -156,12 +157,8 @@ private Factory(
}

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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 051f58b

Please sign in to comment.