diff --git a/pom.xml b/pom.xml index e670832..ec25577 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - v0.2-alpha + develop-SNAPSHOT diff --git a/src/main/java/envoy/client/data/Cache.java b/src/main/java/envoy/client/data/Cache.java index a727d01..2622322 100644 --- a/src/main/java/envoy/client/data/Cache.java +++ b/src/main/java/envoy/client/data/Cache.java @@ -6,7 +6,7 @@ import java.util.Queue; import java.util.function.Consumer; import java.util.logging.Logger; -import envoy.client.util.EnvoyLog; +import envoy.util.EnvoyLog; /** * Stores elements in a queue to process them later.
diff --git a/src/main/java/envoy/client/data/Config.java b/src/main/java/envoy/client/data/ClientConfig.java similarity index 53% rename from src/main/java/envoy/client/data/Config.java rename to src/main/java/envoy/client/data/ClientConfig.java index 1f351ea..349fa54 100644 --- a/src/main/java/envoy/client/data/Config.java +++ b/src/main/java/envoy/client/data/ClientConfig.java @@ -2,33 +2,38 @@ 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.Config; +import envoy.data.ConfigItem; import envoy.data.LoginCredentials; -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.
+ * Implements a configuration specific to the Envoy Client with default values + * and convenience methods.
*
* Project: envoy-client
- * File: Config.java
- * Created: 12 Oct 2019
+ * File: ClientConfig.java
+ * Created: 01.03.2020
* * @author Kai S. K. Engelbart - * @since Envoy v0.1-alpha + * @since Envoy v0.1-beta */ -public class Config { +public class ClientConfig extends Config { - private Map> items = new HashMap<>(); + private static ClientConfig config; - private static Config 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 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)); @@ -40,72 +45,6 @@ public class Config { items.put("password", new ConfigItem<>("password", "pw", String::toCharArray)); } - /** - * @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 all mandatory config items are initialized - * @since Envoy v0.1-alpha - */ - public boolean isInitialized() { - return items.values().stream().filter(ConfigItem::isMandatory).map(ConfigItem::get).noneMatch(Objects::isNull); - } - /** * @return the host name of the Envoy server * @since Envoy v0.1-alpha @@ -178,4 +117,4 @@ public class Config { return null; } } -} \ No newline at end of file +} diff --git a/src/main/java/envoy/client/data/ConfigItem.java b/src/main/java/envoy/client/data/ConfigItem.java deleted file mode 100644 index 6a987cb..0000000 --- a/src/main/java/envoy/client/data/ConfigItem.java +++ /dev/null @@ -1,92 +0,0 @@ -package envoy.client.data; - -import java.util.function.Function; - -/** - * Contains a single {@link Config} value as well as the corresponding command - * line arguments and its default value.
- *
- * 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 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, 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} - * @since Envoy v0.2-alpha - */ - public String getCommandLong() { return commandLong; } - - /** - * @return The short command line argument to set the value of this - * {@link ConfigItem} - * @since Envoy v0.2-alpha - */ - public String getCommandShort() { return commandShort; } - - /** - * @return the value of this {@link 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 0a04729..f59b0f2 100644 --- a/src/main/java/envoy/client/data/PersistentLocalDb.java +++ b/src/main/java/envoy/client/data/PersistentLocalDb.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import envoy.data.ConfigItem; import envoy.data.IdGenerator; import envoy.util.SerializationUtils; diff --git a/src/main/java/envoy/client/data/Settings.java b/src/main/java/envoy/client/data/Settings.java index 8cf0283..c44e4f4 100644 --- a/src/main/java/envoy/client/data/Settings.java +++ b/src/main/java/envoy/client/data/Settings.java @@ -33,12 +33,12 @@ public class Settings { /** * Settings are stored in this file. */ - private static final File settingsFile = new File(Config.getInstance().getHomeDirectory(), "settings.ser"); + private static final File settingsFile = new File(ClientConfig.getInstance().getHomeDirectory(), "settings.ser"); /** * User-defined themes are stored inside this file. */ - private static final File themeFile = new File(Config.getInstance().getHomeDirectory(), "themes.ser"); + private static final File themeFile = new File(ClientConfig.getInstance().getHomeDirectory(), "themes.ser"); /** * Singleton instance of this class. diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 7a7f960..42bfe0b 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -10,13 +10,13 @@ import java.util.logging.Logger; import javax.naming.TimeLimitExceededException; import envoy.client.data.Cache; -import envoy.client.data.Config; +import envoy.client.data.ClientConfig; import envoy.client.data.LocalDb; import envoy.client.event.SendEvent; -import envoy.client.util.EnvoyLog; import envoy.data.*; import envoy.event.*; import envoy.event.ContactOperationEvent.Operation; +import envoy.util.EnvoyLog; import envoy.util.SerializationUtils; /** @@ -45,8 +45,8 @@ public class Client implements Closeable { private volatile boolean rejected; // Configuration and logging - private static final Config config = Config.getInstance(); - private static final Logger logger = EnvoyLog.getLogger(Client.class); + private static final ClientConfig config = ClientConfig.getInstance(); + private static final Logger logger = EnvoyLog.getLogger(Client.class); /** * Enters the online mode by acquiring a user ID from the server. As a diff --git a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java index 8197d53..834bcc8 100644 --- a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java +++ b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java @@ -3,10 +3,10 @@ package envoy.client.net; import java.util.function.Consumer; import java.util.logging.Logger; -import envoy.client.util.EnvoyLog; import envoy.data.Message.MessageStatus; import envoy.event.EventBus; import envoy.event.MessageStatusChangeEvent; +import envoy.util.EnvoyLog; /** * Project: envoy-client
diff --git a/src/main/java/envoy/client/net/ReceivedMessageProcessor.java b/src/main/java/envoy/client/net/ReceivedMessageProcessor.java index 5196402..95f19c5 100644 --- a/src/main/java/envoy/client/net/ReceivedMessageProcessor.java +++ b/src/main/java/envoy/client/net/ReceivedMessageProcessor.java @@ -4,10 +4,10 @@ import java.util.function.Consumer; import java.util.logging.Logger; import envoy.client.event.MessageCreationEvent; -import envoy.client.util.EnvoyLog; import envoy.data.Message; import envoy.data.Message.MessageStatus; import envoy.event.EventBus; +import envoy.util.EnvoyLog; /** * Project: envoy-client
diff --git a/src/main/java/envoy/client/net/Receiver.java b/src/main/java/envoy/client/net/Receiver.java index 4a4fda8..3b53b48 100644 --- a/src/main/java/envoy/client/net/Receiver.java +++ b/src/main/java/envoy/client/net/Receiver.java @@ -10,7 +10,7 @@ import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; -import envoy.client.util.EnvoyLog; +import envoy.util.EnvoyLog; import envoy.util.SerializationUtils; /** diff --git a/src/main/java/envoy/client/net/WriteProxy.java b/src/main/java/envoy/client/net/WriteProxy.java index 272da8b..7d25bf1 100644 --- a/src/main/java/envoy/client/net/WriteProxy.java +++ b/src/main/java/envoy/client/net/WriteProxy.java @@ -5,9 +5,9 @@ import java.util.logging.Level; import java.util.logging.Logger; import envoy.client.data.LocalDb; -import envoy.client.util.EnvoyLog; import envoy.data.Message; import envoy.event.MessageStatusChangeEvent; +import envoy.util.EnvoyLog; /** * Implements methods to send {@link Message}s and diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 22ab504..813cd71 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -22,12 +22,12 @@ import envoy.client.net.WriteProxy; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListModel; import envoy.client.ui.settings.SettingsScreen; -import envoy.client.util.EnvoyLog; import envoy.data.Message; import envoy.data.Message.MessageStatus; import envoy.data.MessageBuilder; import envoy.data.User; import envoy.event.*; +import envoy.util.EnvoyLog; /** * Project: envoy-client
diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index 7b93bdf..0c507eb 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -14,13 +14,13 @@ import javax.swing.border.EmptyBorder; import envoy.client.data.*; import envoy.client.event.HandshakeSuccessfulEvent; import envoy.client.net.Client; -import envoy.client.util.EnvoyLog; import envoy.data.LoginCredentials; import envoy.data.Message; import envoy.data.User; import envoy.event.EventBus; import envoy.event.HandshakeRejectionEvent; import envoy.exception.EnvoyException; +import envoy.util.EnvoyLog; /** * Project: envoy-client
@@ -59,9 +59,9 @@ public class LoginDialog extends JDialog { private final LocalDb localDb; private final Cache receivedMessageCache; - private static final Config config = Config.getInstance(); - private static final Logger logger = EnvoyLog.getLogger(LoginDialog.class); - private static final long serialVersionUID = 352021600833907468L; + private static final ClientConfig config = ClientConfig.getInstance(); + private static final Logger logger = EnvoyLog.getLogger(LoginDialog.class); + private static final long serialVersionUID = 352021600833907468L; /** * Displays a dialog enabling the user to enter their user name and password. diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 62d3f69..81b393f 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -4,6 +4,7 @@ import java.awt.EventQueue; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -14,9 +15,10 @@ import javax.swing.SwingUtilities; import envoy.client.data.*; import envoy.client.net.Client; import envoy.client.net.WriteProxy; -import envoy.client.util.EnvoyLog; +import envoy.data.Config; import envoy.data.Message; import envoy.exception.EnvoyException; +import envoy.util.EnvoyLog; /** * Starts the Envoy client and prompts the user to enter their name.
@@ -48,13 +50,15 @@ public class Startup { * @since Envoy v0.1-alpha */ public static void main(String[] args) { - Config config = Config.getInstance(); + ClientConfig config = ClientConfig.getInstance(); SwingUtilities.invokeLater(() -> chatWindow = new ChatWindow()); try { // Load the configuration from client.properties first - config.load(); + Properties properties = new Properties(); + properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties")); + config.load(properties); // Override configuration values with command line arguments if (args.length > 0) config.load(args); @@ -62,13 +66,13 @@ public class Startup { // Check if all mandatory configuration values have been initialized if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized"); } catch (Exception e) { - JOptionPane - .showMessageDialog(null, "Error loading configuration values:\n" + e.toString(), "Configuration error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "Error loading configuration values:\n" + e, "Configuration error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); System.exit(1); } // Setup logger for the envoy package + EnvoyLog.initialize(config); EnvoyLog.attach("envoy"); EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier()); EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier()); @@ -85,8 +89,7 @@ public class Startup { localDb = new PersistentLocalDb(new File(config.getHomeDirectory(), config.getLocalDB().getPath())); } catch (IOException e3) { logger.log(Level.SEVERE, "Could not initialize local database", e3); - JOptionPane - .showMessageDialog(null, "Could not initialize local database!\n" + e3, "Local database error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "Could not initialize local database!\n" + e3, "Local database error", JOptionPane.ERROR_MESSAGE); System.exit(1); return; } diff --git a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java index f4537b3..13df564 100644 --- a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java +++ b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java @@ -13,7 +13,7 @@ import javax.swing.JTextPane; import envoy.client.data.Settings; import envoy.client.data.SettingsItem; import envoy.client.ui.Theme; -import envoy.client.util.EnvoyLog; +import envoy.util.EnvoyLog; /** * Displays GUI components that allow general settings regarding the client.
diff --git a/src/main/java/envoy/client/ui/settings/SettingsScreen.java b/src/main/java/envoy/client/ui/settings/SettingsScreen.java index a84085b..c04da96 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/settings/SettingsScreen.java @@ -13,8 +13,8 @@ import envoy.client.data.Settings; import envoy.client.event.ThemeChangeEvent; import envoy.client.ui.PrimaryButton; import envoy.client.ui.Theme; -import envoy.client.util.EnvoyLog; import envoy.event.EventBus; +import envoy.util.EnvoyLog; /** * This class provides the GUI to change the user specific settings.
diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index 09a05e2..fff118f 100644 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -13,8 +13,8 @@ import envoy.client.data.Settings; import envoy.client.event.ThemeChangeEvent; import envoy.client.ui.Color; import envoy.client.ui.Theme; -import envoy.client.util.EnvoyLog; import envoy.event.EventBus; +import envoy.util.EnvoyLog; /** * Displays GUI components that allow changing the current {@Theme} and creating diff --git a/src/main/java/envoy/client/util/EnvoyLog.java b/src/main/java/envoy/client/util/EnvoyLog.java deleted file mode 100644 index cf2818d..0000000 --- a/src/main/java/envoy/client/util/EnvoyLog.java +++ /dev/null @@ -1,107 +0,0 @@ -package envoy.client.util; - -import java.io.File; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.logging.*; - -import envoy.client.data.Config; - -/** - * Configures the {@link java.util.logging} API to output the log into the - * console and a log file.
- *
- * Call the {@link EnvoyLog#attach(String)} method to configure a part of the - * logger hierarchy.
- *
- * Project: envoy-client
- * File: EnvoyLogger.java
- * Created: 14 Dec 2019
- * - * @author Leon Hofmeister - * @author Kai S. K. Engelbart - * @since Envoy v0.2-alpha - */ -public class EnvoyLog { - - private static FileHandler fileHandler; - private static StreamHandler consoleHandler; - - static { - - // Remove default console handler - LogManager.getLogManager().reset(); - - // Configure log file - File logFile = new File(Config.getInstance().getHomeDirectory(), - "log/envoy_user_" + new SimpleDateFormat("yyyy-MM-dd--hh-mm-mm").format(new Date()) + ".log"); - logFile.getParentFile().mkdirs(); - - // Configure formatting - System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] [%3$s] %5$s %n"); - SimpleFormatter formatter = new SimpleFormatter(); - - try { - fileHandler = new FileHandler(logFile.getAbsolutePath()); - fileHandler.setLevel(Config.getInstance().getFileLevelBarrier()); - fileHandler.setFormatter(formatter); - } catch (SecurityException | IOException e) { - e.printStackTrace(); - } - consoleHandler = new StreamHandler(System.out, formatter); - consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier()); - consoleHandler.setFormatter(formatter); - } - - private EnvoyLog() {} - - /** - * Configures all loggers that are contained within the hierarchy of a specific - * path to use the console and file handlers. - * - * @param path the path to the loggers to configure - * @since Envoy Client v0.1-beta - */ - public static void attach(String path) { - // Get root logger - Logger logger = Logger.getLogger(path); - - // Add handlers - if (fileHandler != null) logger.addHandler(fileHandler); - logger.addHandler(consoleHandler); - - // Delegate logger level filtering to the handlers - logger.setLevel(Level.ALL); - } - - /** - * Creates a logger for a specified class, which output will be displayed inside - * the console and written to the log file. - * - * @param logClass the class in which the logger is used - * @return the created logger - * @since Envoy v0.2-alpha - */ - public static Logger getLogger(Class logClass) { - return Logger.getLogger(logClass.getCanonicalName()); - } - - /** - * Defines the logger level required for a record to be written to the log file. - * - * @param fileLevelBarrier the log file level - * @since Envoy v0.2-alpha - */ - public static void setFileLevelBarrier(Level fileLevelBarrier) { if (fileHandler != null) fileHandler.setLevel(fileLevelBarrier); } - - /** - * Defines the logger level required for a record to be written to the console. - * - * @param consoleLevelBarrier the console logger level - * @since Envoy v0.2-alpha - */ - public static void setConsoleLevelBarrier(Level consoleLevelBarrier) { - if (consoleHandler != null) consoleHandler.setLevel(consoleLevelBarrier); - } -}