|
| 1 | +package com.github.imdmk.spenttime.configuration; |
| 2 | + |
| 3 | +import eu.okaeri.configs.ConfigManager; |
| 4 | +import eu.okaeri.configs.OkaeriConfig; |
| 5 | +import eu.okaeri.configs.exception.OkaeriException; |
| 6 | +import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer; |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | +import org.yaml.snakeyaml.DumperOptions; |
| 9 | +import org.yaml.snakeyaml.LoaderOptions; |
| 10 | +import org.yaml.snakeyaml.Yaml; |
| 11 | +import org.yaml.snakeyaml.constructor.Constructor; |
| 12 | +import org.yaml.snakeyaml.representer.Representer; |
| 13 | +import org.yaml.snakeyaml.resolver.Resolver; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.util.Objects; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.ConcurrentHashMap; |
| 20 | +import java.util.concurrent.ExecutorService; |
| 21 | +import java.util.concurrent.Executors; |
| 22 | +import java.util.logging.Level; |
| 23 | +import java.util.logging.Logger; |
| 24 | + |
| 25 | +public final class ConfigurationManager { |
| 26 | + |
| 27 | + private final Set<ConfigSection> configs = ConcurrentHashMap.newKeySet(); |
| 28 | + |
| 29 | + private final Logger logger; |
| 30 | + private final ExecutorService executor; |
| 31 | + |
| 32 | + public ConfigurationManager(@NotNull Logger logger) { |
| 33 | + this.logger = Objects.requireNonNull(logger, "logger cannot be null"); |
| 34 | + this.executor = Executors.newSingleThreadExecutor(); |
| 35 | + } |
| 36 | + |
| 37 | + public <T extends ConfigSection> T create(@NotNull Class<T> config, @NotNull File dataFolder) { |
| 38 | + T configFile = ConfigManager.create(config); |
| 39 | + File file = new File(dataFolder, configFile.getFileName()); |
| 40 | + |
| 41 | + YamlSnakeYamlConfigurer yamlSnakeYamlConfigurer = this.createYamlSnakeYamlConfigurer(); |
| 42 | + |
| 43 | + configFile.withConfigurer(yamlSnakeYamlConfigurer); |
| 44 | + configFile.withSerdesPack(configFile.getSerdesPack()); |
| 45 | + configFile.withBindFile(file); |
| 46 | + configFile.withRemoveOrphans(true); |
| 47 | + configFile.saveDefaults(); |
| 48 | + configFile.load(true); |
| 49 | + |
| 50 | + this.configs.add(configFile); |
| 51 | + |
| 52 | + return configFile; |
| 53 | + } |
| 54 | + |
| 55 | + private @NotNull YamlSnakeYamlConfigurer createYamlSnakeYamlConfigurer() { |
| 56 | + LoaderOptions loaderOptions = new LoaderOptions(); |
| 57 | + Constructor constructor = new Constructor(loaderOptions); |
| 58 | + |
| 59 | + DumperOptions dumperOptions = new DumperOptions(); |
| 60 | + dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO); |
| 61 | + dumperOptions.setIndent(2); |
| 62 | + dumperOptions.setSplitLines(false); |
| 63 | + |
| 64 | + Representer representer = new CustomRepresenter(dumperOptions); |
| 65 | + Resolver resolver = new Resolver(); |
| 66 | + |
| 67 | + Yaml yaml = new Yaml(constructor, representer, dumperOptions, loaderOptions, resolver); |
| 68 | + return new YamlSnakeYamlConfigurer(yaml); |
| 69 | + } |
| 70 | + |
| 71 | + public @NotNull CompletableFuture<Void> reloadAll() { |
| 72 | + return CompletableFuture.runAsync(this::loadAll, this.executor); |
| 73 | + } |
| 74 | + |
| 75 | + private void loadAll() { |
| 76 | + this.configs.forEach(this::load); |
| 77 | + } |
| 78 | + |
| 79 | + public void load(@NotNull OkaeriConfig config) { |
| 80 | + try { |
| 81 | + config.load(true); |
| 82 | + } |
| 83 | + catch (OkaeriException exception) { |
| 84 | + this.logger.log(Level.SEVERE, "Failed to load config: " + config.getClass().getSimpleName(), exception); |
| 85 | + throw new ConfigurationLoadException(exception); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public void shutdown() { |
| 90 | + this.logger.info("Shutting down ConfigurationManager executor"); |
| 91 | + this.executor.shutdownNow(); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments