Moved client.properties loading from Startup to Config
This commit is contained in:
parent
0f64ce0a01
commit
e69deb9bd6
@ -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.
|
||||
*
|
||||
* @param properties a {@link Properties} object containing information about
|
||||
* This file contains 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) {
|
||||
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
|
||||
|
@ -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);
|
||||
|
||||
// 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);
|
||||
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
|
||||
|
Reference in New Issue
Block a user