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.util.Properties;
import envoy.exception.EnvoyException;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Config.java</strong><br>
@ -29,17 +31,26 @@ public class Config {
/**
* 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
* the server and port, as well as the path to the local
* database and the synchronization timeout
* @throws EnvoyException if the {@code client.properties} file could not be
* loaded
* @since Envoy v0.1-alpha
*/
public void load(Properties 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"));
public void load() throws EnvoyException {
ClassLoader loader = getClass().getClassLoader();
try {
Properties properties = new Properties();
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.
*
* @param args the command line arguments to parse
* @throws EnvoyException if the command line arguments contain an unknown token
* @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++)
switch (args[i]) {
case "--server":
@ -64,6 +76,8 @@ public class Config {
case "-db":
localDB = new File(args[++i]);
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.
* @since Envoy v0.1-alpha
*/
public boolean isInitialized() {
return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null;
}
public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; }
/**
* @return the host name of the Envoy server
@ -114,7 +126,7 @@ public class Config {
/**
* Changes the default local database.
* Exclusively intended for development purposes.
*
*
* @param localDB the file containing the local database
* @since Envoy v0.1-alpha
**/

View File

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