Added config utility
This commit is contained in:
49
src/main/java/de/rwu/easydrop/util/ConfigUtil.java
Normal file
49
src/main/java/de/rwu/easydrop/util/ConfigUtil.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user