diff --git a/pom.xml b/pom.xml
index 3d271ff..db5b8da 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
com.github.informatik-ag-ngl
envoy-common
- f~login_or_registration-SNAPSHOT
+ develop-SNAPSHOT
com.github.informatik-ag-ngl
diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java
index 31aa072..98e8b3d 100644
--- a/src/main/java/envoy/server/Startup.java
+++ b/src/main/java/envoy/server/Startup.java
@@ -6,6 +6,7 @@ import java.util.Set;
import com.jenkov.nioserver.Server;
+import envoy.server.data.ConfigItem;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectMessageProcessor;
import envoy.server.net.ObjectMessageReader;
@@ -38,10 +39,14 @@ public class Startup {
processors.add(new IdGeneratorRequestProcessor());
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
- // TODO: Prevent lazy DB initialization
- PersistenceManager.getPersistenceManager();
+ initializeCurrentMessageId();
server.start();
server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance());
}
+
+ private static void initializeCurrentMessageId() {
+ PersistenceManager persMan = PersistenceManager.getPersistenceManager();
+ if (persMan.getConfigItemById("currentMessageId") == null) persMan.addConfigItem(new ConfigItem("currentMessageId", "0"));
+ }
}
\ No newline at end of file
diff --git a/src/main/java/envoy/server/data/ConfigItem.java b/src/main/java/envoy/server/data/ConfigItem.java
new file mode 100644
index 0000000..2fdb8ba
--- /dev/null
+++ b/src/main/java/envoy/server/data/ConfigItem.java
@@ -0,0 +1,65 @@
+package envoy.server.data;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+/**
+ * Project: envoy-server-standalone
+ * File: ConfigItem.java
+ * Created: 28 Jan 2020
+ *
+ * @author Kai S. K. Engelbart
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+@Entity
+@Table(name = "configuration")
+public class ConfigItem {
+
+ @Id
+ private String key;
+ private String value;
+
+ /**
+ * Creates an instance of @link{ConfigItem}.
+ *
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public ConfigItem() {}
+
+ /**
+ * Creates an instance of @link{ConfigItem}.
+ *
+ * @param key
+ * @param value
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public ConfigItem(String key, String value) {
+ this.key = key;
+ this.value = value;
+ }
+
+ /**
+ * @return the key
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public String getKey() { return key; }
+
+ /**
+ * @param key the key to set
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public void setKey(String key) { this.key = key; }
+
+ /**
+ * @return the value
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public String getValue() { return value; }
+
+ /**
+ * @param value the value to set
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public void setValue(String value) { this.value = value; }
+}
diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java
index 9330296..97d2a3a 100644
--- a/src/main/java/envoy/server/database/PersistenceManager.java
+++ b/src/main/java/envoy/server/database/PersistenceManager.java
@@ -5,6 +5,7 @@ import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
+import envoy.server.data.ConfigItem;
import envoy.server.data.Message;
import envoy.server.data.User;
@@ -59,13 +60,29 @@ public class PersistenceManager {
entityManager.getTransaction().commit();
}
+ /**
+ * Adds a {@link ConfigItem} to the database.
+ *
+ * @param configItem the {@link ConfigItem} to add to the database
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public void addConfigItem(ConfigItem configItem) {
+ entityManager.getTransaction().begin();
+ entityManager.persist(configItem);
+ entityManager.getTransaction().commit();
+ }
+
/**
* Updates a {@link User} in the database
*
* @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); }
+ public void updateUser(User user) {
+ entityManager.getTransaction().begin();
+ entityManager.merge(user);
+ entityManager.getTransaction().commit();
+ }
/**
* Updates a {@link Message} in the database.
@@ -73,7 +90,23 @@ public class PersistenceManager {
* @param message the message to update
* @since Envoy Server Standalone v0.1-alpha
*/
- public void updateMessage(Message message) { entityManager.merge(message); }
+ public void updateMessage(Message message) {
+ entityManager.getTransaction().begin();
+ entityManager.merge(message);
+ entityManager.getTransaction().commit();
+ }
+
+ /**
+ * Updates a {@link ConfigItem} in the database.
+ *
+ * @param configItem the configItem to update
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public void updateConfigItem(ConfigItem configItem) {
+ entityManager.getTransaction().begin();
+ entityManager.merge(configItem);
+ entityManager.getTransaction().commit();
+ }
/**
* Searches for a {@link User} with a specific id.
@@ -104,6 +137,8 @@ public class PersistenceManager {
*/
public Message getMessageById(long id) { return entityManager.find(Message.class, id); }
+ public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); }
+
/**
* Returns all messages received while being offline.
*
diff --git a/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java
index fb958e8..31ba1c0 100644
--- a/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java
+++ b/src/main/java/envoy/server/processors/IdGeneratorRequestProcessor.java
@@ -5,6 +5,8 @@ import java.io.IOException;
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.net.ObjectWriteProxy;
/**
@@ -17,8 +19,7 @@ import envoy.server.net.ObjectWriteProxy;
*/
public class IdGeneratorRequestProcessor implements ObjectProcessor {
- private static long currentId = 0;
- private static final long ID_RANGE = 2;
+ private static final long ID_RANGE = 2;
@Override
public Class getInputClass() { return IdGeneratorRequest.class; }
@@ -27,8 +28,10 @@ public class IdGeneratorRequestProcessor implements ObjectProcessor