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/data/ClientConfig.java

121 lines
3.8 KiB
Java
Raw Normal View History

package envoy.client.data;
2019-10-30 06:26:50 +01:00
import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.util.function.Function;
import java.util.logging.Level;
2019-10-30 06:26:50 +01:00
import envoy.data.Config;
import envoy.data.ConfigItem;
import envoy.data.LoginCredentials;
2019-10-30 06:26:50 +01:00
/**
* Implements a configuration specific to the Envoy Client with default values
* and convenience methods.<br>
* <br>
2019-10-30 06:26:50 +01:00
* Project: <strong>envoy-client</strong><br>
* File: <strong>ClientConfig.java</strong><br>
* Created: <strong>01.03.2020</strong><br>
*
2019-10-30 06:26:50 +01:00
* @author Kai S. K. Engelbart
* @since Envoy v0.1-beta
2019-10-30 06:26:50 +01:00
*/
public class ClientConfig extends Config {
2019-10-30 06:26:50 +01:00
private static ClientConfig config;
/**
* @return the singleton instance of the client config
* @since Envoy v0.1-beta
*/
public static ClientConfig getInstance() {
if (config == null) config = new ClientConfig();
return config;
}
private ClientConfig() {
items.put("server", new ConfigItem<>("server", "s", Function.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.CONFIG, true));
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true));
items.put("user", new ConfigItem<>("user", "u", Function.identity()));
items.put("password", new ConfigItem<>("password", "pw", String::toCharArray));
}
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 (String) items.get("server").get(); }
2019-10-30 06:26:50 +01:00
/**
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 Integer getPort() { return (Integer) items.get("port").get(); }
2019-10-30 06:26:50 +01:00
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-12-21 12:36:26 +01:00
*/
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(); }
/**
* @return the user name
* @since Envoy v0.3-alpha
*/
public String getUser() { return (String) items.get("user").get(); }
/**
* @return the password
* @since Envoy v0.3-alpha
*/
public char[] getPassword() { return (char[]) items.get("password").get(); }
/**
* @return {@code true} if user name and password are set
* @since Envoy v0.3-alpha
*/
public boolean hasLoginCredentials() { return getUser() != null && getPassword() != null; }
/**
* @return login credentials for the specified user name and password, without
* the registration option
* @since Envoy v0.3-alpha
*/
public LoginCredentials getLoginCredentials() {
try {
return new LoginCredentials(getUser(), getPassword(), false);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
}