diff options
author | Martin Bonato <mbonato@datentechnik-innovation.com> | 2015-04-09 13:24:55 +0200 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-06-19 11:09:55 +0200 |
commit | 0fb4c31f049d71e917dfbfdab96553a807195d0c (patch) | |
tree | edeb1c502de674320928feb71bcd83c3056d135e /id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence | |
parent | 6d1f99a50744de690afccc12568539c23d839f93 (diff) | |
download | moa-id-spss-0fb4c31f049d71e917dfbfdab96553a807195d0c.tar.gz moa-id-spss-0fb4c31f049d71e917dfbfdab96553a807195d0c.tar.bz2 moa-id-spss-0fb4c31f049d71e917dfbfdab96553a807195d0c.zip |
Rename java packages
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence')
3 files changed, 294 insertions, 0 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/Configuration.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/Configuration.java new file mode 100644 index 000000000..f357fc570 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/Configuration.java @@ -0,0 +1,60 @@ +package at.gv.egovernment.moa.id.commons.config.persistence; + +import java.util.List; + +/** + * An interface for a key-value configuration. + */ +public interface Configuration { + + /** + * Gets all keys in the database. NOTE: may return an empty list or {@code null}. + * @return a List containing all keys in the database or {@code null}. + */ + List<String> getAllKeys(); + + /** + * 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 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 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 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); + + /** + * 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); + +}
\ No newline at end of file diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/ConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/ConfigurationImpl.java new file mode 100644 index 000000000..c90b60440 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/ConfigurationImpl.java @@ -0,0 +1,161 @@ +package at.gv.egovernment.moa.id.commons.config.persistence; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.EntityExistsException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Required; +import org.springframework.stereotype.Component; + +import at.gv.egovernment.moa.id.commons.db.dao.config.ConfigProperty; +import at.gv.egovernment.moa.id.commons.db.dao.config.ConfigPropertyDao; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +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(); + + /** + * Sets the {@link ConfigPropertyDao}. + * @param configPropertyDao the ConfigPropertyDao + */ + @Required + public void setConfigPropertyDao(ConfigPropertyDao configPropertyDao) { + this.configPropertyDao = configPropertyDao; + } + + @Override + public List<String> getAllKeys(){ + try { + return this.configPropertyDao.getAllKeys(); + } catch (Exception e) { + log.debug("Error while retrieving a list of all keys in the database."); + return null; + } + } + + @Override + public Object get(String key) { + // return null if key does not exist + try { + ConfigProperty property = configPropertyDao.getProperty(key); + if (property != null && property.getValue() != null) { + return mapper.deserialize(property.getValue(), null); + } else { + return null; + } + } catch (IllegalArgumentException e) { + log.debug("Error while searching for key '{}' in the database.", key); + return null; + } catch (Exception e) { + log.debug("Error while deserializing value of key '{}' to object.", key); + return null; + } + } + + @Override + public <T> T get(String key, Class<T> clazz) { + // return null if key does not exist + try { + ConfigProperty property = configPropertyDao.getProperty(key); + if (property != null && property.getValue() != null) { + return clazz.cast(mapper.deserialize(property.getValue(), clazz)); + } else { + return null; + } + } catch (IllegalArgumentException e) { + log.debug("Error while searching for key '{}' in the database.", key); + return null; + } catch (Exception e) { + log.debug("Error while deserializing value of key '{}' to object of type {}.", key, clazz.getClass()); + return null; + } + } + + @Override + public boolean set(String key, Object value) { + + try { + if (value == null) { + configPropertyDao.delete(key); + return true; + } else { + + ConfigProperty keyValue = new ConfigProperty(); + keyValue.setKey(key); + + keyValue.setValue(mapper.serialize(value)); + configPropertyDao.saveProperty(keyValue); + return true; + } + } catch (JsonProcessingException e) { + log.debug("Error while serializing object for key '{}'.", key); + return false; + } catch (EntityExistsException e) { + log.debug("Property '{}' already exists!", key); + return false; + } catch (Exception e) { + log.debug("Error while setting value for key '{}' in the database.", key); + return false; + } + } + + @Override + public <T> T get(String key, Class<T> clazz, Object defaultValue) { + + T value = get(key, clazz); + if (value != null) { + return value; + } else { + return clazz.cast(defaultValue); + } + } + + @SuppressWarnings("unchecked") + @Override + public <T> List<T> getList(String key, Class<T> clazz) { + + CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(List.class, clazz); + try { + if ((configPropertyDao.getProperty(key) == null) + || (configPropertyDao.getProperty(key).getValue() == null)) { + return new ArrayList<T>(); + } + String json = configPropertyDao.getProperty(key).getValue(); + ObjectMapper mapper = new ObjectMapper(); + + return (List<T>) mapper.readValue(json, listType); + } catch (JsonMappingException e) { + ArrayList<T> tmp = new ArrayList<T>(); + T value = get(key, clazz); + if (value != null) { + tmp.add(value); + } + return tmp; + } catch (IOException e) { + log.debug("Error while deserializing value for key '{}' to List<{}>.", key, clazz.getClass()); + return new ArrayList<T>(); + } catch (Exception e){ + log.debug("Error while searching key '{}' in the database.", key); + return new ArrayList<T>(); + } + } + +} diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/JsonMapper.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/JsonMapper.java new file mode 100644 index 000000000..6138d571b --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/JsonMapper.java @@ -0,0 +1,73 @@ +package at.gv.egovernment.moa.id.commons.config.persistence; + +import java.io.IOException; + +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.type.TypeFactory; + +/** + * Helper class to handle the JSON (de-)serialization. + * + */ +public class JsonMapper { + + private ObjectMapper mapper = new ObjectMapper(); + + /** + * The default constructor where the default pretty printer is disabled. + */ + public JsonMapper() { + this(false); + } + + /** + * The constructor. + * @param prettyPrint enables or disables the default pretty printer + */ + public JsonMapper(boolean prettyPrint) { + mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); + mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); + mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); + if (prettyPrint) { + mapper.enable(SerializationFeature.INDENT_OUTPUT); + } + } + + /** + * 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 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{ + + ObjectMapper mapper = new ObjectMapper(); + if (clazz != null) { + JavaType javaType = TypeFactory.defaultInstance().constructType(clazz); + return mapper.readValue(value, javaType); + } else { + return mapper.readValue(value, Object.class); + } + } +} |