Skip to content

Commit

Permalink
Initial implementation of IsolatedDatabaseHelperFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
bivashy committed Feb 25, 2024
1 parent 11c2b19 commit bf580f7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,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, null));
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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.mastercapexd.auth.database;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import com.bivashy.auth.api.AuthPlugin;

import me.mastercapexd.auth.util.InheritableClassLoader;

public class IsolatedDatabaseHelperFactory {

private final ClassLoader parentClassLoader;
private final InheritableClassLoader classLoader;

public IsolatedDatabaseHelperFactory(ClassLoader parentClassLoader) {
this.parentClassLoader = parentClassLoader;
this.classLoader = new InheritableClassLoader(parentClassLoader);
}

// TODO: Replace DatabaseHelper with interface to avoid ClassCastException
public DatabaseHelper create(AuthPlugin authPlugin) {
try {
return tryToCreate(authPlugin);
} catch (IOException | ReflectiveOperationException e) {
e.printStackTrace(); // TODO: Replace with more robust logging
return null;
}
}

private DatabaseHelper tryToCreate(
AuthPlugin authPlugin) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
String className = DatabaseHelper.class.getName();
String classPath = '/' + className.replace('.', '/') + ".class";
Class<?> databaseHelperClass = classLoader.defineClass(className, parentClassLoader.getResourceAsStream(classPath));
Constructor<?> databaseHelperConstructor = databaseHelperClass.getConstructor(AuthPlugin.class, ClassLoader.class);
return (DatabaseHelper) databaseHelperConstructor.newInstance(authPlugin, classLoader);
}

}

0 comments on commit bf580f7

Please sign in to comment.