moved dealing with configuration to its own file, support for naming a different file and command line overrides
This commit is contained in:
parent
bdd469d386
commit
895c3bd5cd
3 changed files with 42 additions and 13 deletions
|
@ -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");
|
||||
|
|
|
@ -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);
|
||||
|
|
40
corefx/src/main/java/com/core/utils/ConfigUtils.java
Normal file
40
corefx/src/main/java/com/core/utils/ConfigUtils.java
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue