Download dependencies at runtime instead of shading them in the jar

This commit is contained in:
Luck 2016-12-22 21:17:02 +00:00
parent f36f411a8d
commit d3e60487bb
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
12 changed files with 340 additions and 173 deletions

View File

@ -47,26 +47,6 @@
</excludes> </excludes>
</artifactSet> </artifactSet>
<relocations> <relocations>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>me.lucko.luckperms.lib.slf4j</shadedPattern>
</relocation>
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>me.lucko.luckperms.lib.hikari</shadedPattern>
</relocation>
<relocation>
<pattern>com.mongodb</pattern>
<shadedPattern>me.lucko.luckperms.lib.mongodb</shadedPattern>
</relocation>
<relocation>
<pattern>org.bson</pattern>
<shadedPattern>me.lucko.luckperms.lib.bson</shadedPattern>
</relocation>
<relocation>
<pattern>org.h2</pattern>
<shadedPattern>me.lucko.luckperms.lib.h2</shadedPattern>
</relocation>
<relocation> <relocation>
<pattern>com.google.common</pattern> <pattern>com.google.common</pattern>
<shadedPattern>me.lucko.luckperms.lib.guava</shadedPattern> <shadedPattern>me.lucko.luckperms.lib.guava</shadedPattern>
@ -76,16 +56,8 @@
<shadedPattern>me.lucko.luckperms.lib.gson</shadedPattern> <shadedPattern>me.lucko.luckperms.lib.gson</shadedPattern>
</relocation> </relocation>
<relocation> <relocation>
<pattern>redis.clients.jedis</pattern> <pattern>io.github.mkremins.fanciful</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.jedis</shadedPattern> <shadedPattern>me.lucko.luckperms.lib.fanciful</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.util</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.util</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons.pool2</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.pool2</shadedPattern>
</relocation> </relocation>
</relocations> </relocations>
</configuration> </configuration>

View File

@ -44,48 +44,11 @@
</goals> </goals>
<configuration> <configuration>
<minimizeJar>false</minimizeJar> <minimizeJar>false</minimizeJar>
<artifactSet>
<excludes>
<exclude>org.xerial:*</exclude>
</excludes>
</artifactSet>
<relocations> <relocations>
<relocation> <relocation>
<pattern>io.github.mkremins.fanciful</pattern> <pattern>io.github.mkremins.fanciful</pattern>
<shadedPattern>me.lucko.luckperms.lib.fanciful</shadedPattern> <shadedPattern>me.lucko.luckperms.lib.fanciful</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>me.lucko.luckperms.lib.slf4j</shadedPattern>
</relocation>
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>me.lucko.luckperms.lib.hikari</shadedPattern>
</relocation>
<relocation>
<pattern>com.mongodb</pattern>
<shadedPattern>me.lucko.luckperms.lib.mongodb</shadedPattern>
</relocation>
<relocation>
<pattern>org.bson</pattern>
<shadedPattern>me.lucko.luckperms.lib.bson</shadedPattern>
</relocation>
<relocation>
<pattern>org.h2</pattern>
<shadedPattern>me.lucko.luckperms.lib.h2</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.jedis</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.jedis</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.util</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.util</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons.pool2</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.pool2</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
</execution> </execution>
@ -138,6 +101,13 @@
<version>${project.version}</version> <version>${project.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- HikariCP - needed as source -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<!-- Lombok --> <!-- Lombok -->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

View File

@ -49,6 +49,7 @@ import me.lucko.luckperms.common.contexts.ServerCalculator;
import me.lucko.luckperms.common.core.UuidCache; import me.lucko.luckperms.common.core.UuidCache;
import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.data.Importer; import me.lucko.luckperms.common.data.Importer;
import me.lucko.luckperms.common.dependencies.DependencyManager;
import me.lucko.luckperms.common.managers.GroupManager; import me.lucko.luckperms.common.managers.GroupManager;
import me.lucko.luckperms.common.managers.TrackManager; import me.lucko.luckperms.common.managers.TrackManager;
import me.lucko.luckperms.common.managers.UserManager; import me.lucko.luckperms.common.managers.UserManager;
@ -58,6 +59,7 @@ import me.lucko.luckperms.common.managers.impl.GenericUserManager;
import me.lucko.luckperms.common.messaging.RedisMessaging; import me.lucko.luckperms.common.messaging.RedisMessaging;
import me.lucko.luckperms.common.storage.Storage; import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.StorageFactory; import me.lucko.luckperms.common.storage.StorageFactory;
import me.lucko.luckperms.common.storage.StorageType;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.tasks.UpdateTask;
@ -132,6 +134,9 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
getLog().info("Loading configuration..."); getLog().info("Loading configuration...");
configuration = new BukkitConfig(this); configuration = new BukkitConfig(this);
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
DependencyManager.loadDependencies(this, storageTypes);
// setup the Bukkit defaults hook // setup the Bukkit defaults hook
defaultsProvider = new DefaultsProvider(); defaultsProvider = new DefaultsProvider();
childPermissionProvider = new ChildPermissionProvider(); childPermissionProvider = new ChildPermissionProvider();
@ -166,7 +171,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
pm.registerEvents(new BukkitListener(this), this); pm.registerEvents(new BukkitListener(this), this);
// initialise datastore // initialise datastore
storage = StorageFactory.getInstance(this, "h2"); storage = StorageFactory.getInstance(this, StorageType.H2);
// initialise redis // initialise redis
if (getConfiguration().isRedisEnabled()) { if (getConfiguration().isRedisEnabled()) {

View File

@ -49,42 +49,6 @@
<pattern>io.github.mkremins.fanciful</pattern> <pattern>io.github.mkremins.fanciful</pattern>
<shadedPattern>me.lucko.luckperms.lib.fanciful</shadedPattern> <shadedPattern>me.lucko.luckperms.lib.fanciful</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>me.lucko.luckperms.lib.slf4j</shadedPattern>
</relocation>
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>me.lucko.luckperms.lib.hikari</shadedPattern>
</relocation>
<relocation>
<pattern>com.mongodb</pattern>
<shadedPattern>me.lucko.luckperms.lib.mongodb</shadedPattern>
</relocation>
<relocation>
<pattern>org.bson</pattern>
<shadedPattern>me.lucko.luckperms.lib.bson</shadedPattern>
</relocation>
<relocation>
<pattern>org.h2</pattern>
<shadedPattern>me.lucko.luckperms.lib.h2</shadedPattern>
</relocation>
<relocation>
<pattern>org.sqlite</pattern>
<shadedPattern>me.lucko.luckperms.lib.sqlite</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.jedis</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.jedis</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.util</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.util</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons.pool2</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.pool2</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
</execution> </execution>
@ -130,6 +94,13 @@
<version>${project.version}</version> <version>${project.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- slf4j library -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.9</version>
<scope>provided</scope>
</dependency>
<!-- Lombok --> <!-- Lombok -->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

View File

@ -42,6 +42,7 @@ import me.lucko.luckperms.common.contexts.ServerCalculator;
import me.lucko.luckperms.common.core.UuidCache; import me.lucko.luckperms.common.core.UuidCache;
import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.data.Importer; import me.lucko.luckperms.common.data.Importer;
import me.lucko.luckperms.common.dependencies.DependencyManager;
import me.lucko.luckperms.common.managers.GroupManager; import me.lucko.luckperms.common.managers.GroupManager;
import me.lucko.luckperms.common.managers.TrackManager; import me.lucko.luckperms.common.managers.TrackManager;
import me.lucko.luckperms.common.managers.UserManager; import me.lucko.luckperms.common.managers.UserManager;
@ -51,6 +52,7 @@ import me.lucko.luckperms.common.managers.impl.GenericUserManager;
import me.lucko.luckperms.common.messaging.RedisMessaging; import me.lucko.luckperms.common.messaging.RedisMessaging;
import me.lucko.luckperms.common.storage.Storage; import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.StorageFactory; import me.lucko.luckperms.common.storage.StorageFactory;
import me.lucko.luckperms.common.storage.StorageType;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.tasks.UpdateTask;
@ -108,11 +110,14 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
getLog().info("Loading configuration..."); getLog().info("Loading configuration...");
configuration = new BungeeConfig(this); configuration = new BungeeConfig(this);
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
DependencyManager.loadDependencies(this, storageTypes);
// register events // register events
getProxy().getPluginManager().registerListener(this, new BungeeListener(this)); getProxy().getPluginManager().registerListener(this, new BungeeListener(this));
// initialise datastore // initialise datastore
storage = StorageFactory.getInstance(this, "h2"); storage = StorageFactory.getInstance(this, StorageType.H2);
// initialise redis // initialise redis
if (getConfiguration().isRedisEnabled()) { if (getConfiguration().isRedisEnabled()) {

View File

@ -30,7 +30,7 @@
<groupId>me.lucko.luckperms</groupId> <groupId>me.lucko.luckperms</groupId>
<artifactId>luckperms-api</artifactId> <artifactId>luckperms-api</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- fanciful --> <!-- fanciful -->
<dependency> <dependency>
@ -39,47 +39,54 @@
<version>1.1.0</version> <version>1.1.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
<scope>provided</scope>
</dependency>
<!-- HikariCP --> <!-- HikariCP -->
<dependency> <dependency>
<groupId>com.zaxxer</groupId> <groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId> <artifactId>HikariCP</artifactId>
<version>2.5.1</version> <version>2.5.1</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- Jedis --> <!-- Jedis -->
<dependency> <dependency>
<groupId>redis.clients</groupId> <groupId>redis.clients</groupId>
<artifactId>jedis</artifactId> <artifactId>jedis</artifactId>
<version>2.8.1</version> <version>2.8.1</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- SQLite --> <!-- SQLite -->
<dependency> <dependency>
<groupId>org.xerial</groupId> <groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId> <artifactId>sqlite-jdbc</artifactId>
<version>3.8.11.2</version> <version>3.8.11.2</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- H2 --> <!-- H2 -->
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<version>1.4.192</version> <version>1.4.192</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- MongoDB --> <!-- MongoDB -->
<dependency> <dependency>
<groupId>org.mongodb</groupId> <groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId> <artifactId>mongo-java-driver</artifactId>
<version>3.3.0</version> <version>3.3.0</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- slf4j library --> <!-- slf4j library -->
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId> <artifactId>slf4j-simple</artifactId>
<version>1.7.9</version> <version>1.7.9</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- SnakeYAML --> <!-- SnakeYAML -->
<dependency> <dependency>

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum Dependency {
MYSQL_DRIVER("https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar", "5.1.6", "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"),
H2_DRIVER("https://repo1.maven.org/maven2/com/h2database/h2/1.4.193/h2-1.4.193.jar", "1.4.193", "org.h2.Driver"),
SQLITE_DRIVER("https://repo1.maven.org/maven2/org/xerial/sqlite-jdbc/3.15.1/sqlite-jdbc-3.15.1.jar", "3.15.1", "org.sqlite.JDBC"),
HIKARI("https://repo1.maven.org/maven2/com/zaxxer/HikariCP/2.5.1/HikariCP-2.5.1.jar", "2.5.1", "com.zaxxer.hikari.HikariDataSource"),
SLF4J_SIMPLE("https://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.7.9/slf4j-simple-1.7.9.jar", "1.7.9", "org.slf4j.impl.SimpleLoggerFactory"),
SLF4J_API("https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.9/slf4j-api-1.7.9.jar", "1.7.9", "org.slf4j.helpers.BasicMarkerFactory"),
MONGODB_DRIVER("https://repo1.maven.org/maven2/org/mongodb/mongo-java-driver/3.4.1/mongo-java-driver-3.4.1.jar", "3.4.1", "com.mongodb.Mongo"),
JEDIS("https://repo1.maven.org/maven2/redis/clients/jedis/2.9.0/jedis-2.9.0.jar", "2.9.0", "redis.clients.jedis.Jedis"),
APACHE_COMMONS_POOL("https://repo1.maven.org/maven2/org/apache/commons/commons-pool2/2.4.2/commons-pool2-2.4.2.jar", "2.4.1", "org.apache.commons.pool2.PoolUtils");
private final String url;
private final String version;
private final String testClass;
}

View File

@ -0,0 +1,140 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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;
import lombok.experimental.UtilityClass;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import me.lucko.luckperms.api.PlatformType;
import me.lucko.luckperms.common.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.StorageType;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@UtilityClass
public class DependencyManager {
private static Method ADD_URL_METHOD;
static {
try {
ADD_URL_METHOD = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
ADD_URL_METHOD.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
private static final Map<StorageType, List<Dependency>> STORAGE_DEPENDENCIES = ImmutableMap.<StorageType, List<Dependency>>builder()
.put(StorageType.JSON, ImmutableList.of())
.put(StorageType.YAML, ImmutableList.of())
.put(StorageType.MONGODB, ImmutableList.of(Dependency.MONGODB_DRIVER))
.put(StorageType.MYSQL, ImmutableList.of(Dependency.MYSQL_DRIVER, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE, Dependency.HIKARI))
.put(StorageType.SQLITE, ImmutableList.of(Dependency.SQLITE_DRIVER))
.put(StorageType.H2, ImmutableList.of(Dependency.H2_DRIVER))
.build();
public static void loadDependencies(LuckPermsPlugin plugin, Set<StorageType> storageTypes) {
plugin.getLog().info("Loading dependencies...");
List<Dependency> dependencies = new ArrayList<>();
for (StorageType storageType : storageTypes) {
dependencies.addAll(STORAGE_DEPENDENCIES.get(storageType));
}
if (plugin.getConfiguration().isRedisEnabled()) {
dependencies.add(Dependency.APACHE_COMMONS_POOL);
dependencies.add(Dependency.JEDIS);
}
plugin.getLog().info("Identified the following dependencies: " + dependencies.toString());
File data = new File(plugin.getDataFolder(), "lib");
data.mkdirs();
// Download files.
List<Map.Entry<Dependency, File>> toLoad = new ArrayList<>();
for (Dependency dependency : dependencies) {
try {
toLoad.add(Maps.immutableEntry(dependency, downloadDependency(plugin, data, dependency)));
} catch (Throwable e) {
plugin.getLog().severe("Exception whilst downloading dependency " + dependency.name());
e.printStackTrace();
}
}
// Load classes.
for (Map.Entry<Dependency, File> e : toLoad) {
try {
loadJar(plugin, e.getValue(), e.getKey().getTestClass());
} catch (Throwable e1) {
plugin.getLog().severe("Failed to load jar for dependency " + e.getKey().name());
e1.printStackTrace();
}
}
}
private static File downloadDependency(LuckPermsPlugin plugin, File libDir, Dependency dependency) throws Exception {
String name = dependency.name().toLowerCase() + "-" + dependency.getVersion() + ".jar";
URL url = new URL(dependency.getUrl());
File file = new File(libDir, name);
if (file.exists()) {
return file;
}
plugin.getLog().info("Dependency '" + name + "' could not be found. Attempting to download.");
try (InputStream in = url.openStream()) {
Files.copy(in, file.toPath());
}
if (!file.exists()) {
throw new IllegalStateException("File not present. - " + file.toString());
} else {
plugin.getLog().info("Dependency '" + name + "' successfully downloaded.");
return file;
}
}
private static void loadJar(LuckPermsPlugin plugin, File file, String baseClass) throws Exception {
URLClassLoader classLoader = (URLClassLoader) plugin.getClass().getClassLoader();
if (plugin.getType() != PlatformType.SPONGE) {
classLoader = (URLClassLoader) classLoader.getParent();
}
ADD_URL_METHOD.invoke(classLoader, file.toURI().toURL());
classLoader.loadClass(baseClass).newInstance(); // Load a test class
}
}

View File

@ -34,6 +34,7 @@ import me.lucko.luckperms.common.storage.backing.MongoDBBacking;
import me.lucko.luckperms.common.storage.backing.MySQLBacking; import me.lucko.luckperms.common.storage.backing.MySQLBacking;
import me.lucko.luckperms.common.storage.backing.SQLiteBacking; import me.lucko.luckperms.common.storage.backing.SQLiteBacking;
import me.lucko.luckperms.common.storage.backing.YAMLBacking; import me.lucko.luckperms.common.storage.backing.YAMLBacking;
import me.lucko.luckperms.common.utils.ImmutableCollectors;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
@ -43,67 +44,89 @@ import java.util.Set;
@UtilityClass @UtilityClass
public class StorageFactory { public class StorageFactory {
private static final Set<String> TYPES = ImmutableSet.of("json", "yaml", "flatfile", "mongodb", "mysql", "sqlite", "h2");
public static Storage getInstance(LuckPermsPlugin plugin, String defaultMethod) {
Storage storage;
public static Set<StorageType> getRequiredTypes(LuckPermsPlugin plugin, StorageType defaultMethod) {
plugin.getLog().info("Detecting storage method..."); plugin.getLog().info("Detecting storage method...");
if (plugin.getConfiguration().isSplitStorage()) { if (plugin.getConfiguration().isSplitStorage()) {
plugin.getLog().info("Using split storage."); plugin.getLog().info("Loading split storage options.");
Map<String, String> types = plugin.getConfiguration().getSplitStorageOptions();
Map<String, String> types = new HashMap<>(plugin.getConfiguration().getSplitStorageOptions());
types.entrySet().stream() types.entrySet().stream()
.filter(e -> !TYPES.contains(e.getValue().toLowerCase())) .filter(e -> StorageType.parse(e.getValue()) == null)
.forEach(e -> { .forEach(e -> {
plugin.getLog().severe("Storage method for " + e.getKey() + " - " + e.getValue() + " not recognised. " + plugin.getLog().severe("Storage method for " + e.getKey() + " - " + e.getValue() + " not recognised. " +
"Using the default instead."); "Using the default instead.");
e.setValue(defaultMethod); e.setValue(defaultMethod.getIdentifiers().get(0));
}); });
Set<String> neededTypes = new HashSet<>(); Set<String> neededTypes = new HashSet<>();
neededTypes.addAll(types.values()); neededTypes.addAll(types.values());
return neededTypes.stream().map(StorageType::parse).collect(ImmutableCollectors.toImmutableSet());
} else {
String method = plugin.getConfiguration().getStorageMethod();
StorageType type = StorageType.parse(method);
if (type == null) {
plugin.getLog().severe("Storage method '" + method + "' not recognised. Using the default instead.");
type = defaultMethod;
}
return ImmutableSet.of(type);
}
}
public static Storage getInstance(LuckPermsPlugin plugin, StorageType defaultMethod) {
Storage storage;
plugin.getLog().info("Initializing storage backings...");
if (plugin.getConfiguration().isSplitStorage()) {
plugin.getLog().info("Using split storage.");
Map<String, String> types = new HashMap<>(plugin.getConfiguration().getSplitStorageOptions());
types.entrySet().stream()
.filter(e -> StorageType.parse(e.getValue()) == null)
.forEach(e -> e.setValue(defaultMethod.getIdentifiers().get(0)));
Set<String> neededTypes = new HashSet<>();
neededTypes.addAll(types.values());
Map<String, AbstractBacking> backing = new HashMap<>(); Map<String, AbstractBacking> backing = new HashMap<>();
for (String type : neededTypes) { for (String type : neededTypes) {
backing.put(type, backingFromString(type, plugin)); backing.put(type, makeBacking(StorageType.parse(type), plugin));
} }
storage = AbstractStorage.wrap(plugin, new SplitBacking(plugin, backing, types)); storage = AbstractStorage.wrap(plugin, new SplitBacking(plugin, backing, types));
} else { } else {
String storageMethod = plugin.getConfiguration().getStorageMethod().toLowerCase(); String method = plugin.getConfiguration().getStorageMethod().toLowerCase();
if (!TYPES.contains(storageMethod)) { StorageType type = StorageType.parse(method);
plugin.getLog().severe("Storage method '" + storageMethod + "' not recognised. Using the default instead."); if (type == null) {
storageMethod = defaultMethod; type = defaultMethod;
} }
storage = fromString(storageMethod, plugin); storage = makeInstance(type, plugin);
plugin.getLog().info("Using " + storage.getName() + " as storage method.");
} }
plugin.getLog().info("Initialising datastore..."); plugin.getLog().info("Initialising storage provider...");
storage.init(); storage.init();
return storage; return storage;
} }
private static Storage fromString(String storageMethod, LuckPermsPlugin plugin) { private static Storage makeInstance(StorageType type, LuckPermsPlugin plugin) {
return AbstractStorage.wrap(plugin, backingFromString(storageMethod, plugin)); return AbstractStorage.wrap(plugin, makeBacking(type, plugin));
} }
private static AbstractBacking backingFromString(String method, LuckPermsPlugin plugin) { private static AbstractBacking makeBacking(StorageType method, LuckPermsPlugin plugin) {
switch (method) { switch (method) {
case "mysql": case MYSQL:
return new MySQLBacking(plugin, plugin.getConfiguration().getDatabaseValues()); return new MySQLBacking(plugin, plugin.getConfiguration().getDatabaseValues());
case "sqlite": case SQLITE:
return new SQLiteBacking(plugin, new File(plugin.getDataFolder(), "luckperms.sqlite")); return new SQLiteBacking(plugin, new File(plugin.getDataFolder(), "luckperms.sqlite"));
case "h2": case H2:
return new H2Backing(plugin, new File(plugin.getDataFolder(), "luckperms.db")); return new H2Backing(plugin, new File(plugin.getDataFolder(), "luckperms.db"));
case "mongodb": case MONGODB:
return new MongoDBBacking(plugin, plugin.getConfiguration().getDatabaseValues()); return new MongoDBBacking(plugin, plugin.getConfiguration().getDatabaseValues());
case "yaml": case YAML:
return new YAMLBacking(plugin, plugin.getDataFolder()); return new YAMLBacking(plugin, plugin.getDataFolder());
default: default:
return new JSONBacking(plugin, plugin.getDataFolder()); return new JSONBacking(plugin, plugin.getDataFolder());

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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.storage;
import lombok.Getter;
import com.google.common.collect.ImmutableList;
import java.util.List;
public enum StorageType {
JSON("json", "flatfile"),
YAML("yaml", "yml"),
MONGODB("mongodb"),
MYSQL("mysql"),
SQLITE("sqlite"),
H2("h2");
@Getter
private final List<String> identifiers;
StorageType(String... identifiers) {
this.identifiers = ImmutableList.copyOf(identifiers);
}
public static StorageType parse(String name) {
for (StorageType t : values()) {
for (String id : t.getIdentifiers()) {
if (id.equalsIgnoreCase(name)) {
return t;
}
}
}
return null;
}
}

View File

@ -56,34 +56,6 @@
<pattern>io.github.mkremins.fanciful</pattern> <pattern>io.github.mkremins.fanciful</pattern>
<shadedPattern>me.lucko.luckperms.lib.fanciful</shadedPattern> <shadedPattern>me.lucko.luckperms.lib.fanciful</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>me.lucko.luckperms.lib.hikari</shadedPattern>
</relocation>
<relocation>
<pattern>com.mongodb</pattern>
<shadedPattern>me.lucko.luckperms.lib.mongodb</shadedPattern>
</relocation>
<relocation>
<pattern>org.bson</pattern>
<shadedPattern>me.lucko.luckperms.lib.bson</shadedPattern>
</relocation>
<relocation>
<pattern>com.mysql</pattern>
<shadedPattern>me.lucko.luckperms.lib.mysql</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.jedis</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.jedis</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.util</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.util</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons.pool2</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis.pool2</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
</execution> </execution>
@ -142,13 +114,6 @@
<version>${project.version}</version> <version>${project.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
<scope>compile</scope>
</dependency>
<!-- Lombok --> <!-- Lombok -->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

View File

@ -43,11 +43,13 @@ import me.lucko.luckperms.common.contexts.ServerCalculator;
import me.lucko.luckperms.common.core.UuidCache; import me.lucko.luckperms.common.core.UuidCache;
import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.data.Importer; import me.lucko.luckperms.common.data.Importer;
import me.lucko.luckperms.common.dependencies.DependencyManager;
import me.lucko.luckperms.common.managers.TrackManager; import me.lucko.luckperms.common.managers.TrackManager;
import me.lucko.luckperms.common.managers.impl.GenericTrackManager; import me.lucko.luckperms.common.managers.impl.GenericTrackManager;
import me.lucko.luckperms.common.messaging.RedisMessaging; import me.lucko.luckperms.common.messaging.RedisMessaging;
import me.lucko.luckperms.common.storage.Storage; import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.StorageFactory; import me.lucko.luckperms.common.storage.StorageFactory;
import me.lucko.luckperms.common.storage.StorageType;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.tasks.UpdateTask;
@ -163,11 +165,14 @@ public class LPSpongePlugin implements LuckPermsPlugin {
getLog().info("Loading configuration..."); getLog().info("Loading configuration...");
configuration = new SpongeConfig(this); configuration = new SpongeConfig(this);
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
DependencyManager.loadDependencies(this, storageTypes);
// register events // register events
game.getEventManager().registerListeners(this, new SpongeListener(this)); game.getEventManager().registerListeners(this, new SpongeListener(this));
// initialise datastore // initialise datastore
storage = StorageFactory.getInstance(this, "h2"); storage = StorageFactory.getInstance(this, StorageType.H2);
// initialise redis // initialise redis
if (getConfiguration().isRedisEnabled()) { if (getConfiguration().isRedisEnabled()) {