Implemented login through command line arguments

* Moved Config and ConfigItem to data package
* Added mandatory property to ConfigItem
* Added user and password ConfigItems to Config
This commit is contained in:
Kai S. K. Engelbart 2020-02-06 22:17:14 +01:00
parent 74715bbf82
commit effe41f458
7 changed files with 91 additions and 27 deletions

View File

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;
import envoy.client.data.Config;
import envoy.client.ui.Color;
import envoy.client.ui.Theme;
import envoy.util.SerializationUtils;

View File

@ -1,9 +1,12 @@
package envoy.client;
package envoy.client.data;
import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.function.Function;
import java.util.logging.Level;
import envoy.data.LoginCredentials;
import envoy.exception.EnvoyException;
/**
@ -26,14 +29,15 @@ public class Config {
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));
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));
}
/**
@ -95,10 +99,12 @@ public class Config {
}
/**
* @return {@code true} if server, port and localDB directory are known.
* @return {@code true} if all mandatory config items are initialized
* @since Envoy v0.1-alpha
*/
public boolean isInitialized() { return items.values().stream().map(ConfigItem::get).noneMatch(Objects::isNull); }
public boolean isInitialized() {
return items.values().stream().filter(ConfigItem::isMandatory).map(ConfigItem::get).noneMatch(Objects::isNull);
}
/**
* @return the host name of the Envoy server
@ -141,4 +147,35 @@ public class Config {
* @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;
}
}
}

View File

@ -1,4 +1,4 @@
package envoy.client;
package envoy.client.data;
import java.util.function.Function;
@ -9,49 +9,70 @@ import java.util.function.Function;
* Project: <strong>envoy-clientChess</strong><br>
* File: <strong>ConfigItem.javaEvent.java</strong><br>
* Created: <strong>21.12.2019</strong><br>
*
*
* @author Kai S. K. Engelbart
* @param <T> the type of the config item's value
* @since Envoy v0.2-alpha
*/
public class ConfigItem<T> {
private String commandLong, commandShort;
private Function<String, T> parseFunction;
private T value;
private final String commandLong, commandShort;
private final Function<String, T> parseFunction;
private final boolean mandatory;
private T value;
/**
* Initializes a {@link ConfigItem}
*
*
* @param commandLong the long command line argument to set this value
* @param commandShort the short command line argument to set this value
* @param parseFunction the {@code Function<String, T>} that parses the value
* from a string
* @param defaultValue the optional default value to set before parsing
* @param mandatory indicated that this config item must be initialized with
* a non-null value
* @since Envoy v0.2-alpha
*/
public ConfigItem(String commandLong, String commandShort, Function<String, T> parseFunction, T defaultValue) {
public ConfigItem(String commandLong, String commandShort, Function<String, T> parseFunction, T defaultValue, boolean mandatory) {
this.commandLong = commandLong;
this.commandShort = commandShort;
this.parseFunction = parseFunction;
this.mandatory = mandatory;
value = defaultValue;
}
/**
* Initializes an optional {@link ConfigItem} without a default value.
*
* @param commandLong the long command line argument to set this value
* @param commandShort the short command line argument to set this value
* @param parseFunction the {@code Function<String, T>} that parses the value
* from a string
* @since Envoy v0.3-alpha
*/
public ConfigItem(String commandLong, String commandShort, Function<String, T> parseFunction) {
this(commandLong, commandShort, parseFunction, null, false);
}
/**
* Parses this {@ConfigItem}'s value from a string
*
* @param input the string to parse from
* @since Envoy v0.2-alpha
*/
public void parse(String input) { value = parseFunction.apply(input); }
/**
* @return The long command line argument to set the value of this {@link ConfigItem}
* @return The long command line argument to set the value of this
* {@link ConfigItem}
* @since Envoy v0.2-alpha
*/
public String getCommandLong() { return commandLong; }
/**
* @return The short command line argument to set the value of this {@link ConfigItem}
* @return The short command line argument to set the value of this
* {@link ConfigItem}
* @since Envoy v0.2-alpha
*/
public String getCommandShort() { return commandShort; }
@ -61,4 +82,11 @@ public class ConfigItem<T> {
* @since Envoy v0.2-alpha
*/
public T get() { return value; }
/**
* @return {@code true} if this {@link ConfigItem} is mandatory for successful
* application initialization
* @since Envoy v0.3-alpha
*/
public boolean isMandatory() { return mandatory; }
}

View File

@ -5,7 +5,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import envoy.client.ConfigItem;
import envoy.data.IdGenerator;
import envoy.util.SerializationUtils;

View File

@ -9,8 +9,8 @@ import java.util.logging.Logger;
import javax.naming.TimeLimitExceededException;
import envoy.client.Config;
import envoy.client.data.Cache;
import envoy.client.data.Config;
import envoy.client.data.LocalDb;
import envoy.client.util.EnvoyLog;
import envoy.data.*;

View File

@ -11,7 +11,6 @@ import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import envoy.client.Config;
import envoy.client.Settings;
import envoy.client.data.*;
import envoy.client.net.Client;
@ -63,7 +62,7 @@ public class Startup {
// Override configuration values with command line arguments
if (args.length > 0) config.load(args);
// Check if all configuration values have been initialized
// Check if all mandatory configuration values have been initialized
if (!config.isInitialized()) throw new EnvoyException("Server or port are not defined");
} catch (Exception e) {
JOptionPane
@ -76,8 +75,8 @@ public class Startup {
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
// Ask the user for their user name and password
LoginCredentials credentials = new LoginDialog().getCredentials();
// Acquire login credentials
LoginCredentials credentials = config.hasLoginCredentials() ? config.getLoginCredentials() : new LoginDialog().getCredentials();
if (credentials == null) {
logger.info("The login process has been aborted by the user. Exiting...");

View File

@ -4,7 +4,7 @@ import java.io.File;
import java.io.IOException;
import java.util.logging.*;
import envoy.client.Config;
import envoy.client.data.Config;
/**
* Project: <strong>envoy-client</strong><br>