From 0347715d7207c10b53d06e936ba392f5bded8232 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 15 Feb 2020 22:43:20 +0100 Subject: [PATCH 1/5] 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; From 87ca90c333640c20f7bd0075d6f290b82e174898 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 15 Feb 2020 22:47:14 +0100 Subject: [PATCH 2/5] Added received and read dates to database message copy constructor Fixes #24 --- src/main/java/envoy/server/data/Message.java | 9 ++++++--- .../server/processors/MessageProcessor.java | 6 +++--- .../MessageStatusChangeProcessor.java | 18 ++++++++++-------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index 35a32e4..afe907d 100644 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -52,14 +52,15 @@ public class Message { private byte[] attachment; /** - * The constructor for a database object + * The constructor for a database object. * * @since Envoy Server Standalone v0.1-alpha */ public Message() {} - // TODO: everything except ID /** + * Constructs a database message from a common message. + * * @param message the {@link envoy.data.Message} to convert into a database * {@link Message} * @since Envoy Server Standalone v0.1-alpha @@ -70,9 +71,11 @@ public class Message { status = message.getStatus(); text = message.getText(); creationDate = message.getCreationDate(); + receivedDate = message.getReceivedDate(); + readDate = message.getReadDate(); sender = persMan.getUserById(message.getSenderId()); recipient = persMan.getUserById(message.getRecipientId()); - // attachment = message.getAttachment().toByteArray();DOES NOT WORK YET + // TODO: attachment = message.getAttachment().toByteArray();DOES NOT WORK YET } /** diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index 4b2175a..93211f1 100644 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -22,9 +22,6 @@ import envoy.server.net.ObjectWriteProxy; */ public class MessageProcessor implements ObjectProcessor { - @Override - public Class getInputClass() { return Message.class; } - @Override public void process(Message message, long socketId, ObjectWriteProxy writeProxy) { @@ -44,4 +41,7 @@ public class MessageProcessor implements ObjectProcessor { } PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message)); } + + @Override + public Class getInputClass() { return Message.class; } } diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index 2769c3a..ad52145 100644 --- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -20,26 +20,28 @@ import envoy.server.net.ObjectWriteProxy; */ public class MessageStatusChangeProcessor implements ObjectProcessor { - @Override - public Class getInputClass() { return MessageStatusChangeEvent.class; } + private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); + private final ConnectionManager connectionManager = ConnectionManager.getInstance(); @Override public void process(MessageStatusChangeEvent input, long socketId, ObjectWriteProxy writeProxy) throws IOException { try { // any other status than read is not supposed to be sent to the server - if (input.get() != MessageStatus.READ) throw new EnvoyException("Message" + input.getId() + "has an invalid status"); + if (input.get() != MessageStatus.READ) throw new EnvoyException("Message " + input + " has an invalid status"); } catch (EnvoyException e) { throw new IOException(e); } - ConnectionManager conMan = ConnectionManager.getInstance(); - PersistenceManager perMan = PersistenceManager.getInstance(); - envoy.server.data.Message msg = perMan.getMessageById(input.getId()); + envoy.server.data.Message msg = persistenceManager.getMessageById(input.getId()); msg.setStatus(input.get()); msg.setReadDate(input.getDate()); - perMan.updateMessage(msg); + persistenceManager.updateMessage(msg); // Notifies the sender of the message about the status-update to READ - if (conMan.isOnline(msg.getSender().getId())) writeProxy.write(conMan.getSocketId(msg.getSender().getId()), input); + final long senderId = msg.getSender().getId(); + if (connectionManager.isOnline(senderId)) writeProxy.write(connectionManager.getSocketId(senderId), input); } + + @Override + public Class getInputClass() { return MessageStatusChangeEvent.class; } } From 2813b54e32128aa62aaa5c5618a8d451720d4148 Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 16 Feb 2020 10:31:17 +0100 Subject: [PATCH 3/5] Copying received and read dates in Message#toCommonMessage() --- src/main/java/envoy/server/data/Message.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index afe907d..2655cbf 100644 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -79,13 +79,21 @@ public class Message { } /** - * @return a database {@link Message} converted into an - * {@link envoy.data.Message} + * Converts this message into an instance of {@link envoy.data.Message}. + * + * @return a {@link envoy.data.Message} containing the same values as this + * message * @since Envoy Server Standalone v0.1-alpha */ public envoy.data.Message toCommonMessage() { - // TODO: Attachment, dates - return new MessageBuilder(sender.getId(), recipient.getId(), id).setText(text).setDate(creationDate).setStatus(status).build(); + // TODO: Attachment + envoy.data.Message message = new MessageBuilder(sender.getId(), recipient.getId(), id).setText(text) + .setDate(creationDate) + .setStatus(status) + .build(); + message.setReceivedDate(receivedDate); + message.setReadDate(readDate); + return message; } /** From 47ca47a7611cafcf59d49be1af283ddb3bdae0a5 Mon Sep 17 00:00:00 2001 From: delvh Date: Tue, 18 Feb 2020 16:28:32 +0100 Subject: [PATCH 4/5] Added null check to ObjectMessageProcessor --- src/main/java/envoy/server/net/ObjectMessageProcessor.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/server/net/ObjectMessageProcessor.java b/src/main/java/envoy/server/net/ObjectMessageProcessor.java index 226fb69..346e253 100644 --- a/src/main/java/envoy/server/net/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/net/ObjectMessageProcessor.java @@ -38,7 +38,12 @@ public class ObjectMessageProcessor implements IMessageProcessor { public void process(Message message, WriteProxy writeProxy) { try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { Object obj = in.readObject(); - System.out.println("Read object: " + obj.toString()); + if (obj == null) { + System.out.println("received a null object"); + return; + } + + System.out.println("Read object: " + obj); // Process object processors.stream().filter(p -> p.getInputClass().isInstance(obj)).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> { From ca2fa5d1e20382a9bd45f3ea0f372e3c9faab21b Mon Sep 17 00:00:00 2001 From: delvh Date: Tue, 18 Feb 2020 16:36:31 +0100 Subject: [PATCH 5/5] Added newline at the end of PersistenceManager --- src/main/java/envoy/server/data/PersistenceManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index 208db2f..57fc469 100644 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -184,15 +184,15 @@ 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); - + // Synchronize changes with the database transaction.begin(); entityManager.merge(u1); @@ -226,4 +226,4 @@ public class PersistenceManager { entityManager.remove(obj); transaction.commit(); } -} \ No newline at end of file +}