144 lines
5.1 KiB
Java
144 lines
5.1 KiB
Java
package envoy.client;
|
|
|
|
import java.io.File;
|
|
import java.util.*;
|
|
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("ignoreLocalDB", new ConfigItem<>("ignoreLocalDB", "nodb", (input) -> Boolean.parseBoolean(input), false));
|
|
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().map(ConfigItem::get).noneMatch(Objects::isNull); }
|
|
|
|
/**
|
|
* @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 {@code true} if the local database is to be ignored
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public Boolean isIgnoreLocalDB() { return (Boolean) items.get("ignoreLocalDB").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(); }
|
|
} |