package envoy.client; import java.io.File; import java.util.Properties; import envoy.exception.EnvoyException; /** * Project: envoy-client
* File: Config.java
* Created: 12 Oct 2019
* * @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; } /** * 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 * * @throws EnvoyException if the {@code client.properties} file could not be * loaded * @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); } } /** * 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 * @since Envoy v0.1-alpha */ public void load(String[] args) throws EnvoyException { 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"); } } /** * @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; } /** * @return the host name of the Envoy server * @since Envoy v0.1-alpha */ public String getServer() { return server; } /** * Changes the default server host name. * Exclusively intended for development purposes. * * @param server the host name of the Envoy server * @since Envoy v0.1-alpha */ public void setServer(String server) { this.server = server; } /** * @return the port at which the Envoy server is located on the host * @since Envoy v0.1-alpha */ public int getPort() { return port; } /** * Changes the default port. * Exclusively intended for development purposes. * * @param port the port where an Envoy server is located * @since Envoy v0.1-alpha */ public void setPort(int port) { this.port = port; } /** * @return the local database specific to the client user * @since Envoy v0.1-alpha **/ public File getLocalDB() { return localDB; } /** * Changes the default local database. * Exclusively intended for development purposes. * * @param localDB the file containing the local database * @since Envoy v0.1-alpha **/ 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; } }