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

139 lines
4.8 KiB
Java

package envoy.client;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import envoy.exception.EnvoyException;
/**
* Manages all application settings that are set during application startup by
* either loading them from the {@link Properties} file
* {@code client.properties} or parsing them from the command line arguments of
* the application.<br>
* <br>
* 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 Map<String, ConfigItem<?>> items = new HashMap<>();
private static Config config;
private Config() {
items.put("server", new ConfigItem<>("server", "s", (input) -> input, null));
items.put("port", new ConfigItem<>("port", "p", (input) -> Integer.parseInt(input), null));
items.put("localDB", new ConfigItem<>("localDB", "db", (input) -> new File(input), new File("localDB")));
items.put("homeDirectory",
new ConfigItem<>("homeDirectory", "h", (input) -> new File(input), new File(System.getProperty("user.home"), ".envoy")));
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", (input) -> Level.parse(input), Level.CONFIG));
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", (input) -> Level.parse(input), Level.FINEST));
}
/**
* @return the singleton instance of the {@link Config}
* @since Envoy v0.1-alpha
*/
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"));
items.forEach((name, item) -> { if (properties.containsKey(name)) item.parse(properties.getProperty(name)); });
} 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++)
for (ConfigItem<?> item : items.values())
if (args[i].startsWith("--")) {
if (args[i].length() == 2) throw new EnvoyException("Malformed command line argument at position " + i);
final String commandLong = args[i].substring(2);
if (item.getCommandLong().equals(commandLong)) {
item.parse(args[++i]);
break;
}
} else if (args[i].startsWith("-")) {
if (args[i].length() == 1) throw new EnvoyException("Malformed command line argument at position " + i);
final String commandShort = args[i].substring(1);
if (item.getCommandShort().equals(commandShort)) {
item.parse(args[++i]);
break;
}
} else throw new EnvoyException("Malformed command line argument at position " + i);
}
/**
* @return {@code true} if server, port and localDB directory are known.
* @since Envoy v0.1-alpha
*/
public boolean isInitialized() { return items.values().stream().noneMatch(item -> item.get() == null); }
/**
* @return the host name of the Envoy server
* @since Envoy v0.1-alpha
*/
public String getServer() { return (String) items.get("server").get(); }
/**
* @return the port at which the Envoy server is located on the host
* @since Envoy v0.1-alpha
*/
public Integer getPort() { return (Integer) items.get("port").get(); }
/**
* @return the local database specific to the client user
* @since Envoy v0.1-alpha
*/
public File getLocalDB() { return (File) items.get("localDB").get(); }
/**
* @return the directory in which all local files are saves
* @since Envoy v0.2-alpha
*/
public File getHomeDirectory() { return (File) items.get("homeDirectory").get(); }
/**
* @return the minimal {@link Level} to log inside the log file
* @since Envoy v0.2-alpha
*/
public Level getFileLevelBarrier() { return (Level) items.get("fileLevelBarrier").get(); }
/**
* @return the minimal {@link Level} to log inside the console
* @since Envoy v0.2-alpha
*/
public Level getConsoleLevelBarrier() { return (Level) items.get("consoleLevelBarrier").get(); }
}