From 0378c6610371a1a9b3828f3ee9d79f67720dc91b Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Mon, 26 Nov 2018 15:17:10 -0800 Subject: [PATCH] corefx - migrated to using a json based configuration file for more robust configuration information --- corefx/src/main/java/com/core/Controller.java | 43 ++++++++------- .../java/com/core/graph/NetworkGraph.java | 3 +- .../core/ui/dialogs/GuiPreferencesDialog.java | 20 +++---- .../com/core/ui/dialogs/MobilityDialog.java | 16 +++--- .../main/java/com/core/utils/ConfigUtils.java | 53 ++++++++----------- .../java/com/core/utils/Configuration.java | 17 ++++++ .../main/java/com/core/utils/JsonUtils.java | 4 ++ .../java/com/core/utils/NodeTypeConfig.java | 16 ++++++ corefx/src/main/resources/config.json | 6 +++ corefx/src/main/resources/config.properties | 2 - 10 files changed, 112 insertions(+), 68 deletions(-) create mode 100644 corefx/src/main/java/com/core/utils/Configuration.java create mode 100644 corefx/src/main/java/com/core/utils/NodeTypeConfig.java create mode 100644 corefx/src/main/resources/config.json delete mode 100644 corefx/src/main/resources/config.properties diff --git a/corefx/src/main/java/com/core/Controller.java b/corefx/src/main/java/com/core/Controller.java index 776939af..5f1b36c6 100644 --- a/corefx/src/main/java/com/core/Controller.java +++ b/corefx/src/main/java/com/core/Controller.java @@ -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,20 +300,27 @@ 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")); - File file = fileChooser.showOpenDialog(window); - if (file != null) { - logger.info("opening session xml: {}", file.getPath()); - try { - SessionOverview sessionOverview = coreClient.openSession(file); - Integer sessionId = sessionOverview.getId(); - joinSession(sessionId); - Toast.info(String.format("Joined Session %s", sessionId)); - } catch (IOException ex) { - logger.error("error opening session xml", ex); + 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); + Integer sessionId = sessionOverview.getId(); + joinSession(sessionId); + Toast.info(String.format("Joined Session %s", sessionId)); + } catch (IOException 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); diff --git a/corefx/src/main/java/com/core/graph/NetworkGraph.java b/corefx/src/main/java/com/core/graph/NetworkGraph.java index c2ad5b8c..418d503d 100644 --- a/corefx/src/main/java/com/core/graph/NetworkGraph.java +++ b/corefx/src/main/java/com/core/graph/NetworkGraph.java @@ -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); diff --git a/corefx/src/main/java/com/core/ui/dialogs/GuiPreferencesDialog.java b/corefx/src/main/java/com/core/ui/dialogs/GuiPreferencesDialog.java index 4c2d3480..198b71b9 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/GuiPreferencesDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/GuiPreferencesDialog.java @@ -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 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(); } } diff --git a/corefx/src/main/java/com/core/ui/dialogs/MobilityDialog.java b/corefx/src/main/java/com/core/ui/dialogs/MobilityDialog.java index d2705702..8e8c8388 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/MobilityDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/MobilityDialog.java @@ -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,14 +72,19 @@ 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")); - File file = fileChooser.showOpenDialog(getController().getWindow()); - if (file != null) { - logger.info("opening session xml: {}", file.getPath()); - textField.setText(file.getPath()); + 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())); } } diff --git a/corefx/src/main/java/com/core/utils/ConfigUtils.java b/corefx/src/main/java/com/core/utils/ConfigUtils.java index 48c9d43f..85635a3a 100644 --- a/corefx/src/main/java/com/core/utils/ConfigUtils.java +++ b/corefx/src/main/java/com/core/utils/ConfigUtils.java @@ -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"); diff --git a/corefx/src/main/java/com/core/utils/Configuration.java b/corefx/src/main/java/com/core/utils/Configuration.java new file mode 100644 index 00000000..6dd2f352 --- /dev/null +++ b/corefx/src/main/java/com/core/utils/Configuration.java @@ -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 nodeTypeConfigs = new ArrayList<>(); +} diff --git a/corefx/src/main/java/com/core/utils/JsonUtils.java b/corefx/src/main/java/com/core/utils/JsonUtils.java index f2c67804..77ba3281 100644 --- a/corefx/src/main/java/com/core/utils/JsonUtils.java +++ b/corefx/src/main/java/com/core/utils/JsonUtils.java @@ -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); } diff --git a/corefx/src/main/java/com/core/utils/NodeTypeConfig.java b/corefx/src/main/java/com/core/utils/NodeTypeConfig.java new file mode 100644 index 00000000..7735130f --- /dev/null +++ b/corefx/src/main/java/com/core/utils/NodeTypeConfig.java @@ -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 services = new ArrayList<>(); +} diff --git a/corefx/src/main/resources/config.json b/corefx/src/main/resources/config.json new file mode 100644 index 00000000..55db8ff4 --- /dev/null +++ b/corefx/src/main/resources/config.json @@ -0,0 +1,6 @@ +{ + "coreRest": "http://127.0.0.1:5000", + "xmlPath": null, + "mobilityPath": null, + "shellCommand": "gnome-terminal --window --" +} diff --git a/corefx/src/main/resources/config.properties b/corefx/src/main/resources/config.properties deleted file mode 100644 index e3ab482a..00000000 --- a/corefx/src/main/resources/config.properties +++ /dev/null @@ -1,2 +0,0 @@ -core-rest=http://127.0.0.1:5000 -shell-command=gnome-terminal --window --