This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/Config.java

148 lines
4.0 KiB
Java
Raw Normal View History

2019-10-30 06:26:50 +01:00
package envoy.client;
import java.io.File;
import java.util.Properties;
import envoy.exception.EnvoyException;
2019-10-30 06:26:50 +01:00
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Config.java</strong><br>
* Created: <strong>12 Oct 2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.1-alpha
*/
public class Config {
private String server;
private int port;
private File localDB;
private int syncTimeout;
private static Config config;
private Config() {}
public static Config getInstance() {
if (config == null) config = new Config();
return config;
}
2019-10-30 06:26:50 +01:00
/**
* 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
2019-10-30 06:26:50 +01:00
*
* @throws EnvoyException if the {@code client.properties} file could not be
* loaded
2019-10-30 06:26:50 +01:00
* @since Envoy v0.1-alpha
*/
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);
}
2019-10-30 06:26:50 +01:00
}
/**
* Sets the server, port and localDB path via command line properties --server /
* -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
2019-10-30 06:26:50 +01:00
* @since Envoy v0.1-alpha
*/
public void load(String[] args) throws EnvoyException {
2019-10-30 06:26:50 +01:00
for (int i = 0; i < args.length; i++)
switch (args[i]) {
case "--server":
case "-s":
server = args[++i];
break;
case "--port":
case "-p":
port = Integer.parseInt(args[++i]);
break;
case "--localDB":
case "-db":
localDB = new File(args[++i]);
break;
default:
throw new EnvoyException("Unknown token " + args[i] + " found");
2019-10-30 06:26:50 +01:00
}
}
2019-10-30 17:01:55 +01:00
/**
* @return {@code true} if server, port and localDB directory are known.
2019-10-30 06:26:50 +01:00
* @since Envoy v0.1-alpha
*/
public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; }
2019-10-30 06:26:50 +01:00
/**
2019-10-30 17:01:55 +01:00
* @return the host name of the Envoy server
2019-10-30 06:26:50 +01:00
* @since Envoy v0.1-alpha
*/
public String getServer() { return server; }
/**
2019-10-30 17:01:55 +01:00
* Changes the default server host name.
* Exclusively intended for development purposes.
2019-10-30 06:26:50 +01:00
*
2019-10-30 17:01:55 +01:00
* @param server the host name of the Envoy server
2019-10-30 06:26:50 +01:00
* @since Envoy v0.1-alpha
*/
public void setServer(String server) { this.server = server; }
/**
2019-10-30 17:01:55 +01:00
* @return the port at which the Envoy server is located on the host
2019-10-30 06:26:50 +01:00
* @since Envoy v0.1-alpha
*/
public int getPort() { return port; }
/**
* Changes the default port.
2019-10-30 17:01:55 +01:00
* Exclusively intended for development purposes.
2019-10-30 06:26:50 +01:00
*
2019-10-30 17:01:55 +01:00
* @param port the port where an Envoy server is located
2019-10-30 06:26:50 +01:00
* @since Envoy v0.1-alpha
*/
public void setPort(int port) { this.port = port; }
2019-10-30 07:45:33 +01:00
/**
2019-10-30 17:01:55 +01:00
* @return the local database specific to the client user
* @since Envoy v0.1-alpha
**/
2019-10-30 06:26:50 +01:00
public File getLocalDB() { return localDB; }
2019-10-30 07:45:33 +01:00
/**
2019-10-30 17:01:55 +01:00
* Changes the default local database.
* Exclusively intended for development purposes.
*
* @param localDB the file containing the local database
2019-10-30 17:01:55 +01:00
* @since Envoy v0.1-alpha
**/
2019-10-30 06:26:50 +01:00
public void setLocalDB(File localDB) { this.localDB = localDB; }
/**
* @return the current time (milliseconds) that is waited between Syncs
* @since Envoy v0.1-alpha
*/
public int getSyncTimeout() { return syncTimeout; }
/**
* @param syncTimeout sets the time (milliseconds) during which Sync waits
* @since Envoy v0.1-alpha
*/
public void setSyncTimeout(int syncTimeout) { this.syncTimeout = syncTimeout; }
2019-10-30 07:45:33 +01:00
}