Moved client.properties loading from Startup to Config

This commit is contained in:
Kai S. K. Engelbart 2019-12-13 08:50:15 +01:00
parent 117f9043ce
commit daca66d729
2 changed files with 34 additions and 29 deletions

View File

@ -3,6 +3,8 @@ package envoy.client;
import java.io.File; import java.io.File;
import java.util.Properties; import java.util.Properties;
import envoy.exception.EnvoyException;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
* File: <strong>Config.java</strong><br> * File: <strong>Config.java</strong><br>
@ -29,17 +31,26 @@ public class Config {
/** /**
* Defaults to the {@code client.properties} file for information. * Defaults to the {@code client.properties} file for information.
* This file contains information about
* the server and port, as well as the path to the local
* database and the synchronization timeout
* *
* @param properties a {@link Properties} object containing information about * @throws EnvoyException if the {@code client.properties} file could not be
* the server and port, as well as the path to the local * loaded
* database and the synchronization timeout
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void load(Properties properties) { public void load() throws EnvoyException {
if (properties.containsKey("server")) server = properties.getProperty("server"); ClassLoader loader = getClass().getClassLoader();
if (properties.containsKey("port")) port = Integer.parseInt(properties.getProperty("port")); try {
localDB = new File(properties.getProperty("localDB", ".\\localDB")); Properties properties = new Properties();
syncTimeout = Integer.parseInt(properties.getProperty("syncTimeout", "1000")); properties.load(loader.getResourceAsStream("client.properties"));
if (properties.containsKey("server")) server = properties.getProperty("server");
if (properties.containsKey("port")) port = Integer.parseInt(properties.getProperty("port"));
localDB = new File(properties.getProperty("localDB", ".\\localDB"));
syncTimeout = Integer.parseInt(properties.getProperty("syncTimeout", "1000"));
} catch (Exception e) {
throw new EnvoyException("Failed to load client.properties", e);
}
} }
/** /**
@ -47,9 +58,10 @@ public class Config {
* -s, --port / -p and --localDB / -db. * -s, --port / -p and --localDB / -db.
* *
* @param args the command line arguments to parse * @param args the command line arguments to parse
* @throws EnvoyException if the command line arguments contain an unknown token
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void load(String[] args) { public void load(String[] args) throws EnvoyException {
for (int i = 0; i < args.length; i++) for (int i = 0; i < args.length; i++)
switch (args[i]) { switch (args[i]) {
case "--server": case "--server":
@ -64,6 +76,8 @@ public class Config {
case "-db": case "-db":
localDB = new File(args[++i]); localDB = new File(args[++i]);
break; break;
default:
throw new EnvoyException("Unknown token " + args[i] + " found");
} }
} }
@ -71,9 +85,7 @@ public class Config {
* @return {@code true} if server, port and localDB directory are known. * @return {@code true} if server, port and localDB directory are known.
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public boolean isInitialized() { public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; }
return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null;
}
/** /**
* @return the host name of the Envoy server * @return the host name of the Envoy server
@ -114,7 +126,7 @@ public class Config {
/** /**
* Changes the default local database. * Changes the default local database.
* Exclusively intended for development purposes. * Exclusively intended for development purposes.
* *
* @param localDB the file containing the local database * @param localDB the file containing the local database
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
**/ **/

View File

@ -1,8 +1,6 @@
package envoy.client.ui; package envoy.client.ui;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -35,24 +33,19 @@ public class Startup {
Config config = Config.getInstance(); Config config = Config.getInstance();
// Load the configuration from client.properties first
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try { try {
Properties configProperties = new Properties(); // Load the configuration from client.properties first
configProperties.load(loader.getResourceAsStream("client.properties")); config.load();
config.load(configProperties);
} catch (IOException e) {
e.printStackTrace();
}
// Override configuration values with command line arguments // Override configuration values with command line arguments
if (args.length > 0) config.load(args); if (args.length > 0) config.load(args);
// Check if all configuration values have been initialized // Check if all configuration values have been initialized
if (!config.isInitialized()) { if (!config.isInitialized()) throw new EnvoyException("Server or port are not defined");
logger.severe("Server or port are not defined. Exiting..."); } catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, "Error loading configuration values: \n" + e.toString(), "Configuration error", JOptionPane.ERROR_MESSAGE);
System.exit(1); System.exit(1);
e.printStackTrace();
} }
// Ask the user for their user name // Ask the user for their user name