package envoy.client.data; import static java.util.function.Function.identity; import java.io.File; import java.util.logging.Level; import envoy.data.Config; import envoy.data.ConfigItem; /** * Implements a configuration specific to the Envoy Client with default values * and convenience methods. *

* Project: envoy-client
* File: ClientConfig.java
* Created: 01.03.2020
* * @author Kai S. K. Engelbart * @since Envoy Client v0.1-beta */ public class ClientConfig extends Config { private static ClientConfig config; /** * @return the singleton instance of the client config * @since Envoy Client v0.1-beta */ public static ClientConfig getInstance() { if (config == null) config = new ClientConfig(); return config; } private ClientConfig() { items.put("server", new ConfigItem<>("server", "s", identity(), null, true)); items.put("port", new ConfigItem<>("port", "p", Integer::parseInt, null, true)); items.put("localDB", new ConfigItem<>("localDB", "db", File::new, new File("localDB"), true)); items.put("ignoreLocalDB", new ConfigItem<>("ignoreLocalDB", "nodb", Boolean::parseBoolean, false, false)); items.put("homeDirectory", new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy"), true)); items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.OFF, true)); items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.OFF, true)); items.put("user", new ConfigItem<>("user", "u", identity())); items.put("password", new ConfigItem<>("password", "pw", identity())); } /** * @return the host name of the Envoy server * @since Envoy Client 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 Client v0.1-alpha */ public Integer getPort() { return (Integer) items.get("port").get(); } /** * @return the local database specific to the client user * @since Envoy Client 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 Client v0.3-alpha */ public Boolean isIgnoreLocalDB() { return (Boolean) items.get("ignoreLocalDB").get(); } /** * @return the directory in which all local files are saves * @since Envoy Client 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 Client v0.2-alpha */ public Level getFileLevelBarrier() { return (Level) items.get("fileLevelBarrier").get(); } /** * @return the minimal {@link Level} to log inside the console * @since Envoy Client v0.2-alpha */ public Level getConsoleLevelBarrier() { return (Level) items.get("consoleLevelBarrier").get(); } /** * @return the user name * @since Envoy Client v0.3-alpha */ public String getUser() { return (String) items.get("user").get(); } /** * @return the password * @since Envoy Client v0.3-alpha */ public String getPassword() { return (String) items.get("password").get(); } /** * @return {@code true} if user name and password are set * @since Envoy Client v0.3-alpha */ public boolean hasLoginCredentials() { return getUser() != null && getPassword() != null; } }