-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of IsolatedDatabaseHelperFactory
- Loading branch information
Showing
3 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
core/src/main/java/me/mastercapexd/auth/database/IsolatedDatabaseHelperFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |