moved dealing with configuration to its own file, support for naming a different file and command line overrides

This commit is contained in:
Blake J. Harnden 2018-09-11 10:10:16 -07:00
parent bdd469d386
commit 895c3bd5cd
3 changed files with 42 additions and 13 deletions

View file

@ -8,6 +8,7 @@ import com.core.rest.CoreApi;
import com.core.rest.GetConfig;
import com.core.rest.SetConfig;
import com.core.ui.*;
import com.core.utils.ConfigUtils;
import com.core.websocket.CoreWebSocket;
import javafx.application.Application;
import javafx.application.Platform;
@ -73,7 +74,7 @@ public class Controller implements Initializable {
public Controller() {
// load configuration
Properties properties = getConfiguration();
Properties properties = ConfigUtils.load();
String coreUrl = properties.getProperty("core-rest");
logger.info("core rest: {}", coreUrl);
@ -197,17 +198,6 @@ public class Controller implements Initializable {
}
}
private Properties getConfiguration() {
try {
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("/config.properties"));
return properties;
} catch (IOException ex) {
logger.error("error reading config file");
throw new RuntimeException("configuration file did not exist");
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
logger.info("controller initialize");

View file

@ -22,7 +22,6 @@ public class Main extends Application {
// load svg icons
SVGGlyphLoader.loadGlyphsFont(getClass().getResourceAsStream("/icons/icomoon_material.svg"),
"icomoon.svg");
logger.info("icons: {}", SVGGlyphLoader.getAllGlyphsIDs());
// load font
Font.loadFont(getClass().getResourceAsStream("/font/roboto/Roboto-Regular.ttf"), 10);

View file

@ -0,0 +1,40 @@
package com.core.utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.util.Properties;
public final class ConfigUtils {
private static final Logger logger = LogManager.getLogger();
private static final String DEFAULT_CONFIG = "/config.properties";
private ConfigUtils() {
}
public static Properties load() {
String filePath = System.getProperty("config.file", DEFAULT_CONFIG);
logger.info("loading config file: {}", filePath);
try {
Properties properties = new Properties();
properties.load(ConfigUtils.class.getResourceAsStream(filePath));
// override values if provided
for (String key : properties.stringPropertyNames()) {
String value = System.getProperty(key);
if (value != null) {
logger.info("command line config: {} - {}", key, value);
properties.setProperty(key, value);
}
}
return properties;
} catch (IOException ex) {
logger.error("error reading config file");
throw new RuntimeException("configuration file did not exist");
}
}
}