aboutsummaryrefslogtreecommitdiff
path: root/id/server
diff options
context:
space:
mode:
Diffstat (limited to 'id/server')
-rw-r--r--id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java45
-rw-r--r--id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java86
2 files changed, 65 insertions, 66 deletions
diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java
index 43f7d9454..873208aaf 100644
--- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java
+++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java
@@ -3,53 +3,52 @@ package com.datentechnik.moa.id.conf.persistence;
import java.util.List;
/**
- *
+ * An interface for a key-value configuration.
*/
public interface Configuration {
/**
- *
- * @param key
- * @return
+ * Get the value associated with the given key as {@link Object}.
+ * @param key the key
+ * @return the object associated with the given key or {@code null} if the key does not exist or does not have a value.
*/
Object get(String key);
/**
+ * Get the object of type {@code T} associated with the given key.
*
- * @param key
- * @param clazz
- * @return
+ * @param key the key
+ * @param clazz the type of the requested object
+ * @return the object associated with the given key or {@code null} if the key does not exist or does not have a value.
*/
<T> T get(String key, Class<T> clazz);
/**
+ * Store an object associated with a key. If the given object is set to {@code null} then the entry associated with the key is deleted.
*
- * @param key
- * @param value
+ * @param key the key under which the value is stored, respectively key determining the entry to be deleted.
+ * @param value the object to store. if value is set to {@code null} then the entry associated with key {@code key} is deleted.
+ * @return {@code true} if the operation was carried out successfully, {@code false} otherwise.
*/
boolean set(String key, Object value);
/**
+ * Get the object of type {@code T} associated with the given key from the database. If the key does not exist or does not have a value, the given default
+ * value is returned.
*
- * @param key
- * @param clazz
- * @param defaultValue
- * @return
+ * @param key the key
+ * @param clazz the type of the requested object
+ * @param defaultValue the default value to return
+ * @return the object associated with the given key or {@code defaultValue} if the key does not exist or does not have a value.
*/
<T> T get(String key, Class<T> clazz, Object defaultValue);
/**
- *
- * @param key
- * @param clazz
- * @return
+ * Get a list of objects associated with the given key. The list may be empty or contain only a single object.
+ * @param key the key
+ * @param clazz the type of the requested object
+ * @return a list containing objects of type {@code T} or an empty list if no objects are associated with the key.
*/
<T> List<T> getList(String key, Class<T> clazz);
- /**
- *
- * @param key
- * @return
- */
- void delete(String key);
} \ No newline at end of file
diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java
index eebecf509..6d05af791 100644
--- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java
+++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java
@@ -4,6 +4,8 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Component;
@@ -21,12 +23,14 @@ import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
/**
- *
- *
+ * The implementation of a key-value configuration implementing the {@link Configuration} interface.
+ * It employs the {@link ConfigPropertyDao} to persist configuration data.
*/
@Component
public class ConfigurationImpl implements Configuration {
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
ConfigPropertyDao configPropertyDao;
private JsonMapper mapper = new JsonMapper();
@@ -42,10 +46,10 @@ public class ConfigurationImpl implements Configuration {
@Override
public Object get(String key) {
// return null if key does not exist
-
try {
- return mapper.deserialize(getPropertyValue(key), null);
+ return mapper.deserialize(configPropertyDao.getProperty(key).getValue(), null);
} catch (Exception e) {
+ log.trace("Error while deserializing value of key '{}' to object.", key);
return null;
}
}
@@ -58,6 +62,7 @@ public class ConfigurationImpl implements Configuration {
try {
return clazz.cast(mapper.deserialize(property.getValue(), clazz));
} catch (IOException e) {
+ log.trace("Error while deserializing value of key '{}' to object of type {}.",key,clazz.getClass());
return null;
}
} else {
@@ -68,24 +73,27 @@ public class ConfigurationImpl implements Configuration {
@Override
public boolean set(String key, Object value) {
- ConfigProperty keyValue = new ConfigProperty();
- keyValue.setKey(key);
- try {
- keyValue.setValue(mapper.serialize(value));
- // System.out.println(">> key - value: " + keyValue.getKey() + " - " + keyValue.getValue() + "\n");
- configPropertyDao.saveProperty(keyValue);
+ if (value == null) {
+ configPropertyDao.delete(key);
return true;
- } catch (JsonProcessingException e) {
- // TODO do proper error handling
- e.printStackTrace();
- return false;
- }
+ } else {
+ ConfigProperty keyValue = new ConfigProperty();
+ keyValue.setKey(key);
+ try {
+ keyValue.setValue(mapper.serialize(value));
+ configPropertyDao.saveProperty(keyValue);
+ return true;
+ } catch (JsonProcessingException e) {
+ log.trace("Error while serializing object for key '{}'.", key);
+ return false;
+ }
+ }
}
@Override
public <T> T get(String key, Class<T> clazz, Object defaultValue) {
- // TODO complete the method
+
T value = get(key, clazz);
if (value != null) {
return value;
@@ -116,36 +124,30 @@ public class ConfigurationImpl implements Configuration {
}
return tmp;
} catch (IOException e) {
+ log.trace("Error while deserializing value for key '{}' to List<{}>.", key,clazz.getClass());
return new ArrayList<T>();
}
}
- private String getPropertyValue(String key) {
- ConfigProperty property = configPropertyDao.getProperty(key);
- return property.getValue();
- }
-
- @Override
- public void delete(String key) {
- configPropertyDao.delete(key);
- }
-
/**
- *
+ * Helper class to handle the JSON (de-)serialization.
*
*/
private class JsonMapper {
private ObjectMapper mapper = new ObjectMapper();
+ /**
+ * The default constructor where the default pretty printer is disabled.
+ */
public JsonMapper() {
this(false);
}
/**
- *
- * @param prettyPrint
+ * The constructor.
+ * @param prettyPrint enables or disables the default pretty printer
*/
public JsonMapper(boolean prettyPrint) {
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
@@ -157,25 +159,26 @@ public class ConfigurationImpl implements Configuration {
}
/**
- *
- * @param value
- * @return
- * @throws JsonProcessingException
+ * Serialize an object to a JSON string.
+ * @param value the object to serialize
+ * @return a JSON string
+ * @throws JsonProcessingException thrown when an error occurs during serialization
*/
public String serialize(Object value) throws JsonProcessingException {
return mapper.writeValueAsString(value);
}
/**
+ * Deserialize a JSON string.
*
- * @param value
- * @param clazz
- * @return
- * @throws JsonParseException
- * @throws JsonMappingException
- * @throws IOException
+ * @param value the JSON string to deserialize
+ * @param clazz optional parameter that determines the type of the returned object. If not set, an {@link Object} is returned.
+ * @return the deserialized JSON string as an object of type {@code clazz} or {@link Object}
+ * @throws JsonParseException if the JSON string contains invalid content.
+ * @throws JsonMappingException if the input JSON structure does not match structure expected for result type
+ * @throws IOException if an I/O problem occurs (e.g. unexpected end-of-input)
*/
- public <T> Object deserialize(String value, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
+ public <T> Object deserialize(String value, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
if (clazz != null) {
@@ -184,10 +187,7 @@ public class ConfigurationImpl implements Configuration {
} else {
return mapper.readValue(value, Object.class);
}
-
}
-
};
-
}