diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 02b6c88..efb03d6 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -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; diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/data/Config.java similarity index 69% rename from src/main/java/envoy/client/Config.java rename to src/main/java/envoy/client/data/Config.java index 24e7780..1f351ea 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/data/Config.java @@ -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; + } + } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ConfigItem.java b/src/main/java/envoy/client/data/ConfigItem.java similarity index 56% rename from src/main/java/envoy/client/ConfigItem.java rename to src/main/java/envoy/client/data/ConfigItem.java index 87e927b..440a81c 100644 --- a/src/main/java/envoy/client/ConfigItem.java +++ b/src/main/java/envoy/client/data/ConfigItem.java @@ -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: envoy-clientChess
* File: ConfigItem.javaEvent.java
* Created: 21.12.2019
- * + * * @author Kai S. K. Engelbart * @param the type of the config item's value * @since Envoy v0.2-alpha */ public class ConfigItem { - private String commandLong, commandShort; - private Function parseFunction; - private T value; + private final String commandLong, commandShort; + private final Function 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} 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 parseFunction, T defaultValue) { + public ConfigItem(String commandLong, String commandShort, Function 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} that parses the value + * from a string + * @since Envoy v0.3-alpha + */ + public ConfigItem(String commandLong, String commandShort, Function 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 { * @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; } } diff --git a/src/main/java/envoy/client/data/PersistentLocalDb.java b/src/main/java/envoy/client/data/PersistentLocalDb.java index d6e594a..0a04729 100644 --- a/src/main/java/envoy/client/data/PersistentLocalDb.java +++ b/src/main/java/envoy/client/data/PersistentLocalDb.java @@ -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; diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 29b2f03..d742f1b 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -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.*; diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 46f7951..13f8676 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -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..."); diff --git a/src/main/java/envoy/client/util/EnvoyLog.java b/src/main/java/envoy/client/util/EnvoyLog.java index b59b714..ae6c6e5 100644 --- a/src/main/java/envoy/client/util/EnvoyLog.java +++ b/src/main/java/envoy/client/util/EnvoyLog.java @@ -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: envoy-client