From 175140451de8d18747dabfb3d4571a0b72edcf6f Mon Sep 17 00:00:00 2001 From: Marvin Scham Date: Tue, 23 May 2023 02:57:33 +0200 Subject: [PATCH] Added config utility --- .../java/de/rwu/easydrop/util/ConfigUtil.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/de/rwu/easydrop/util/ConfigUtil.java diff --git a/src/main/java/de/rwu/easydrop/util/ConfigUtil.java b/src/main/java/de/rwu/easydrop/util/ConfigUtil.java new file mode 100644 index 0000000..45eae1b --- /dev/null +++ b/src/main/java/de/rwu/easydrop/util/ConfigUtil.java @@ -0,0 +1,49 @@ +package de.rwu.easydrop.util; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.NoSuchElementException; +import java.util.Properties; + +/** + * Allows access to the config. + * + * @since 0.1.0 + */ +public final class ConfigUtil { + /** + * Config file location. + */ + private static final String CONFIG_LOCATION = "config/config.properties"; + + /** + * Private constructor to prevent unwanted instantiation. + * + * @throws UnsupportedOperationException always + */ + private ConfigUtil() throws UnsupportedOperationException { + throw new UnsupportedOperationException("This is a utility class, don't instantiate it."); + } + + /** + * Returns a config value by specified key. + * + * @param key Config Key, like "API_KEY" + * @return Config value + */ + public static String getConfig(final String key) { + Properties config = new Properties(); + try (FileInputStream input = new FileInputStream(CONFIG_LOCATION)) { + config.load(input); + } catch (IOException e) { + e.printStackTrace(); + } + + String value = config.getProperty(key); + if (value == null) { + throw new NoSuchElementException("Requested config value does not exist"); + } + + return value; + } +}