From 0eba5f1cbc6b81b8f420f7987b84ea1ce3f0ab41 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 22 Jan 2018 23:21:47 +0000 Subject: [PATCH] Add a means to provide a custom Storage dao implementation (#590) --- .../dependencies/DependencyRegistry.java | 1 + .../common/storage/StorageFactory.java | 3 ++ .../luckperms/common/storage/StorageType.java | 3 +- .../storage/provider/StorageProvider.java | 39 ++++++++++++++++ .../storage/provider/StorageProviders.java | 45 +++++++++++++++++++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/storage/provider/StorageProvider.java create mode 100644 common/src/main/java/me/lucko/luckperms/common/storage/provider/StorageProviders.java diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java index 4ed2c5842..f9b97703b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java @@ -50,6 +50,7 @@ public class DependencyRegistry { .put(StorageType.POSTGRESQL, ImmutableList.of(Dependency.POSTGRESQL_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)) + .put(StorageType.CUSTOM, ImmutableList.of()) .build(); private final LuckPermsPlugin plugin; 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 b5f5cab2d..ab519eb3e 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 @@ -42,6 +42,7 @@ import me.lucko.luckperms.common.storage.dao.sql.connection.file.SQLiteConnectio import me.lucko.luckperms.common.storage.dao.sql.connection.hikari.MariaDbConnectionFactory; import me.lucko.luckperms.common.storage.dao.sql.connection.hikari.MySqlConnectionFactory; import me.lucko.luckperms.common.storage.dao.sql.connection.hikari.PostgreConnectionFactory; +import me.lucko.luckperms.common.storage.provider.StorageProviders; import me.lucko.luckperms.common.utils.ImmutableCollectors; import java.io.File; @@ -121,6 +122,8 @@ public class StorageFactory { private AbstractDao makeDao(StorageType method) { switch (method) { + case CUSTOM: + return StorageProviders.getProvider().provide(this.plugin); case MARIADB: return new SqlDao( this.plugin, 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 1aa9c96c7..90245fb73 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 @@ -39,7 +39,8 @@ public enum StorageType { MYSQL("MySQL", "mysql"), POSTGRESQL("PostgreSQL", "postgresql"), SQLITE("SQLite", "sqlite"), - H2("H2", "h2"); + H2("H2", "h2"), + CUSTOM("Custom", "custom"); private final String name; diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/provider/StorageProvider.java b/common/src/main/java/me/lucko/luckperms/common/storage/provider/StorageProvider.java new file mode 100644 index 000000000..c2ff61334 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/storage/provider/StorageProvider.java @@ -0,0 +1,39 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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.storage.provider; + +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.storage.dao.AbstractDao; + +/** + * A storage provider + */ +@FunctionalInterface +public interface StorageProvider { + + AbstractDao provide(LuckPermsPlugin plugin); + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/provider/StorageProviders.java b/common/src/main/java/me/lucko/luckperms/common/storage/provider/StorageProviders.java new file mode 100644 index 000000000..a058228a4 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/storage/provider/StorageProviders.java @@ -0,0 +1,45 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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.storage.provider; + +public final class StorageProviders { + private static StorageProvider provider = null; + + public static void register(StorageProvider provider) { + StorageProviders.provider = provider; + } + + public static StorageProvider getProvider() { + if (provider == null) { + throw new IllegalStateException("Provider not present."); + } + + return provider; + } + + private StorageProviders() {} + +}