From 2c62de9658c0318f3b7dcf37fcb4b2a2719eec79 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 23 Apr 2019 21:57:43 +0100 Subject: [PATCH] Refactor storage type config read --- .../migration/MigrationPowerfulPerms.java | 6 +-- .../api/implementation/ApiConfiguration.java | 4 +- .../luckperms/common/config/ConfigKeys.java | 19 ++++---- .../plugin/AbstractLuckPermsPlugin.java | 4 +- .../common/storage/StorageFactory.java | 43 +++---------------- .../luckperms/common/storage/StorageType.java | 4 +- 6 files changed, 25 insertions(+), 55 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java index 510f91a46..ba497d09f 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java @@ -89,10 +89,8 @@ public class MigrationPowerfulPerms extends SubCommand { return CommandResult.STATE_ERROR; } - String method = plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD); - StorageType type = StorageType.parse(method); - - if (type == null || type != StorageType.MYSQL) { + StorageType type = plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD); + if (type != StorageType.MYSQL) { // We need to load the Hikari/MySQL stuff. plugin.getDependencyManager().loadStorageDependencies(ImmutableSet.of(StorageType.MYSQL)); } diff --git a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiConfiguration.java index f161277bc..b7733f9a1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiConfiguration.java @@ -72,7 +72,7 @@ public class ApiConfiguration implements LPConfiguration { @Override public @NonNull String getStorageMethod() { - return this.handle.get(ConfigKeys.STORAGE_METHOD); + return this.handle.get(ConfigKeys.STORAGE_METHOD).getName(); } @Override @@ -83,7 +83,7 @@ public class ApiConfiguration implements LPConfiguration { @Override public @NonNull Map getSplitStorageOptions() { return this.handle.get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream() - .collect(ImmutableCollectors.toMap(e -> e.getKey().name().toLowerCase(), Map.Entry::getValue)); + .collect(ImmutableCollectors.toMap(e -> e.getKey().name().toLowerCase(), e -> e.getValue().getName())); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index 67b7fbfa6..b98b3bcd1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -43,6 +43,7 @@ import me.lucko.luckperms.common.primarygroup.AllParentsByWeightHolder; import me.lucko.luckperms.common.primarygroup.ParentsByWeightHolder; import me.lucko.luckperms.common.primarygroup.PrimaryGroupHolder; import me.lucko.luckperms.common.primarygroup.StoredHolder; +import me.lucko.luckperms.common.storage.StorageType; import me.lucko.luckperms.common.storage.implementation.split.SplitStorageType; import me.lucko.luckperms.common.storage.misc.StorageCredentials; import me.lucko.luckperms.common.util.ImmutableCollectors; @@ -474,7 +475,9 @@ public final class ConfigKeys { /** * The name of the storage method being used */ - public static final ConfigKey STORAGE_METHOD = enduringKey(lowercaseStringKey("storage-method", "h2")); + public static final ConfigKey STORAGE_METHOD = enduringKey(customKey(c -> { + return StorageType.parse(c.getString("storage-method", "h2"), StorageType.H2); + })); /** * If storage files should be monitored for changes @@ -489,13 +492,13 @@ public final class ConfigKeys { /** * The options for split storage */ - public static final ConfigKey> SPLIT_STORAGE_OPTIONS = enduringKey(customKey(c -> { - EnumMap map = new EnumMap<>(SplitStorageType.class); - map.put(SplitStorageType.USER, c.getString("split-storage.methods.user", "h2").toLowerCase()); - map.put(SplitStorageType.GROUP, c.getString("split-storage.methods.group", "h2").toLowerCase()); - map.put(SplitStorageType.TRACK, c.getString("split-storage.methods.track", "h2").toLowerCase()); - map.put(SplitStorageType.UUID, c.getString("split-storage.methods.uuid", "h2").toLowerCase()); - map.put(SplitStorageType.LOG, c.getString("split-storage.methods.log", "h2").toLowerCase()); + public static final ConfigKey> SPLIT_STORAGE_OPTIONS = enduringKey(customKey(c -> { + EnumMap map = new EnumMap<>(SplitStorageType.class); + map.put(SplitStorageType.USER, StorageType.parse(c.getString("split-storage.methods.user", "h2"), StorageType.H2)); + map.put(SplitStorageType.GROUP, StorageType.parse(c.getString("split-storage.methods.group", "h2"), StorageType.H2)); + map.put(SplitStorageType.TRACK, StorageType.parse(c.getString("split-storage.methods.track", "h2"), StorageType.H2)); + map.put(SplitStorageType.UUID, StorageType.parse(c.getString("split-storage.methods.uuid", "h2"), StorageType.H2)); + map.put(SplitStorageType.LOG, StorageType.parse(c.getString("split-storage.methods.log", "h2"), StorageType.H2)); return ImmutableMap.copyOf(map); })); diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java index 66d51718a..a5355dd46 100644 --- a/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java @@ -115,7 +115,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { // now the configuration is loaded, we can create a storage factory and load initial dependencies StorageFactory storageFactory = new StorageFactory(this); - Set storageTypes = storageFactory.getRequiredTypes(StorageType.H2); + Set storageTypes = storageFactory.getRequiredTypes(); this.dependencyManager.loadStorageDependencies(storageTypes); // register listeners @@ -132,7 +132,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { } // initialise storage - this.storage = storageFactory.getInstance(StorageType.H2); + this.storage = storageFactory.getInstance(); this.messagingService = provideMessagingFactory().getInstance(); // setup the update task buffer 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 eb7b3b8b2..151b1f0ce 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 @@ -26,7 +26,6 @@ package me.lucko.luckperms.common.storage; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -59,45 +58,20 @@ public class StorageFactory { this.plugin = plugin; } - public Set getRequiredTypes(StorageType defaultMethod) { + public Set getRequiredTypes() { if (this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) { - return this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream() - .map(e -> { - StorageType type = StorageType.parse(e.getValue()); - if (type == null) { - this.plugin.getLogger().severe("Storage method for " + e.getKey() + " - " + e.getValue() + " not recognised. " + - "Using the default instead."); - type = defaultMethod; - } - return type; - }) - .collect(ImmutableCollectors.toEnumSet(StorageType.class)); + return ImmutableSet.copyOf(this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).values()); } else { - String method = this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD); - StorageType type = StorageType.parse(method); - if (type == null) { - this.plugin.getLogger().severe("Storage method '" + method + "' not recognised. Using the default instead."); - type = defaultMethod; - } - return ImmutableSet.of(type); + return ImmutableSet.of(this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD)); } } - public Storage getInstance(StorageType defaultMethod) { + public Storage getInstance() { Storage storage; if (this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) { this.plugin.getLogger().info("Loading storage provider... [SPLIT STORAGE]"); - Map mappedTypes = this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream() - .map(e -> { - StorageType type = StorageType.parse(e.getValue()); - if (type == null) { - type = defaultMethod; - } - return Maps.immutableEntry(e.getKey(), type); - }) - .collect(ImmutableCollectors.toEnumMap(SplitStorageType.class, Map.Entry::getKey, Map.Entry::getValue)); - + Map mappedTypes = this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS); Map backing = mappedTypes.values().stream() .distinct() .collect(ImmutableCollectors.toEnumMap(StorageType.class, e -> e, this::createNewImplementation)); @@ -106,12 +80,7 @@ public class StorageFactory { storage = new Storage(this.plugin, new SplitStorage(this.plugin, backing, mappedTypes)); } else { - String method = this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD); - StorageType type = StorageType.parse(method); - if (type == null) { - type = defaultMethod; - } - + StorageType type = this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD); this.plugin.getLogger().info("Loading storage provider... [" + type.name() + "]"); storage = makeInstance(type); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/StorageType.java b/common/src/main/java/me/lucko/luckperms/common/storage/StorageType.java index 2d794b4e0..6973ce4ca 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/StorageType.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/StorageType.java @@ -63,7 +63,7 @@ public enum StorageType { this.identifiers = ImmutableList.copyOf(identifiers); } - public static StorageType parse(String name) { + public static StorageType parse(String name, StorageType def) { for (StorageType t : values()) { for (String id : t.getIdentifiers()) { if (id.equalsIgnoreCase(name)) { @@ -71,7 +71,7 @@ public enum StorageType { } } } - return null; + return def; } public String getName() {