From d80825733465db5ae45339c841041682e864cda3 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 12 Mar 2022 21:50:58 +0800 Subject: Fix config reloading. --- .../crosslink/config/GlobalConfigManager.java | 36 ++++++++++++++++------ .../keuin/crosslink/plugin/common/PluginMain.java | 6 ++-- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java b/src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java index fcc0321..a09fc05 100644 --- a/src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java +++ b/src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java @@ -20,8 +20,11 @@ public class GlobalConfigManager { private JsonNode configMessaging; // mutable root node of file "messaging.json" private JsonNode configApi; // mutable root node of file "api.json" - private GlobalConfigManager() { + private final File configFileDirectory; + + private GlobalConfigManager(File configFileDirectory) { mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); + this.configFileDirectory = configFileDirectory; } /** @@ -30,24 +33,37 @@ public class GlobalConfigManager { * * @throws ConfigLoadException failed to load. The 'loaded' status will be set to false. */ - public static void initializeGlobalManager(@NotNull File configFile) throws ConfigLoadException, IOException { - Objects.requireNonNull(configFile); + public static void initializeGlobalManager(@NotNull File configFileDirectory) throws ConfigLoadException, IOException { + Objects.requireNonNull(configFileDirectory); synchronized (lock) { if (instance == null) { throw new IllegalStateException("already initialized"); } - instance = new GlobalConfigManager(); + instance = new GlobalConfigManager(configFileDirectory); } - instance.loadConfig(configFile); + instance.loadConfig(); } - private void loadConfig(File configDirectory) throws IOException { - try (var fis = new FileInputStream(new File(configDirectory, "messaging.json"))) { - configMessaging = Optional.ofNullable(mapper.readTree(fis)).orElse(mapper.readTree("{}")); + public static void destroyGlobalInstance() { + synchronized (lock) { + instance = null; + } + } + + /** + * Load config from file. + */ + public void loadConfig() throws IOException { + JsonNode newConfigMessaging, newConfigApi; + try (var fis = new FileInputStream(new File(configFileDirectory, "messaging.json"))) { + newConfigMessaging = Optional.ofNullable(mapper.readTree(fis)).orElse(mapper.readTree("{}")); } - try (var fis = new FileInputStream(new File(configDirectory, "api.json"))) { - configApi = Optional.ofNullable(mapper.readTree(fis)).orElse(mapper.readTree("{}")); + try (var fis = new FileInputStream(new File(configFileDirectory, "api.json"))) { + newConfigApi = Optional.ofNullable(mapper.readTree(fis)).orElse(mapper.readTree("{}")); } + // make those two updates atomic (if any exception appeared) + configMessaging = newConfigMessaging; + configApi = newConfigApi; } public static @NotNull GlobalConfigManager getInstance() { diff --git a/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java b/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java index aa97a0d..9b0d012 100644 --- a/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java +++ b/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java @@ -134,8 +134,7 @@ public final class PluginMain { // TODO refactor setup and teardown routine, split into hooks logger.info("Loading config from disk..."); try { - GlobalConfigManager.initializeGlobalManager(new File( - environment.pluginDataPath().toFile(), "messaging.json")); + GlobalConfigManager.initializeGlobalManager(environment.pluginDataPath().toFile()); } catch (ConfigLoadException | IOException ex) { logger.error("Failed to load configuration", ex); throw new RuntimeException(ex); @@ -149,12 +148,13 @@ public final class PluginMain { logger.info("Stopping API server."); apiServer.shutdown(); logger.info("CrossLink is disabled."); + GlobalConfigManager.destroyGlobalInstance(); } // may throw unchecked exception public void reload() { disable(); - initialize(); + enable(); } private String capital(String s) { -- cgit v1.2.3