From 05cae9022f374cdf8537a6e6312e36d7b18785d5 Mon Sep 17 00:00:00 2001 From: P0nk Date: Thu, 9 Sep 2021 08:20:45 +0200 Subject: [PATCH] Move charset loading to fix issue with static fields --- src/main/java/config/YamlConfig.java | 41 ++++--------------- .../constants/string/CharsetConstants.java | 28 ++++++++++++- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/main/java/config/YamlConfig.java b/src/main/java/config/YamlConfig.java index ec9e1c7b56..77a0a9a780 100644 --- a/src/main/java/config/YamlConfig.java +++ b/src/main/java/config/YamlConfig.java @@ -6,53 +6,26 @@ import constants.string.CharsetConstants; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.List; public class YamlConfig { - private static final String CONFIG_FILE_NAME = "config.yaml"; - public static final YamlConfig config = loadConfig(CONFIG_FILE_NAME); + public static final String CONFIG_FILE_NAME = "config.yaml"; + public static final YamlConfig config = loadConfig(); public List worlds; public ServerConfig server; - private static YamlConfig loadConfig(String fileName) { + private static YamlConfig loadConfig() { try { - YamlReader reader = new YamlReader(new FileReader(fileName, CharsetConstants.CHARSET)); + YamlReader reader = new YamlReader(new FileReader(CONFIG_FILE_NAME, CharsetConstants.CHARSET)); YamlConfig config = reader.read(YamlConfig.class); reader.close(); return config; + } catch (FileNotFoundException e) { + throw new RuntimeException("Could not read config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage()); } catch (IOException e) { - throw new RuntimeException(getLoadConfigErrorMessage(e)); - } - } - - private static String getLoadConfigErrorMessage(IOException e) { - if (e instanceof FileNotFoundException) { - return "Could not read config file " + CONFIG_FILE_NAME + ": " + e.getMessage(); - } - - return "Could not successfully parse config file " + CONFIG_FILE_NAME + ": " + e.getMessage(); - } - - public static String loadCharset() { - try { - YamlReader reader = new YamlReader(new FileReader(CONFIG_FILE_NAME, StandardCharsets.US_ASCII)); - reader.getConfig().readConfig.setIgnoreUnknownProperties(true); - StrippedYamlConfig charsetConfig = reader.read(StrippedYamlConfig.class); - reader.close(); - return charsetConfig.server.CHARSET; - } catch (IOException e) { - throw new RuntimeException(getLoadConfigErrorMessage(e)); - } - } - - private static class StrippedYamlConfig { - public StrippedServerConfig server; - - private static class StrippedServerConfig { - public String CHARSET; + throw new RuntimeException("Could not successfully parse config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage()); } } } diff --git a/src/main/java/constants/string/CharsetConstants.java b/src/main/java/constants/string/CharsetConstants.java index 631ddeb255..da20cbfc01 100644 --- a/src/main/java/constants/string/CharsetConstants.java +++ b/src/main/java/constants/string/CharsetConstants.java @@ -13,10 +13,14 @@ package constants.string; * CharsetConstants */ +import com.esotericsoftware.yamlbeans.YamlReader; import config.YamlConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -55,8 +59,22 @@ public class CharsetConstants { } } + private static String loadCharsetFromConfig() { + try { + YamlReader reader = new YamlReader(new FileReader(YamlConfig.CONFIG_FILE_NAME, StandardCharsets.US_ASCII)); + reader.getConfig().readConfig.setIgnoreUnknownProperties(true); + StrippedYamlConfig charsetConfig = reader.read(StrippedYamlConfig.class); + reader.close(); + return charsetConfig.server.CHARSET; + } catch (FileNotFoundException e) { + throw new RuntimeException("Could not read config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage()); + } catch (IOException e) { + throw new RuntimeException("Could not successfully parse charset from config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage()); + } + } + private static Charset loadCharset() { - String configCharset = YamlConfig.loadCharset(); + String configCharset = loadCharsetFromConfig(); if (configCharset != null) { Language language = Language.fromCharset(configCharset); return Charset.forName(language.getCharset()); @@ -64,4 +82,12 @@ public class CharsetConstants { return StandardCharsets.US_ASCII; } + + private static class StrippedYamlConfig { + public StrippedServerConfig server; + + private static class StrippedServerConfig { + public String CHARSET; + } + } } \ No newline at end of file