diff options
Diffstat (limited to 'src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java')
-rw-r--r-- | src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java | 73 |
1 files changed, 57 insertions, 16 deletions
diff --git a/src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java b/src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java index 26996c2..fcc0321 100644 --- a/src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java +++ b/src/main/java/com/keuin/crosslink/config/GlobalConfigManager.java @@ -1,35 +1,75 @@ package com.keuin.crosslink.config; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import org.jetbrains.annotations.NotNull; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Objects; +import java.util.Optional; public class GlobalConfigManager { + + private static final Object lock = new Object(); + public static final ObjectMapper mapper = new ObjectMapper(); + + private volatile static GlobalConfigManager instance; + private JsonNode configMessaging; // mutable root node of file "messaging.json" + private JsonNode configApi; // mutable root node of file "api.json" + + private GlobalConfigManager() { + mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); + } + /** - * Load config from disk. + * Load config from disk. Create the global instance. * If loaded successfully, the global 'loaded' status will be set to true. + * * @throws ConfigLoadException failed to load. The 'loaded' status will be set to false. */ - public static void initializeGlobalManager(File configFile) throws ConfigLoadException { - // TODO read config from disk, create the singleton object -// throw new RuntimeException(); + public static void initializeGlobalManager(@NotNull File configFile) throws ConfigLoadException, IOException { + Objects.requireNonNull(configFile); + synchronized (lock) { + if (instance == null) { + throw new IllegalStateException("already initialized"); + } + instance = new GlobalConfigManager(); + } + instance.loadConfig(configFile); + } + + 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("{}")); + } + try (var fis = new FileInputStream(new File(configDirectory, "api.json"))) { + configApi = Optional.ofNullable(mapper.readTree(fis)).orElse(mapper.readTree("{}")); + } } public static @NotNull GlobalConfigManager getInstance() { - // TODO get the singleton object -// throw new RuntimeException(); - throw new RuntimeException("GlobalConfigManager is not initialized"); + final var in = instance; + if (in == null) { + throw new RuntimeException("GlobalConfigManager is not initialized"); + } + return in; } /** - * Get an immutable view of the global config. - * A view is a consistent, but not up-to-date snapshot. - * - * @return the config view. + * Config tree for messaging module. */ - public IConfigView getConfig() { - // TODO - throw new RuntimeException("Global config is not loaded"); + public @NotNull JsonNode messaging() { + return configMessaging.deepCopy(); + } + + /** + * Config tree for HTTP API module. + */ + public @NotNull JsonNode api() { + return configApi.deepCopy(); } public boolean isLoaded() { @@ -39,9 +79,10 @@ public class GlobalConfigManager { /** * Reload the config file from disk. * If loaded successfully, the global 'loaded' status will be set to true. + * * @throws ConfigLoadException failed to reload. - * If previously loaded, the global config won't be modified. - * Otherwise, the 'loaded' status will be set to false. + * If previously loaded, the global config won't be modified. + * Otherwise, the 'loaded' status will be set to false. */ public void reload() throws ConfigLoadException { // TODO |