From 0347715d7207c10b53d06e936ba392f5bded8232 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 15 Feb 2020 22:43:20 +0100 Subject: [PATCH] Moved PersistenceManager into data package, refactored transaction use --- src/main/java/envoy/server/Startup.java | 2 +- src/main/java/envoy/server/data/Message.java | 1 - .../PersistenceManager.java | 81 +++++++++---------- .../envoy/server/net/ConnectionManager.java | 2 +- .../processors/ContactOperationProcessor.java | 2 +- .../ContactsRequestEventProcessor.java | 2 +- .../IdGeneratorRequestProcessor.java | 2 +- .../processors/LoginCredentialProcessor.java | 2 +- .../server/processors/MessageProcessor.java | 2 +- .../MessageStatusChangeProcessor.java | 2 +- .../processors/UserStatusChangeProcessor.java | 2 +- 11 files changed, 49 insertions(+), 51 deletions(-) rename src/main/java/envoy/server/{database => data}/PersistenceManager.java (82%) diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index 13cd852..fe278d1 100644 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -7,7 +7,7 @@ import java.util.Set; import com.jenkov.nioserver.Server; import envoy.server.data.ConfigItem; -import envoy.server.database.PersistenceManager; +import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectMessageProcessor; import envoy.server.net.ObjectMessageReader; diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index 6eceb52..35a32e4 100644 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -5,7 +5,6 @@ import java.util.Date; import javax.persistence.*; import envoy.data.MessageBuilder; -import envoy.server.database.PersistenceManager; /** * This class serves as a way to let Hibernate communicate with the server diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java similarity index 82% rename from src/main/java/envoy/server/database/PersistenceManager.java rename to src/main/java/envoy/server/data/PersistenceManager.java index 12ac446..208db2f 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -1,4 +1,4 @@ -package envoy.server.database; +package envoy.server.data; import java.util.Date; import java.util.List; @@ -8,9 +8,6 @@ import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import envoy.data.User.UserStatus; -import envoy.server.data.ConfigItem; -import envoy.server.data.Message; -import envoy.server.data.User; import envoy.server.net.ConnectionManager; /** @@ -34,13 +31,13 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ private PersistenceManager() { - transaction.begin(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { + transaction.begin(); ConnectionManager.getInstance() .getOnlineUsers() .stream() .map(this::getUserById) - .forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); updateUser(user); }); + .forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); entityManager.merge(user); }); transaction.commit(); })); } @@ -54,13 +51,10 @@ public class PersistenceManager { /** * Adds a {@link User} to the database. * - * @param User the {@link User} to add to the database + * @param user the {@link User} to add to the database * @since Envoy Server Standalone v0.1-alpha */ - public void addUser(User User) { - entityManager.persist(User); - entityManager.flush(); - } + public void addUser(User user) { persist(user); } /** * Adds a {@link Message} to the database. @@ -68,10 +62,7 @@ public class PersistenceManager { * @param message the {@link Message} to add to the database * @since Envoy Server Standalone v0.1-alpha */ - public void addMessage(Message message) { - entityManager.persist(message); - entityManager.flush(); - } + public void addMessage(Message message) { persist(message); } /** * Adds a {@link ConfigItem} to the database. @@ -79,10 +70,7 @@ public class PersistenceManager { * @param configItem the {@link ConfigItem} to add to the database * @since Envoy Server Standalone v0.1-alpha */ - public void addConfigItem(ConfigItem configItem) { - entityManager.persist(configItem); - entityManager.flush(); - } + public void addConfigItem(ConfigItem configItem) { persist(configItem); } /** * Updates a {@link User} in the database @@ -90,10 +78,7 @@ public class PersistenceManager { * @param user the {@link User} to add to the database * @since Envoy Server Standalone v0.1-alpha */ - public void updateUser(User user) { - entityManager.merge(user); - entityManager.flush(); - } + public void updateUser(User user) { merge(user); } /** * Updates a {@link Message} in the database. @@ -101,10 +86,7 @@ public class PersistenceManager { * @param message the message to update * @since Envoy Server Standalone v0.1-alpha */ - public void updateMessage(Message message) { - entityManager.merge(message); - entityManager.flush(); - } + public void updateMessage(Message message) { merge(message); } /** * Updates a {@link ConfigItem} in the database. @@ -112,10 +94,7 @@ public class PersistenceManager { * @param configItem the configItem to update * @since Envoy Server Standalone v0.1-alpha */ - public void updateConfigItem(ConfigItem configItem) { - entityManager.merge(configItem); - entityManager.flush(); - } + public void updateConfigItem(ConfigItem configItem) { merge(configItem); } /** * Deletes a {@link User} in the database. @@ -123,10 +102,7 @@ public class PersistenceManager { * @param user the {@link User} to delete * @since Envoy Server Standalone v0.1-alpha */ - public void deleteUser(User user) { - entityManager.remove(user); - entityManager.flush(); - } + public void deleteUser(User user) { remove(user); } /** * Deletes a {@link Message} in the database. @@ -134,10 +110,7 @@ public class PersistenceManager { * @param message the {@link Message} to delete * @since Envoy Server Standalone v0.1-alpha */ - public void deleteMessage(Message message) { - entityManager.remove(message); - entityManager.flush(); - } + public void deleteMessage(Message message) { remove(message); } /** * Searches for a {@link User} with a specific id. @@ -211,12 +184,20 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public void addContact(long userId1, long userId2) { + + // Get users by ID User u1 = getUserById(userId1); User u2 = getUserById(userId2); + + // Add users to each others contact lists u1.getContacts().add(u2); u2.getContacts().add(u1); - updateUser(u1); - updateUser(u2); + + // Synchronize changes with the database + transaction.begin(); + entityManager.merge(u1); + entityManager.merge(u2); + transaction.commit(); } /** @@ -227,4 +208,22 @@ public class PersistenceManager { public List getContacts(User user) { return entityManager.createNamedQuery("getContactsOfUser").setParameter("user", user).getResultList(); } + + private void persist(Object obj) { + transaction.begin(); + entityManager.persist(obj); + transaction.commit(); + } + + private void merge(Object obj) { + transaction.begin(); + entityManager.merge(obj); + transaction.commit(); + } + + private void remove(Object obj) { + transaction.begin(); + entityManager.remove(obj); + transaction.commit(); + } } \ No newline at end of file diff --git a/src/main/java/envoy/server/net/ConnectionManager.java b/src/main/java/envoy/server/net/ConnectionManager.java index 9419ed3..4d9163c 100644 --- a/src/main/java/envoy/server/net/ConnectionManager.java +++ b/src/main/java/envoy/server/net/ConnectionManager.java @@ -5,7 +5,7 @@ import java.util.*; import com.jenkov.nioserver.ISocketIdListener; import envoy.data.User.UserStatus; -import envoy.server.database.PersistenceManager; +import envoy.server.data.PersistenceManager; import envoy.server.processors.UserStatusChangeProcessor; /** diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java index 0a79197..5b0e079 100644 --- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -6,7 +6,7 @@ import java.util.Arrays; import envoy.data.Contacts; import envoy.event.ContactOperationEvent; import envoy.server.ObjectProcessor; -import envoy.server.database.PersistenceManager; +import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; diff --git a/src/main/java/envoy/server/processors/ContactsRequestEventProcessor.java b/src/main/java/envoy/server/processors/ContactsRequestEventProcessor.java index fe003f4..f00492c 100644 --- a/src/main/java/envoy/server/processors/ContactsRequestEventProcessor.java +++ b/src/main/java/envoy/server/processors/ContactsRequestEventProcessor.java @@ -7,8 +7,8 @@ import envoy.data.Contacts; import envoy.event.ContactSearchRequest; import envoy.event.ContactSearchResult; import envoy.server.ObjectProcessor; +import envoy.server.data.PersistenceManager; import envoy.server.data.User; -import envoy.server.database.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; diff --git a/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java index 39a900c..b8e0cda 100644 --- a/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java +++ b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java @@ -6,7 +6,7 @@ import envoy.data.IdGenerator; import envoy.event.IdGeneratorRequest; import envoy.server.ObjectProcessor; import envoy.server.data.ConfigItem; -import envoy.server.database.PersistenceManager; +import envoy.server.data.PersistenceManager; import envoy.server.net.ObjectWriteProxy; /** diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 2e3b6e0..90cd09a 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -14,7 +14,7 @@ import envoy.data.User.UserStatus; import envoy.event.HandshakeRejectionEvent; import envoy.server.ObjectProcessor; import envoy.server.data.Message; -import envoy.server.database.PersistenceManager; +import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index a1e4f7c..4b2175a 100644 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -6,7 +6,7 @@ import java.util.Date; import envoy.data.Message; import envoy.event.MessageStatusChangeEvent; import envoy.server.ObjectProcessor; -import envoy.server.database.PersistenceManager; +import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index 9949e43..2769c3a 100644 --- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -6,7 +6,7 @@ import envoy.data.Message.MessageStatus; import envoy.event.MessageStatusChangeEvent; import envoy.exception.EnvoyException; import envoy.server.ObjectProcessor; -import envoy.server.database.PersistenceManager; +import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; diff --git a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java index 59f2ca3..c720191 100644 --- a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java @@ -5,8 +5,8 @@ import java.io.IOException; import envoy.data.User.UserStatus; import envoy.event.UserStatusChangeEvent; import envoy.server.ObjectProcessor; +import envoy.server.data.PersistenceManager; import envoy.server.data.User; -import envoy.server.database.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy;