Added config utility

This commit is contained in:
Marvin Scham
2023-05-23 02:57:33 +02:00
parent cadbe04a5d
commit 175140451d

View File

@@ -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;
}
}