Skip to content

Commit

Permalink
Load SQL Drivers using isolated ClassLoader (#159)
Browse files Browse the repository at this point in the history
* Add InheritableClassLoader

* Initial implementation of IsolatedDatabaseHelperFactory

* Load driver into IsolatedClassLoader

* Replace useless parameter in DatabaseHelper constructor
  • Loading branch information
bivashy authored Apr 3, 2024
1 parent 1dc63ef commit 5d4f33d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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());

Expand All @@ -84,7 +84,7 @@ public DatabaseHelper(AuthPlugin plugin) {
accountLinkMigrationCoordinator.migrate(connectionSource, accountLinkDao);
}
enabled = true;
} catch(SQLException | IOException e) {
} catch (SQLException | IOException e) {
e.printStackTrace();
}
});
Expand All @@ -97,7 +97,7 @@ private void modifyDatabaseType(Consumer<List<DatabaseType>> consumer) {
List<DatabaseType> databaseTypes = (List<DatabaseType>) field.get(null);
consumer.accept(databaseTypes);
field.setAccessible(false);
} catch(NoSuchFieldException | IllegalAccessException ignored) {
} catch (NoSuchFieldException | IllegalAccessException ignored) {
}
}

Expand All @@ -124,4 +124,5 @@ public AuthAccountDao getAuthAccountDao() {
public AccountLinkDao getAccountLinkDao() {
return accountLinkDao;
}

}

0 comments on commit 5d4f33d

Please sign in to comment.