corefx - migrated to using a json based configuration file for more robust configuration information
This commit is contained in:
parent
eacb4d0cc0
commit
0378c66103
10 changed files with 112 additions and 68 deletions
|
@ -7,6 +7,7 @@ import com.core.graph.NetworkGraph;
|
||||||
import com.core.ui.*;
|
import com.core.ui.*;
|
||||||
import com.core.ui.dialogs.*;
|
import com.core.ui.dialogs.*;
|
||||||
import com.core.utils.ConfigUtils;
|
import com.core.utils.ConfigUtils;
|
||||||
|
import com.core.utils.Configuration;
|
||||||
import com.core.websocket.CoreWebSocket;
|
import com.core.websocket.CoreWebSocket;
|
||||||
import com.jfoenix.controls.JFXProgressBar;
|
import com.jfoenix.controls.JFXProgressBar;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
|
@ -48,7 +49,7 @@ public class Controller implements Initializable {
|
||||||
|
|
||||||
private Application application;
|
private Application application;
|
||||||
private Stage window;
|
private Stage window;
|
||||||
private Properties properties;
|
private Configuration configuration;
|
||||||
|
|
||||||
// core client utilities
|
// core client utilities
|
||||||
private ICoreClient coreClient = new CoreRestClient();
|
private ICoreClient coreClient = new CoreRestClient();
|
||||||
|
@ -299,20 +300,27 @@ public class Controller implements Initializable {
|
||||||
private void onOpenXmlAction() {
|
private void onOpenXmlAction() {
|
||||||
FileChooser fileChooser = new FileChooser();
|
FileChooser fileChooser = new FileChooser();
|
||||||
fileChooser.setTitle("Open Session");
|
fileChooser.setTitle("Open Session");
|
||||||
String xmlPath = properties.getProperty(ConfigUtils.XML_PATH);
|
fileChooser.setInitialDirectory(new File(configuration.getXmlPath()));
|
||||||
fileChooser.setInitialDirectory(new File(xmlPath));
|
|
||||||
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XML", "*.xml"));
|
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XML", "*.xml"));
|
||||||
File file = fileChooser.showOpenDialog(window);
|
try {
|
||||||
if (file != null) {
|
File file = fileChooser.showOpenDialog(window);
|
||||||
logger.info("opening session xml: {}", file.getPath());
|
if (file != null) {
|
||||||
try {
|
openXml(file);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
} 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 fileChooser = new FileChooser();
|
||||||
fileChooser.setTitle("Save Session");
|
fileChooser.setTitle("Save Session");
|
||||||
fileChooser.setInitialFileName("session.xml");
|
fileChooser.setInitialFileName("session.xml");
|
||||||
String xmlPath = properties.getProperty(ConfigUtils.XML_PATH);
|
fileChooser.setInitialDirectory(new File(configuration.getXmlPath()));
|
||||||
fileChooser.setInitialDirectory(new File(xmlPath));
|
|
||||||
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XML", "*.xml"));
|
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XML", "*.xml"));
|
||||||
File file = fileChooser.showSaveDialog(window);
|
File file = fileChooser.showSaveDialog(window);
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
|
@ -330,7 +337,7 @@ public class Controller implements Initializable {
|
||||||
try {
|
try {
|
||||||
coreClient.saveSession(file);
|
coreClient.saveSession(file);
|
||||||
} catch (IOException ex) {
|
} 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
|
@Override
|
||||||
public void initialize(URL location, ResourceBundle resources) {
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
coreWebSocket = new CoreWebSocket(this);
|
coreWebSocket = new CoreWebSocket(this);
|
||||||
properties = ConfigUtils.load();
|
configuration = ConfigUtils.load();
|
||||||
String coreUrl = properties.getProperty(ConfigUtils.REST_URL);
|
String coreUrl = configuration.getCoreRest();
|
||||||
logger.info("core rest: {}", coreUrl);
|
logger.info("core rest: {}", coreUrl);
|
||||||
connectDialog.setCoreUrl(coreUrl);
|
connectDialog.setCoreUrl(coreUrl);
|
||||||
connectToCore(coreUrl);
|
connectToCore(coreUrl);
|
||||||
|
|
|
@ -3,7 +3,6 @@ package com.core.graph;
|
||||||
import com.core.Controller;
|
import com.core.Controller;
|
||||||
import com.core.data.*;
|
import com.core.data.*;
|
||||||
import com.core.ui.Toast;
|
import com.core.ui.Toast;
|
||||||
import com.core.utils.ConfigUtils;
|
|
||||||
import com.core.utils.IconUtils;
|
import com.core.utils.IconUtils;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
|
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
|
||||||
|
@ -118,7 +117,7 @@ public class NetworkGraph {
|
||||||
|
|
||||||
if (mouseEvent.getClickCount() == 2 && controller.getCoreClient().isRunning()) {
|
if (mouseEvent.getClickCount() == 2 && controller.getCoreClient().isRunning()) {
|
||||||
try {
|
try {
|
||||||
String shellCommand = controller.getProperties().getProperty(ConfigUtils.SHELL_COMMAND);
|
String shellCommand = controller.getConfiguration().getShellCommand();
|
||||||
String terminalCommand = controller.getCoreClient().getTerminalCommand(node);
|
String terminalCommand = controller.getCoreClient().getTerminalCommand(node);
|
||||||
terminalCommand = String.format("%s %s", shellCommand, terminalCommand);
|
terminalCommand = String.format("%s %s", shellCommand, terminalCommand);
|
||||||
logger.info("launching node terminal: {}", terminalCommand);
|
logger.info("launching node terminal: {}", terminalCommand);
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.core.ui.dialogs;
|
||||||
import com.core.Controller;
|
import com.core.Controller;
|
||||||
import com.core.ui.Toast;
|
import com.core.ui.Toast;
|
||||||
import com.core.utils.ConfigUtils;
|
import com.core.utils.ConfigUtils;
|
||||||
|
import com.core.utils.Configuration;
|
||||||
import com.jfoenix.controls.JFXButton;
|
import com.jfoenix.controls.JFXButton;
|
||||||
import com.jfoenix.controls.JFXTextField;
|
import com.jfoenix.controls.JFXTextField;
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
|
@ -12,7 +13,6 @@ import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
public class GuiPreferencesDialog extends StageDialog {
|
public class GuiPreferencesDialog extends StageDialog {
|
||||||
private static final Logger logger = LogManager.getLogger();
|
private static final Logger logger = LogManager.getLogger();
|
||||||
|
@ -30,12 +30,12 @@ public class GuiPreferencesDialog extends StageDialog {
|
||||||
}
|
}
|
||||||
|
|
||||||
private EventHandler<ActionEvent> onSave = event -> {
|
private EventHandler<ActionEvent> onSave = event -> {
|
||||||
Properties properties = getController().getProperties();
|
Configuration configuration = getController().getConfiguration();
|
||||||
properties.setProperty(ConfigUtils.XML_PATH, xmlFilePathTextField.getText());
|
configuration.setXmlPath(xmlFilePathTextField.getText());
|
||||||
properties.setProperty(ConfigUtils.MOBILITY_PATH, mobilityFilePathTextField.getText());
|
configuration.setMobilityPath(mobilityFilePathTextField.getText());
|
||||||
properties.setProperty(ConfigUtils.SHELL_COMMAND, shellCommandTextField.getText());
|
configuration.setShellCommand(shellCommandTextField.getText());
|
||||||
try {
|
try {
|
||||||
ConfigUtils.save(properties);
|
ConfigUtils.save(configuration);
|
||||||
Toast.success("Updated preferences");
|
Toast.success("Updated preferences");
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Toast.error("Failure to update preferences", ex);
|
Toast.error("Failure to update preferences", ex);
|
||||||
|
@ -44,10 +44,10 @@ public class GuiPreferencesDialog extends StageDialog {
|
||||||
};
|
};
|
||||||
|
|
||||||
public void showDialog() {
|
public void showDialog() {
|
||||||
Properties properties = getController().getProperties();
|
Configuration configuration = getController().getConfiguration();
|
||||||
xmlFilePathTextField.setText(properties.getProperty(ConfigUtils.XML_PATH));
|
xmlFilePathTextField.setText(configuration.getXmlPath());
|
||||||
mobilityFilePathTextField.setText(properties.getProperty(ConfigUtils.MOBILITY_PATH));
|
mobilityFilePathTextField.setText(configuration.getMobilityPath());
|
||||||
shellCommandTextField.setText(properties.getProperty(ConfigUtils.SHELL_COMMAND));
|
shellCommandTextField.setText(configuration.getShellCommand());
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import com.core.Controller;
|
||||||
import com.core.data.CoreNode;
|
import com.core.data.CoreNode;
|
||||||
import com.core.data.MobilityConfig;
|
import com.core.data.MobilityConfig;
|
||||||
import com.core.ui.Toast;
|
import com.core.ui.Toast;
|
||||||
import com.core.utils.ConfigUtils;
|
|
||||||
import com.jfoenix.controls.JFXButton;
|
import com.jfoenix.controls.JFXButton;
|
||||||
import com.jfoenix.controls.JFXTextField;
|
import com.jfoenix.controls.JFXTextField;
|
||||||
import com.jfoenix.controls.JFXToggleButton;
|
import com.jfoenix.controls.JFXToggleButton;
|
||||||
|
@ -73,14 +72,19 @@ public class MobilityDialog extends StageDialog {
|
||||||
|
|
||||||
FileChooser fileChooser = new FileChooser();
|
FileChooser fileChooser = new FileChooser();
|
||||||
fileChooser.setTitle("Select File");
|
fileChooser.setTitle("Select File");
|
||||||
String mobilityPath = getController().getProperties().getProperty(ConfigUtils.MOBILITY_PATH);
|
String mobilityPath = getController().getConfiguration().getMobilityPath();
|
||||||
fileChooser.setInitialDirectory(new File(mobilityPath));
|
fileChooser.setInitialDirectory(new File(mobilityPath));
|
||||||
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Mobility",
|
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Mobility",
|
||||||
"*.mobility"));
|
"*.mobility"));
|
||||||
File file = fileChooser.showOpenDialog(getController().getWindow());
|
try {
|
||||||
if (file != null) {
|
File file = fileChooser.showOpenDialog(getController().getWindow());
|
||||||
logger.info("opening session xml: {}", file.getPath());
|
if (file != null) {
|
||||||
textField.setText(file.getPath());
|
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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,36 +4,38 @@ import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
public final class ConfigUtils {
|
public final class ConfigUtils {
|
||||||
private static final Logger logger = LogManager.getLogger();
|
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 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 XML_DIR = Paths.get(HOME.toString(), "xml");
|
||||||
private static final Path MOBILITY_DIR = Paths.get(HOME.toString(), "mobility");
|
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() {
|
private ConfigUtils() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void save(Properties properties) throws IOException {
|
public static void save(Configuration configuration) throws IOException {
|
||||||
properties.store(new FileOutputStream(PROPERTIES_FILE.toFile()), null);
|
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 {
|
try {
|
||||||
if (!HOME.toFile().exists()) {
|
if (!HOME.toFile().exists()) {
|
||||||
logger.info("creating core home directory");
|
logger.info("creating core home directory");
|
||||||
|
@ -42,28 +44,19 @@ public final class ConfigUtils {
|
||||||
Files.createDirectory(MOBILITY_DIR);
|
Files.createDirectory(MOBILITY_DIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
Properties properties = new Properties();
|
Configuration configuration;
|
||||||
if (!PROPERTIES_FILE.toFile().exists()) {
|
if (!CONFIG_FILE.toFile().exists()) {
|
||||||
logger.info("creating default configuration");
|
logger.info("creating default configuration");
|
||||||
Files.copy(ConfigUtils.class.getResourceAsStream(DEFAULT_CONFIG), PROPERTIES_FILE);
|
Files.copy(ConfigUtils.class.getResourceAsStream(DEFAULT_CONFIG), CONFIG_FILE);
|
||||||
properties.load(new FileInputStream(PROPERTIES_FILE.toFile()));
|
configuration = readConfig();
|
||||||
properties.setProperty(XML_PATH, XML_DIR.toString());
|
configuration.setXmlPath(XML_DIR.toString());
|
||||||
properties.setProperty(MOBILITY_PATH, MOBILITY_DIR.toString());
|
configuration.setMobilityPath(MOBILITY_DIR.toString());
|
||||||
save(properties);
|
save(configuration);
|
||||||
} else {
|
} else {
|
||||||
properties.load(new FileInputStream(PROPERTIES_FILE.toFile()));
|
configuration = readConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
// override values if provided
|
return configuration;
|
||||||
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) {
|
} catch (IOException ex) {
|
||||||
logger.error("error reading config file");
|
logger.error("error reading config file");
|
||||||
throw new RuntimeException("configuration file did not exist");
|
throw new RuntimeException("configuration file did not exist");
|
||||||
|
|
17
corefx/src/main/java/com/core/utils/Configuration.java
Normal file
17
corefx/src/main/java/com/core/utils/Configuration.java
Normal 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<>();
|
||||||
|
}
|
|
@ -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 {
|
public static String toString(Object obj) throws JsonProcessingException {
|
||||||
return mapper.writeValueAsString(obj);
|
return mapper.writeValueAsString(obj);
|
||||||
}
|
}
|
||||||
|
|
16
corefx/src/main/java/com/core/utils/NodeTypeConfig.java
Normal file
16
corefx/src/main/java/com/core/utils/NodeTypeConfig.java
Normal 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<>();
|
||||||
|
}
|
6
corefx/src/main/resources/config.json
Normal file
6
corefx/src/main/resources/config.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"coreRest": "http://127.0.0.1:5000",
|
||||||
|
"xmlPath": null,
|
||||||
|
"mobilityPath": null,
|
||||||
|
"shellCommand": "gnome-terminal --window --"
|
||||||
|
}
|
|
@ -1,2 +0,0 @@
|
||||||
core-rest=http://127.0.0.1:5000
|
|
||||||
shell-command=gnome-terminal --window --
|
|
Loading…
Reference in a new issue