Rewrote Config handling

This commit is contained in:
Marvin Scham
2023-05-24 00:22:00 +02:00
parent cb57e1c190
commit 69bde92a70
3 changed files with 96 additions and 53 deletions

View File

@@ -1,10 +1,12 @@
package de.rwu.easydrop;
import javax.naming.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.rwu.easydrop.api.client.AmazonProductDataSource;
import de.rwu.easydrop.util.ConfigUtil;
import de.rwu.easydrop.util.Config;
/**
* Kickoff point for the service.
@@ -29,9 +31,10 @@ public final class Main {
*
* @param args
*/
public static void main(final String[] args) {
String amznBaseUrl = ConfigUtil.getConfig("AMAZON_API_URL");
String amznApiKey = ConfigUtil.getConfig("AMAZON_API_KEY");
public static void main(final String[] args) throws ConfigurationException {
Config config = Config.getInstance();
String amznBaseUrl = config.getProperty("AMAZON_API_URL");
String amznApiKey = config.getProperty("AMAZON_API_KEY");
String testProduct = null;
AmazonProductDataSource amznSrc = new AmazonProductDataSource(amznBaseUrl, amznApiKey);

View File

@@ -0,0 +1,89 @@
package de.rwu.easydrop.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Properties;
import javax.naming.ConfigurationException;
/**
* Allows access to the config.
*
* @since 0.1.0
*/
public final class Config {
/**
* Config file location.
*/
private static final String CONFIG_LOCATION = "config/config.properties";
/**
* Holds the config values.
*/
private Properties properties = null;
/**
* Singleton instance.
*/
private static Config instance = null;
/**
* Private constructor to prevent unwanted instantiation.
*/
private Config() throws ConfigurationException {
loadConfig();
}
/**
* Returns current config instance.
*
* @return Config instance
* @throws ConfigurationException
*/
public static Config getInstance() throws ConfigurationException {
if (instance == null) {
return new Config();
}
return instance;
}
/**
* Loads config file values into the instance.
*
* @throws ConfigurationException
*/
public void loadConfig() throws ConfigurationException {
Properties newProps = new Properties();
try (FileInputStream input = new FileInputStream(CONFIG_LOCATION)) {
newProps.load(input);
properties = newProps;
} catch (IOException e) {
throw new ConfigurationException("Couldn't load required config file");
}
}
/**
* Returns a config property by specified key.
*
* @param key Config Key, like "API_KEY"
* @return Config value
* @throws NoSuchElementException Required key missing
*/
public String getProperty(final String key) throws NoSuchElementException {
String value = properties.getProperty(key);
if (value == null) {
throw new NoSuchElementException("Requested config value does not exist");
}
return value;
}
/**
* Overrides a config property loaded from file for current instance.
*
* @param key Config Key
* @param value Property Value
*/
public void setProperty(final String key, final String value) {
properties.setProperty(key, value);
}
}

View File

@@ -1,49 +0,0 @@
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;
}
}