From 763830c72746eeef60f3c7e8d5a7bb4f313f5210 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 16 Sep 2020 15:41:00 +0200 Subject: [PATCH] Remove TransientLocalDB and no-db config value --- .../java/envoy/client/data/ClientConfig.java | 7 -- .../main/java/envoy/client/data/LocalDB.java | 91 ++++++++++++++----- .../envoy/client/data/PersistentLocalDB.java | 90 ------------------ .../envoy/client/data/TransientLocalDB.java | 15 --- .../main/java/envoy/client/ui/Startup.java | 17 +--- 5 files changed, 74 insertions(+), 146 deletions(-) delete mode 100644 client/src/main/java/envoy/client/data/PersistentLocalDB.java delete mode 100644 client/src/main/java/envoy/client/data/TransientLocalDB.java diff --git a/client/src/main/java/envoy/client/data/ClientConfig.java b/client/src/main/java/envoy/client/data/ClientConfig.java index c6d4158..a7763e0 100644 --- a/client/src/main/java/envoy/client/data/ClientConfig.java +++ b/client/src/main/java/envoy/client/data/ClientConfig.java @@ -35,7 +35,6 @@ public final class ClientConfig extends Config { put("server", "s", identity()); put("port", "p", Integer::parseInt); put("localDB", "db", File::new); - put("ignoreLocalDB", "nodb", Boolean::parseBoolean); put("user", "u", identity()); put("password", "pw", identity()); } @@ -58,12 +57,6 @@ public final class ClientConfig extends Config { */ 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 user name * @since Envoy Client v0.3-alpha diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index d344d98..d44caa0 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -1,17 +1,19 @@ package envoy.client.data; +import java.io.*; import java.time.Instant; import java.util.*; import envoy.data.*; -import envoy.event.GroupResize; -import envoy.event.MessageStatusChange; -import envoy.event.NameChange; +import envoy.event.*; +import envoy.util.SerializationUtils; /** * Stores information about the current {@link User} and their {@link Chat}s. * For message ID generation a {@link IDGenerator} is stored as well. *

+ * The managed objects are stored inside a folder in the local file system. + *

* Project: envoy-client
* File: LocalDB.java
* Created: 3 Feb 2020
@@ -19,60 +21,105 @@ import envoy.event.NameChange; * @author Kai S. K. Engelbart * @since Envoy Client v0.3-alpha */ -public abstract class LocalDB { +public final class LocalDB { - protected User user; - protected Map users = new HashMap<>(); - protected List chats = new ArrayList<>(); - protected IDGenerator idGenerator; - protected CacheMap cacheMap = new CacheMap(); - protected Instant lastSync = Instant.EPOCH; + private User user; + private Map users = new HashMap<>(); + private List chats = new ArrayList<>(); + private IDGenerator idGenerator; + private CacheMap cacheMap = new CacheMap(); + private Instant lastSync = Instant.EPOCH; + private File dbDir, userFile, idGeneratorFile, usersFile; - { + /** + * Constructs an empty local database. To serialize any user-specific data to + * the file system, call {@link LocalDB#initializeUserStorage()} first + * and then {@link LocalDB#save(boolean)}. + * + * @param dbDir the directory in which to persist data + * @throws IOException if {@code dbDir} is a file (and not a directory) + * @since Envoy Client v0.2-beta + */ + public LocalDB(File dbDir) throws IOException { + this.dbDir = dbDir; + + // Test if the database directory is actually a directory + if (dbDir.exists() && !dbDir.isDirectory()) + throw new IOException(String.format("LocalDBDir '%s' is not a directory!", dbDir.getAbsolutePath())); + + // Initialize global files + idGeneratorFile = new File(dbDir, "id_gen.db"); + usersFile = new File(dbDir, "users.db"); + + // Initialize offline caches cacheMap.put(Message.class, new Cache<>()); cacheMap.put(MessageStatusChange.class, new Cache<>()); } /** - * Initializes a storage space for a user-specific list of chats. + * Creates a database file for a user-specific list of chats. * - * @since Envoy Client v0.3-alpha + * @throws IllegalStateException if the client user is not specified + * @since Envoy Client v0.1-alpha */ - public void initializeUserStorage() {} + public void initializeUserStorage() { + if (user == null) throw new IllegalStateException("Client user is null, cannot initialize user storage"); + userFile = new File(dbDir, user.getID() + ".db"); + } /** * Stores all users. If the client user is specified, their chats will be stored * as well. The message id generator will also be saved if present. * * @param isOnline determines which {@code lastSync} time stamp is saved - * @throws Exception if the saving process failed + * @throws IOException if the saving process failed * @since Envoy Client v0.3-alpha */ - public void save(boolean isOnline) throws Exception {} + public void save(boolean isOnline) throws IOException { + // Save users + SerializationUtils.write(usersFile, users); + + // Save user data and last sync time stamp + if (user != null) SerializationUtils.write(userFile, chats, cacheMap, isOnline ? Instant.now() : lastSync); + + // Save id generator + if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator); + } /** * Loads all user data. * - * @throws Exception if the loading process failed + * @throws ClassNotFoundException if the loading process failed + * @throws IOException if the loading process failed * @since Envoy Client v0.3-alpha */ - public void loadUsers() throws Exception {} + public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); } /** * Loads all data of the client user. * - * @throws Exception if the loading process failed + * @throws ClassNotFoundException if the loading process failed + * @throws IOException if the loading process failed * @since Envoy Client v0.3-alpha */ - public void loadUserData() throws Exception {} + public void loadUserData() throws ClassNotFoundException, IOException { + try (var in = new ObjectInputStream(new FileInputStream(userFile))) { + chats = (ArrayList) in.readObject(); + cacheMap = (CacheMap) in.readObject(); + lastSync = (Instant) in.readObject(); + } + } /** * Loads the ID generator. Any exception thrown during this process is ignored. * * @since Envoy Client v0.3-alpha */ - public void loadIDGenerator() {} - + public void loadIDGenerator() { + try { + idGenerator = SerializationUtils.read(idGeneratorFile, IDGenerator.class); + } catch (ClassNotFoundException | IOException e) {} + } /** * Synchronizes the contact list of the client user with the chat and user * storage. diff --git a/client/src/main/java/envoy/client/data/PersistentLocalDB.java b/client/src/main/java/envoy/client/data/PersistentLocalDB.java deleted file mode 100644 index 1b0c310..0000000 --- a/client/src/main/java/envoy/client/data/PersistentLocalDB.java +++ /dev/null @@ -1,90 +0,0 @@ -package envoy.client.data; - -import java.io.*; -import java.time.Instant; -import java.util.ArrayList; -import java.util.HashMap; - -import envoy.data.IDGenerator; -import envoy.util.SerializationUtils; - -/** - * Implements a {@link LocalDB} in a way that stores all information inside a - * folder on the local file system. - *

- * Project: envoy-client
- * File: PersistentLocalDB.java
- * Created: 27.10.2019
- * - * @author Kai S. K. Engelbart - * @author Maximilian Käfer - * @since Envoy Client v0.1-alpha - */ -public final class PersistentLocalDB extends LocalDB { - - private File dbDir, userFile, idGeneratorFile, usersFile; - - /** - * Constructs an empty local database. To serialize any user-specific data to - * the file system, call {@link PersistentLocalDB#initializeUserStorage()} first - * and then {@link PersistentLocalDB#save(boolean)}. - * - * @param dbDir the directory in which to persist data - * @throws IOException if {@code dbDir} is a file (and not a directory) - * @since Envoy Client v0.1-alpha - */ - public PersistentLocalDB(File dbDir) throws IOException { - this.dbDir = dbDir; - - // Test if the database directory is actually a directory - if (dbDir.exists() && !dbDir.isDirectory()) - throw new IOException(String.format("LocalDBDir '%s' is not a directory!", dbDir.getAbsolutePath())); - - // Initialize global files - idGeneratorFile = new File(dbDir, "id_gen.db"); - usersFile = new File(dbDir, "users.db"); - } - - /** - * Creates a database file for a user-specific list of chats. - * - * @throws IllegalStateException if the client user is not specified - * @since Envoy Client v0.1-alpha - */ - @Override - public void initializeUserStorage() { - if (user == null) throw new IllegalStateException("Client user is null, cannot initialize user storage"); - userFile = new File(dbDir, user.getID() + ".db"); - } - - @Override - public void save(boolean isOnline) throws IOException { - // Save users - SerializationUtils.write(usersFile, users); - - // Save user data and last sync time stamp - if (user != null) SerializationUtils.write(userFile, chats, cacheMap, isOnline ? Instant.now() : lastSync); - - // Save id generator - if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator); - } - - @Override - public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); } - - @Override - public void loadUserData() throws ClassNotFoundException, IOException { - try (var in = new ObjectInputStream(new FileInputStream(userFile))) { - chats = (ArrayList) in.readObject(); - cacheMap = (CacheMap) in.readObject(); - lastSync = (Instant) in.readObject(); - } - } - - @Override - public void loadIDGenerator() { - try { - idGenerator = SerializationUtils.read(idGeneratorFile, IDGenerator.class); - } catch (ClassNotFoundException | IOException e) {} - } -} diff --git a/client/src/main/java/envoy/client/data/TransientLocalDB.java b/client/src/main/java/envoy/client/data/TransientLocalDB.java deleted file mode 100644 index 5c7d2a6..0000000 --- a/client/src/main/java/envoy/client/data/TransientLocalDB.java +++ /dev/null @@ -1,15 +0,0 @@ -package envoy.client.data; - -/** - * Implements a {@link LocalDB} in a way that does not persist any information - * after application shutdown. - *

- * Project: envoy-client
- * File: TransientLocalDB.java
- * Created: 3 Feb 2020
- * - * @author Kai S. K. Engelbart - * @since Envoy Client v0.3-alpha - */ -public final class TransientLocalDB extends LocalDB { -} diff --git a/client/src/main/java/envoy/client/ui/Startup.java b/client/src/main/java/envoy/client/ui/Startup.java index 8e7132a..30757c5 100644 --- a/client/src/main/java/envoy/client/ui/Startup.java +++ b/client/src/main/java/envoy/client/ui/Startup.java @@ -1,12 +1,9 @@ package envoy.client.ui; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; +import java.io.*; import java.time.Instant; import java.util.concurrent.TimeoutException; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.util.logging.*; import javafx.application.Application; import javafx.scene.control.Alert; @@ -19,8 +16,7 @@ import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.controller.LoginScene; import envoy.data.*; import envoy.data.User.UserStatus; -import envoy.event.GroupMessageStatusChange; -import envoy.event.MessageStatusChange; +import envoy.event.*; import envoy.exception.EnvoyException; import envoy.util.EnvoyLog; @@ -70,11 +66,8 @@ public final class Startup extends Application { logger.log(Level.INFO, "Envoy starting..."); // Initialize the local database - if (config.isIgnoreLocalDB()) { - localDB = new TransientLocalDB(); - new Alert(AlertType.WARNING, "Ignoring local database.\nMessages will not be saved!").showAndWait(); - } else try { - localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath())); + try { + localDB = new LocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath())); } catch (final IOException e3) { logger.log(Level.SEVERE, "Could not initialize local database: ", e3); new Alert(AlertType.ERROR, "Could not initialize local database!\n" + e3).showAndWait();