+ * Copyright (c) contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package me.lucko.luckperms.common.dependencies.classloader;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+/**
+ * A classloader "isolated" from the rest of the Minecraft server.
+ *
+ * Used to load specific LuckPerms dependencies without causing conflicts
+ * with other plugins, or libraries provided by the server implementation.
+ */
+public class IsolatedClassLoader extends URLClassLoader {
+
+ public IsolatedClassLoader(URL[] urls) {
+ super(urls, null);
+ }
+
+}
diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/Relocation.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/Relocation.java
index e9ec5abbb..64366c878 100644
--- a/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/Relocation.java
+++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/Relocation.java
@@ -25,23 +25,23 @@
package me.lucko.luckperms.common.dependencies.relocation;
-import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
public final class Relocation {
- public static List of(String name, String... packages) {
- List ret = new ArrayList<>();
- for (String p : packages) {
- ret.add(new Relocation(p.replace("{}", "."), "me.lucko.luckperms.lib." + name));
- }
- return ret;
+ public static Relocation of(String id, String pattern) {
+ return new Relocation(pattern.replace("{}", "."), "me.lucko.luckperms.lib." + id);
+ }
+
+ public static List allOf(Relocation... relocations) {
+ return Arrays.asList(relocations);
}
private final String pattern;
private final String relocatedPattern;
- public Relocation(String pattern, String relocatedPattern) {
+ private Relocation(String pattern, String relocatedPattern) {
this.pattern = pattern;
this.relocatedPattern = relocatedPattern;
}
diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/RelocationHandler.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/RelocationHandler.java
index 26b21b0af..ea304e029 100644
--- a/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/RelocationHandler.java
+++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/RelocationHandler.java
@@ -27,86 +27,55 @@ package me.lucko.luckperms.common.dependencies.relocation;
import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.dependencies.DependencyManager;
+import me.lucko.luckperms.common.dependencies.classloader.IsolatedClassLoader;
import java.io.File;
-import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
-import java.net.URL;
-import java.net.URLClassLoader;
+import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
/**
* Handles class runtime relocation of packages in downloaded dependencies
*/
-public class RelocationHandler implements AutoCloseable {
- private final DependencyManager dependencyManager;
+public class RelocationHandler {
+ private static final Set DEPENDENCIES = EnumSet.of(Dependency.ASM, Dependency.ASM_COMMONS, Dependency.JAR_RELOCATOR);
- private URLClassLoader classLoader;
- private Constructor> relocatorConstructor;
- private Method relocatorRunMethod;
+ private final Constructor> jarRelocatorConstructor;
+ private final Method jarRelocatorRunMethod;
public RelocationHandler(DependencyManager dependencyManager) {
- this.dependencyManager = dependencyManager;
try {
- setup();
+ // download the required dependencies for remapping
+ dependencyManager.loadDependencies(DEPENDENCIES);
+ // get a classloader containing the required dependencies as sources
+ IsolatedClassLoader classLoader = dependencyManager.obtainClassLoaderWith(DEPENDENCIES);
+
+ // load the relocator class
+ Class> jarRelocatorClass = classLoader.loadClass("me.lucko.jarrelocator.JarRelocator");
+
+ // prepare the the reflected constructor & method instances
+ this.jarRelocatorConstructor = jarRelocatorClass.getDeclaredConstructor(File.class, File.class, Map.class);
+ this.jarRelocatorConstructor.setAccessible(true);
+
+ this.jarRelocatorRunMethod = jarRelocatorClass.getDeclaredMethod("run");
+ this.jarRelocatorRunMethod.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
- private void setup() throws Exception {
- File saveDirectory = this.dependencyManager.getSaveDirectory();
-
- // download the required dependencies for the remapping.
- File asm = this.dependencyManager.downloadDependency(saveDirectory, Dependency.ASM);
- File asmCommons = this.dependencyManager.downloadDependency(saveDirectory, Dependency.ASM_COMMONS);
- File jarRelocator = this.dependencyManager.downloadDependency(saveDirectory, Dependency.JAR_RELOCATOR);
-
- URL[] urls = new URL[]{
- asm.toURI().toURL(),
- asmCommons.toURI().toURL(),
- jarRelocator.toURI().toURL()
- };
-
- // construct an isolated classloader instance containing the dependencies needed
- this.classLoader = new URLClassLoader(urls, null);
-
- // load the relocator class
- Class> relocatorClass = this.classLoader.loadClass("me.lucko.jarrelocator.JarRelocator");
-
- // prepare the the reflected constructor & method instances
- this.relocatorConstructor = relocatorClass.getDeclaredConstructor(File.class, File.class, Map.class);
- this.relocatorConstructor.setAccessible(true);
-
- this.relocatorRunMethod = relocatorClass.getDeclaredMethod("run");
- this.relocatorRunMethod.setAccessible(true);
- }
-
public void remap(File input, File output, List relocations) throws Exception {
- if (this.classLoader == null) {
- throw new IllegalStateException("ClassLoader is closed");
- }
-
Map mappings = new HashMap<>();
for (Relocation relocation : relocations) {
mappings.put(relocation.getPattern(), relocation.getRelocatedPattern());
}
// create and invoke a new relocator
- Object relocator = this.relocatorConstructor.newInstance(input, output, mappings);
- this.relocatorRunMethod.invoke(relocator);
- }
-
- @Override
- public void close() throws IOException {
- if (this.classLoader == null) {
- return;
- }
-
- this.classLoader.close();
- this.classLoader = null;
+ Object relocator = this.jarRelocatorConstructor.newInstance(input, output, mappings);
+ this.jarRelocatorRunMethod.invoke(relocator);
}
}
diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java
index 69b7f7e40..b5f5cab2d 100644
--- a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java
+++ b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java
@@ -122,28 +122,33 @@ public class StorageFactory {
private AbstractDao makeDao(StorageType method) {
switch (method) {
case MARIADB:
- return new SqlDao(this.plugin, new MariaDbConnectionFactory(
- this.plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)),
+ return new SqlDao(
+ this.plugin,
+ new MariaDbConnectionFactory(this.plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)),
this.plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)
);
case MYSQL:
- return new SqlDao(this.plugin, new MySqlConnectionFactory(
- this.plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)),
+ return new SqlDao(
+ this.plugin,
+ new MySqlConnectionFactory(this.plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)),
this.plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)
);
case SQLITE:
- return new SqlDao(this.plugin, new SQLiteConnectionFactory(
- new File(this.plugin.getDataDirectory(), "luckperms-sqlite.db")),
+ return new SqlDao(
+ this.plugin,
+ new SQLiteConnectionFactory(this.plugin, new File(this.plugin.getDataDirectory(), "luckperms-sqlite.db")),
this.plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)
);
case H2:
- return new SqlDao(this.plugin, new H2ConnectionFactory(
- new File(this.plugin.getDataDirectory(), "luckperms-h2")),
+ return new SqlDao(
+ this.plugin,
+ new H2ConnectionFactory(this.plugin, new File(this.plugin.getDataDirectory(), "luckperms-h2")),
this.plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)
);
case POSTGRESQL:
- return new SqlDao(this.plugin, new PostgreConnectionFactory(
- this.plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)),
+ return new SqlDao(
+ this.plugin,
+ new PostgreConnectionFactory(this.plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)),
this.plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)
);
case MONGODB:
diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/H2ConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/H2ConnectionFactory.java
index c13f5ce33..7fe4a6a76 100644
--- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/H2ConnectionFactory.java
+++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/H2ConnectionFactory.java
@@ -25,19 +25,27 @@
package me.lucko.luckperms.common.storage.dao.sql.connection.file;
-import org.h2.Driver;
+import me.lucko.luckperms.common.dependencies.Dependency;
+import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URLClassLoader;
import java.sql.Connection;
+import java.sql.Driver;
import java.sql.SQLException;
+import java.util.EnumSet;
import java.util.Properties;
-import java.util.concurrent.locks.ReentrantLock;
public class H2ConnectionFactory extends FlatfileConnectionFactory {
- private final ReentrantLock lock = new ReentrantLock();
+
+ // the driver used to obtain connections
+ private final Driver driver;
+ // the active connection
private NonClosableConnection connection;
- public H2ConnectionFactory(File file) {
+ public H2ConnectionFactory(LuckPermsPlugin plugin, File file) {
super("H2", file);
// backwards compat
@@ -45,20 +53,25 @@ public class H2ConnectionFactory extends FlatfileConnectionFactory {
if (data.exists()) {
data.renameTo(new File(file.getParent(), file.getName() + ".mv.db"));
}
+
+ // setup the classloader
+ URLClassLoader classLoader = plugin.getDependencyManager().obtainClassLoaderWith(EnumSet.of(Dependency.H2_DRIVER));
+ try {
+ Class> driverClass = classLoader.loadClass("org.h2.Driver");
+ Method loadMethod = driverClass.getMethod("load");
+ this.driver = (Driver) loadMethod.invoke(null);
+ } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
}
@Override
- public Connection getConnection() throws SQLException {
- this.lock.lock();
- try {
- if (this.connection == null || this.connection.isClosed()) {
- Connection connection = Driver.load().connect("jdbc:h2:" + this.file.getAbsolutePath(), new Properties());
- if (connection != null) {
- this.connection = new NonClosableConnection(connection);
- }
+ public synchronized Connection getConnection() throws SQLException {
+ if (this.connection == null || this.connection.isClosed()) {
+ Connection connection = this.driver.connect("jdbc:h2:" + this.file.getAbsolutePath(), new Properties());
+ if (connection != null) {
+ this.connection = new NonClosableConnection(connection);
}
- } finally {
- this.lock.unlock();
}
if (this.connection == null) {
diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/SQLiteConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/SQLiteConnectionFactory.java
index 57224df30..7cbf4980d 100644
--- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/SQLiteConnectionFactory.java
+++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/SQLiteConnectionFactory.java
@@ -25,19 +25,26 @@
package me.lucko.luckperms.common.storage.dao.sql.connection.file;
-import org.sqlite.JDBC;
+import me.lucko.luckperms.common.dependencies.Dependency;
+import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.SQLException;
+import java.util.EnumSet;
import java.util.Properties;
-import java.util.concurrent.locks.ReentrantLock;
public class SQLiteConnectionFactory extends FlatfileConnectionFactory {
- private final ReentrantLock lock = new ReentrantLock();
+
+ // the method invoked to obtain new connection instances
+ private final Method createConnectionMethod;
+ // the active connection
private NonClosableConnection connection;
- public SQLiteConnectionFactory(File file) {
+ public SQLiteConnectionFactory(LuckPermsPlugin plugin, File file) {
super("SQLite", file);
// backwards compat
@@ -45,21 +52,37 @@ public class SQLiteConnectionFactory extends FlatfileConnectionFactory {
if (data.exists()) {
data.renameTo(file);
}
+
+ // setup the classloader
+ URLClassLoader classLoader = plugin.getDependencyManager().obtainClassLoaderWith(EnumSet.of(Dependency.SQLITE_DRIVER));
+ try {
+ Class> jdcbClass = classLoader.loadClass("org.sqlite.JDBC");
+ this.createConnectionMethod = jdcbClass.getMethod("createConnection", String.class, Properties.class);
+ } catch (ClassNotFoundException | NoSuchMethodException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private Connection createConnection(String url) throws SQLException {
+ try {
+ return (Connection) this.createConnectionMethod.invoke(null, url, new Properties());
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
+ if (e.getCause() instanceof SQLException) {
+ throw ((SQLException) e.getCause());
+ }
+ throw new RuntimeException(e);
+ }
}
@Override
- public Connection getConnection() throws SQLException {
- this.lock.lock();
- try {
- if (this.connection == null || this.connection.isClosed()) {
- Connection connection = JDBC.createConnection("jdbc:sqlite:" + this.file.getAbsolutePath(), new Properties());
- if (connection != null) {
- this.connection = new NonClosableConnection(connection);
- }
+ public synchronized Connection getConnection() throws SQLException {
+ if (this.connection == null || this.connection.isClosed()) {
+ Connection connection = createConnection("jdbc:sqlite:" + this.file.getAbsolutePath());
+ if (connection != null) {
+ this.connection = new NonClosableConnection(connection);
}
-
- } finally {
- this.lock.unlock();
}
if (this.connection == null) {
diff --git a/sponge/pom.xml b/sponge/pom.xml
index bae019216..f6a766599 100644
--- a/sponge/pom.xml
+++ b/sponge/pom.xml
@@ -64,14 +64,6 @@
org.postgresql
me.lucko.luckperms.lib.postgresql
-
- org.h2
- me.lucko.luckperms.lib.h2
-
-
- org.sqlite
- me.lucko.luckperms.lib.sqlite
-
com.zaxxer.hikari
me.lucko.luckperms.lib.hikari