From 5d4f33d25ae7441d325e522a22a218ca25f3f9e2 Mon Sep 17 00:00:00 2001 From: bivashy <85439143+bivashy@users.noreply.github.com> Date: Wed, 3 Apr 2024 21:40:46 +0500 Subject: [PATCH] Load SQL Drivers using isolated ClassLoader (#159) * Add InheritableClassLoader * Initial implementation of IsolatedDatabaseHelperFactory * Load driver into IsolatedClassLoader * Replace useless parameter in DatabaseHelper constructor --- .../main/java/me/mastercapexd/auth/BaseAuthPlugin.java | 4 +++- .../me/mastercapexd/auth/database/DatabaseHelper.java | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java b/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java index d22dc5bf..5e5d7d68 100644 --- a/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java +++ b/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java @@ -9,6 +9,7 @@ import java.util.concurrent.Executors; import java.util.stream.IntStream; +import com.alessiodp.libby.classloader.IsolatedClassLoader; import com.bivashy.auth.api.AuthPlugin; import com.bivashy.auth.api.AuthPluginProvider; import com.bivashy.auth.api.account.AccountFactory; @@ -155,7 +156,8 @@ private void initializeBasic() { this.authenticationStepContextFactoryBucket = new BaseAuthenticationStepContextFactoryBucket(config.getAuthenticationSteps()); this.accountFactory = new AuthAccountFactory(); this.linkTypeProvider = BaseLinkTypeProvider.allLinks(); - this.accountDatabase = new AuthAccountDatabaseProxy(new DatabaseHelper(this)); + // TODO: Replace this with IsolatedDatabaseHelperFactory + this.accountDatabase = new AuthAccountDatabaseProxy(new DatabaseHelper(this, new IsolatedClassLoader())); this.loginManagement = new BaseLoginManagement(this); this.registerAuthenticationSteps(); diff --git a/core/src/main/java/me/mastercapexd/auth/database/DatabaseHelper.java b/core/src/main/java/me/mastercapexd/auth/database/DatabaseHelper.java index 40e53db1..e692beae 100644 --- a/core/src/main/java/me/mastercapexd/auth/database/DatabaseHelper.java +++ b/core/src/main/java/me/mastercapexd/auth/database/DatabaseHelper.java @@ -43,7 +43,7 @@ public class DatabaseHelper { private AuthAccountDao authAccountDao; private AccountLinkDao accountLinkDao; - public DatabaseHelper(AuthPlugin plugin) { + public DatabaseHelper(AuthPlugin plugin, ClassLoader classLoader) { DatabaseSettings databaseConfiguration = plugin.getConfig().getDatabaseConfiguration(); SchemaSettings schemaSettings = databaseConfiguration.getSchemaSettings(); @@ -62,7 +62,7 @@ public DatabaseHelper(AuthPlugin plugin) { String cacheDriverCheckSum = HashUtils.getFileCheckSum(cacheDriverFile, HashUtils.getMD5()); if (!cacheDriverFile.exists() || cacheDriverCheckSum != null && !DownloadUtil.checkSum(HashUtils.mapToMd5URL(downloadUrl), cacheDriverCheckSum)) DownloadUtil.downloadFile(downloadUrl, cacheDriverFile); - DriverUtil.loadDriver(cacheDriverFile, plugin.getClass().getClassLoader()); + DriverUtil.loadDriver(cacheDriverFile, classLoader); DataPersisterManager.registerDataPersisters(new CryptoProviderPersister()); @@ -84,7 +84,7 @@ public DatabaseHelper(AuthPlugin plugin) { accountLinkMigrationCoordinator.migrate(connectionSource, accountLinkDao); } enabled = true; - } catch(SQLException | IOException e) { + } catch (SQLException | IOException e) { e.printStackTrace(); } }); @@ -97,7 +97,7 @@ private void modifyDatabaseType(Consumer> consumer) { List databaseTypes = (List) field.get(null); consumer.accept(databaseTypes); field.setAccessible(false); - } catch(NoSuchFieldException | IllegalAccessException ignored) { + } catch (NoSuchFieldException | IllegalAccessException ignored) { } } @@ -124,4 +124,5 @@ public AuthAccountDao getAuthAccountDao() { public AccountLinkDao getAccountLinkDao() { return accountLinkDao; } + }