corefx - migrated to using a json based configuration file for more robust configuration information

This commit is contained in:
Blake J. Harnden 2018-11-26 15:17:10 -08:00
parent eacb4d0cc0
commit 0378c66103
10 changed files with 112 additions and 68 deletions

View file

@ -7,6 +7,7 @@ import com.core.graph.NetworkGraph;
import com.core.ui.*;
import com.core.ui.dialogs.*;
import com.core.utils.ConfigUtils;
import com.core.utils.Configuration;
import com.core.websocket.CoreWebSocket;
import com.jfoenix.controls.JFXProgressBar;
import javafx.application.Application;
@ -48,7 +49,7 @@ public class Controller implements Initializable {
private Application application;
private Stage window;
private Properties properties;
private Configuration configuration;
// core client utilities
private ICoreClient coreClient = new CoreRestClient();
@ -299,11 +300,19 @@ public class Controller implements Initializable {
private void onOpenXmlAction() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Session");
String xmlPath = properties.getProperty(ConfigUtils.XML_PATH);
fileChooser.setInitialDirectory(new File(xmlPath));
fileChooser.setInitialDirectory(new File(configuration.getXmlPath()));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XML", "*.xml"));
try {
File file = fileChooser.showOpenDialog(window);
if (file != null) {
openXml(file);
}
} catch (IllegalArgumentException ex) {
Toast.error(String.format("Invalid XML directory: %s", configuration.getXmlPath()));
}
}
private void openXml(File file) {
logger.info("opening session xml: {}", file.getPath());
try {
SessionOverview sessionOverview = coreClient.openSession(file);
@ -311,8 +320,7 @@ public class Controller implements Initializable {
joinSession(sessionId);
Toast.info(String.format("Joined Session %s", sessionId));
} catch (IOException ex) {
logger.error("error opening session xml", ex);
}
Toast.error("Error opening session xml", ex);
}
}
@ -321,8 +329,7 @@ public class Controller implements Initializable {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Session");
fileChooser.setInitialFileName("session.xml");
String xmlPath = properties.getProperty(ConfigUtils.XML_PATH);
fileChooser.setInitialDirectory(new File(xmlPath));
fileChooser.setInitialDirectory(new File(configuration.getXmlPath()));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XML", "*.xml"));
File file = fileChooser.showSaveDialog(window);
if (file != null) {
@ -330,7 +337,7 @@ public class Controller implements Initializable {
try {
coreClient.saveSession(file);
} catch (IOException ex) {
logger.error("error saving session xml", ex);
Toast.error("Error saving session xml", ex);
}
}
}
@ -385,8 +392,8 @@ public class Controller implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
coreWebSocket = new CoreWebSocket(this);
properties = ConfigUtils.load();
String coreUrl = properties.getProperty(ConfigUtils.REST_URL);
configuration = ConfigUtils.load();
String coreUrl = configuration.getCoreRest();
logger.info("core rest: {}", coreUrl);
connectDialog.setCoreUrl(coreUrl);
connectToCore(coreUrl);

View file

@ -3,7 +3,6 @@ package com.core.graph;
import com.core.Controller;
import com.core.data.*;
import com.core.ui.Toast;
import com.core.utils.ConfigUtils;
import com.core.utils.IconUtils;
import com.google.common.base.Supplier;
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
@ -118,7 +117,7 @@ public class NetworkGraph {
if (mouseEvent.getClickCount() == 2 && controller.getCoreClient().isRunning()) {
try {
String shellCommand = controller.getProperties().getProperty(ConfigUtils.SHELL_COMMAND);
String shellCommand = controller.getConfiguration().getShellCommand();
String terminalCommand = controller.getCoreClient().getTerminalCommand(node);
terminalCommand = String.format("%s %s", shellCommand, terminalCommand);
logger.info("launching node terminal: {}", terminalCommand);

View file

@ -3,6 +3,7 @@ package com.core.ui.dialogs;
import com.core.Controller;
import com.core.ui.Toast;
import com.core.utils.ConfigUtils;
import com.core.utils.Configuration;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import javafx.event.ActionEvent;
@ -12,7 +13,6 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.util.Properties;
public class GuiPreferencesDialog extends StageDialog {
private static final Logger logger = LogManager.getLogger();
@ -30,12 +30,12 @@ public class GuiPreferencesDialog extends StageDialog {
}
private EventHandler<ActionEvent> onSave = event -> {
Properties properties = getController().getProperties();
properties.setProperty(ConfigUtils.XML_PATH, xmlFilePathTextField.getText());
properties.setProperty(ConfigUtils.MOBILITY_PATH, mobilityFilePathTextField.getText());
properties.setProperty(ConfigUtils.SHELL_COMMAND, shellCommandTextField.getText());
Configuration configuration = getController().getConfiguration();
configuration.setXmlPath(xmlFilePathTextField.getText());
configuration.setMobilityPath(mobilityFilePathTextField.getText());
configuration.setShellCommand(shellCommandTextField.getText());
try {
ConfigUtils.save(properties);
ConfigUtils.save(configuration);
Toast.success("Updated preferences");
} catch (IOException ex) {
Toast.error("Failure to update preferences", ex);
@ -44,10 +44,10 @@ public class GuiPreferencesDialog extends StageDialog {
};
public void showDialog() {
Properties properties = getController().getProperties();
xmlFilePathTextField.setText(properties.getProperty(ConfigUtils.XML_PATH));
mobilityFilePathTextField.setText(properties.getProperty(ConfigUtils.MOBILITY_PATH));
shellCommandTextField.setText(properties.getProperty(ConfigUtils.SHELL_COMMAND));
Configuration configuration = getController().getConfiguration();
xmlFilePathTextField.setText(configuration.getXmlPath());
mobilityFilePathTextField.setText(configuration.getMobilityPath());
shellCommandTextField.setText(configuration.getShellCommand());
show();
}
}

View file

@ -4,7 +4,6 @@ import com.core.Controller;
import com.core.data.CoreNode;
import com.core.data.MobilityConfig;
import com.core.ui.Toast;
import com.core.utils.ConfigUtils;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import com.jfoenix.controls.JFXToggleButton;
@ -73,15 +72,20 @@ public class MobilityDialog extends StageDialog {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select File");
String mobilityPath = getController().getProperties().getProperty(ConfigUtils.MOBILITY_PATH);
String mobilityPath = getController().getConfiguration().getMobilityPath();
fileChooser.setInitialDirectory(new File(mobilityPath));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Mobility",
"*.mobility"));
try {
File file = fileChooser.showOpenDialog(getController().getWindow());
if (file != null) {
logger.info("opening session xml: {}", file.getPath());
textField.setText(file.getPath());
}
} catch (IllegalArgumentException ex) {
Toast.error(String.format("Invalid mobility directory: %s",
getController().getConfiguration().getMobilityPath()));
}
}
public void showDialog(CoreNode node) {

View file

@ -4,36 +4,38 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
public final class ConfigUtils {
private static final Logger logger = LogManager.getLogger();
private static final String DEFAULT_CONFIG = "/config.properties";
private static final String CONFIG_FILE_NAME = "config.json";
private static final String DEFAULT_CONFIG = "/" + CONFIG_FILE_NAME;
private static final Path HOME = Paths.get(System.getProperty("user.home"), ".core");
private static final Path PROPERTIES_FILE = Paths.get(HOME.toString(), "config.properties");
private static final Path CONFIG_FILE = Paths.get(HOME.toString(), CONFIG_FILE_NAME);
private static final Path XML_DIR = Paths.get(HOME.toString(), "xml");
private static final Path MOBILITY_DIR = Paths.get(HOME.toString(), "mobility");
// config fields
public static final String REST_URL = "core-rest";
public static final String XML_PATH = "xml-path";
public static final String MOBILITY_PATH = "mobility-path";
public static final String SHELL_COMMAND = "shell-command";
private ConfigUtils() {
}
public static void save(Properties properties) throws IOException {
properties.store(new FileOutputStream(PROPERTIES_FILE.toFile()), null);
public static void save(Configuration configuration) throws IOException {
String fileData = JsonUtils.toPrettyString(configuration);
try (PrintWriter out = new PrintWriter(CONFIG_FILE.toFile())) {
out.println(fileData);
}
}
public static Properties load() {
private static Configuration readConfig() throws IOException {
return JsonUtils.read(new FileInputStream(CONFIG_FILE.toFile()), Configuration.class);
}
public static Configuration load() {
try {
if (!HOME.toFile().exists()) {
logger.info("creating core home directory");
@ -42,28 +44,19 @@ public final class ConfigUtils {
Files.createDirectory(MOBILITY_DIR);
}
Properties properties = new Properties();
if (!PROPERTIES_FILE.toFile().exists()) {
Configuration configuration;
if (!CONFIG_FILE.toFile().exists()) {
logger.info("creating default configuration");
Files.copy(ConfigUtils.class.getResourceAsStream(DEFAULT_CONFIG), PROPERTIES_FILE);
properties.load(new FileInputStream(PROPERTIES_FILE.toFile()));
properties.setProperty(XML_PATH, XML_DIR.toString());
properties.setProperty(MOBILITY_PATH, MOBILITY_DIR.toString());
save(properties);
Files.copy(ConfigUtils.class.getResourceAsStream(DEFAULT_CONFIG), CONFIG_FILE);
configuration = readConfig();
configuration.setXmlPath(XML_DIR.toString());
configuration.setMobilityPath(MOBILITY_DIR.toString());
save(configuration);
} else {
properties.load(new FileInputStream(PROPERTIES_FILE.toFile()));
configuration = readConfig();
}
// 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;
return configuration;
} catch (IOException ex) {
logger.error("error reading config file");
throw new RuntimeException("configuration file did not exist");

View file

@ -0,0 +1,17 @@
package com.core.utils;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
public class Configuration {
private String coreRest;
private String xmlPath;
private String mobilityPath;
private String shellCommand;
private List<NodeTypeConfig> nodeTypeConfigs = new ArrayList<>();
}

View file

@ -39,6 +39,10 @@ public final class JsonUtils {
}
}
public static String toPrettyString(Object obj) throws JsonProcessingException {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
}
public static String toString(Object obj) throws JsonProcessingException {
return mapper.writeValueAsString(obj);
}

View file

@ -0,0 +1,16 @@
package com.core.utils;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
public class NodeTypeConfig {
private String model;
private String display;
private String icon;
private List<String> services = new ArrayList<>();
}

View file

@ -0,0 +1,6 @@
{
"coreRest": "http://127.0.0.1:5000",
"xmlPath": null,
"mobilityPath": null,
"shellCommand": "gnome-terminal --window --"
}

View file

@ -1,2 +0,0 @@
core-rest=http://127.0.0.1:5000
shell-command=gnome-terminal --window --