diff options
author | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-07-02 07:26:30 +0200 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-07-02 07:26:30 +0200 |
commit | 5bc01dab26425144a41dbece04b642fb963e1315 (patch) | |
tree | 543fe5f7b8d576c7e85ba711c1f9bf03d3dc699f /id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence | |
parent | 06e63a4fb89dd92583fc3e72e12750becdd6c8ef (diff) | |
download | moa-id-spss-5bc01dab26425144a41dbece04b642fb963e1315.tar.gz moa-id-spss-5bc01dab26425144a41dbece04b642fb963e1315.tar.bz2 moa-id-spss-5bc01dab26425144a41dbece04b642fb963e1315.zip |
devel
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence')
2 files changed, 156 insertions, 201 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfiguration.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfiguration.java index 159b587df..fbb1597f3 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfiguration.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfiguration.java @@ -1,5 +1,6 @@ package at.gv.egovernment.moa.id.commons.config.persistence; +import java.util.Map; import java.util.Properties; import at.gv.egiz.components.configuration.api.Configuration; @@ -13,71 +14,42 @@ public interface MOAIDConfiguration extends Configuration { /** * Get all key/value pairs with a prefix * - * @param preFix A key prefix - * @return All key/value pairs with this prefix. The prefix is removed from the key. + * @param preFix: A key prefix + * @param removePrefix: Indicates, if the prefix should be removed from the result key + * @return All key/value pairs with this prefix or null if no key is found. The prefix is removed from the key. * **/ - public Properties getPropertySubset(String preFix) throws ConfigurationException; + public Map<String, String> getPropertySubset(String preFix) throws ConfigurationException; /** + * Searches the configuration use '*' or '%' for wildcards. + * + * Example: + * + * db.server1.url=... + * db.server1.user=... + * db.server1.password=... + * db.server1.driver=... + * db.server2.url=... + * db.server2.user=... + * db.server2.password=... + * db.server2.driver=... + * + * searchString: "db.server1.*" returns + * ["db.server1.url", "db.server1.user", "db.server1.password", "db.server1.driver"] + * @param searchkey The search string + * @return All key/value pairs with this prefix or null if no key is found. The prefix is not removed from the key. + * @throws ConfigurationException if something went wrong + */ + public Map<String, String> searchPropertiesWithWildcard(String searchkey) throws ConfigurationException; + + /** * Load an OnlineApplication configuration and remove the OA key prefix * * @param publicURLPrefix: Unique identifier of online application * @return Properties of the online application or null if no OA is found * @throws ConfigurationException in case of an configuration access error */ - public Properties getOnlineApplication(String publicURLPrefix) throws ConfigurationException; - - -// /** -// * 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 String associated with the given key or {@code null} if the key does not exist or does not have a value. -// */ -// String 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 String 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, String 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); - + public Map<String, String> getOnlineApplication(String publicURLPrefix) throws ConfigurationException; }
\ 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/MOAIDConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java index 2d652d748..06e174942 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java @@ -1,51 +1,108 @@ package at.gv.egovernment.moa.id.commons.config.persistence; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; -import java.util.Properties; +import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; -import org.springframework.beans.factory.annotation.Required; import org.springframework.stereotype.Component; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.transaction.annotation.Transactional; import at.gv.egiz.components.configuration.api.Configuration; import at.gv.egiz.components.configuration.api.ConfigurationException; import at.gv.egovernment.moa.id.commons.config.MOAIDConfigurationConstants; import at.gv.egovernment.moa.id.commons.db.dao.config.ConfigProperty; -import at.gv.egovernment.moa.id.commons.db.dao.config.ConfigPropertyDaoImpl; +import at.gv.egovernment.moa.id.commons.db.dao.config.DatabaseConfigPropertyImpl; +import at.gv.egovernment.moa.id.commons.utils.KeyValueUtils; import at.gv.egovernment.moa.logging.Logger; /** * The implementation of a key-value configuration implementing the {@link Configuration} interface. * It employs the {@link ConfigPropertyDao} to persist configuration data. */ -@Component -public class MOAIDConfigurationImpl extends ConfigPropertyDaoImpl implements MOAIDConfiguration { - - Configuration configPropertyDao; -// private JsonMapper mapper = new JsonMapper(); +@Repository +@EnableTransactionManagement +@Transactional("transactionManager") +public class MOAIDConfigurationImpl extends DatabaseConfigPropertyImpl implements MOAIDConfiguration { - /** - * Sets the {@link ConfigPropertyDao}. - * @param configPropertyDao the ConfigPropertyDao +// Configuration configPropertyDao; +// +// /** +// * Sets the {@link ConfigPropertyDao}. +// * @param configPropertyDao the ConfigPropertyDao +// */ +// @Required +// public void setConfigPropertyDao(Configuration configPropertyDao) { +// this.configPropertyDao = configPropertyDao; +// } + + /* (non-Javadoc) + * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#getPropertySubset(java.lang.String) */ - @Required - public void setConfigPropertyDao(Configuration configPropertyDao) { - this.configPropertyDao = configPropertyDao; - } + public Map<String, String> getPropertySubset(String preFix) throws ConfigurationException{ + EntityManager em = this.getPersistenceContext(); + if (null == em) { + Logger.error("No EntityManager set!"); + throw new ConfigurationException("No EntityManager set!"); + + } + + TypedQuery<ConfigProperty> configQuery = em.createQuery("select * from ConfigProperty dbconfig where dbconfig.key like :key", ConfigProperty.class); + configQuery.setParameter("key", preFix + ".%"); + List<ConfigProperty> configResult = configQuery.getResultList(); + if (configResult == null || configResult.isEmpty()) { + Logger.warn("Found no configuration keys with prefix: " + preFix + ".%"); + return null; + + } + Logger.trace("Find " + configResult.size() + " key/value pairs with prefix: " + preFix + ".%"); + + //build key/value configuration map from database entries + Map<String, String> result = getKeyValueFromDatabaseDAO( + configResult.iterator(), preFix, true); + return result; + + } /* (non-Javadoc) - * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#getPropertySubset(java.lang.String) + * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#searchPropertiesWithWildcard(java.lang.String) */ - public Properties getPropertySubset(String preFix) throws ConfigurationException{ - // TODO Auto-generated method stub - return null; + @Override + public Map<String, String> searchPropertiesWithWildcard(String searchKey) + throws ConfigurationException { + EntityManager em = this.getPersistenceContext(); + if (null == em) { + Logger.error("No EntityManager set!"); + throw new ConfigurationException("No EntityManager set!"); + + } + + TypedQuery<ConfigProperty> configQuery = em.createQuery("select * from ConfigProperty dbconfig where dbconfig.key like :key", ConfigProperty.class); + configQuery.setParameter("key", searchKey.replace("*", "%")); + List<ConfigProperty> configResult = configQuery.getResultList(); + + if (configResult == null || configResult.isEmpty()) { + Logger.warn("Found no configuration keys with searchKey: " + searchKey); + return null; + + } + Logger.trace("Find " + configResult.size() + " key/value pairs with searchKey: " + searchKey); + + //build key/value configuration map from database entries + Map<String, String> result = getKeyValueFromDatabaseDAO( + configResult.iterator(), null, false); + return result; + } @Override - public Properties getOnlineApplication(String publicURLPrefix) + public Map<String, String> getOnlineApplication(String publicURLPrefix) throws ConfigurationException { EntityManager em = this.getPersistenceContext(); if (null == em) { @@ -54,148 +111,74 @@ public class MOAIDConfigurationImpl extends ConfigPropertyDaoImpl implements MOA } + //search key prefix for online application with this publicURLPrefix String keyId = MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES + ".%." + MOAIDConfigurationConstants.SERVICE_UNIQUEIDENTIFIER; - TypedQuery<ConfigProperty> query = em.createQuery("select * from ConfigProperty dbconfig where dbconfig.key like :key and dbconfig.value = SUBSTRING(:uniqueID, 1, LENGTH(dbconfig.value))", ConfigProperty.class); - query.setParameter("key", keyId); - query.setParameter("uniqueID", publicURLPrefix); - List<ConfigProperty> result = query.getResultList(); + + TypedQuery<ConfigProperty> oaSearchQuery = em.createQuery("select dbconfig from ConfigProperty dbconfig where dbconfig.key like :key and dbconfig.value = SUBSTRING(:uniqueID, 1, LENGTH(dbconfig.value))", ConfigProperty.class); + oaSearchQuery.setParameter("key", keyId); + oaSearchQuery.setParameter("uniqueID", publicURLPrefix); + List<ConfigProperty> oaSearchResult = oaSearchQuery.getResultList(); - if (result.size() == 0) { + if (oaSearchResult.size() == 0) { Logger.debug("No entries found."); - return null; - } + return null; } - if (result.size() > 1) { + if (oaSearchResult.size() > 1) { Logger.warn("OAIdentifier match to more then one DB-entry!"); return null; } - String oaKey = result.get(0).getKey(); - String oaValue = result.get(0).getValue(); - Logger.trace("Find online application with uniqueID: " + oaValue + " and keyID: " + oaKey); + String oaIdKey = oaSearchResult.get(0).getKey(); + String oaIdValue = oaSearchResult.get(0).getValue(); + Logger.trace("Find online application with uniqueID: " + oaIdValue + " and keyID: " + oaIdKey); + //load all online application key/value pairs from database + String oaType = KeyValueUtils.getFirstChildAfterPrefix(oaIdKey, MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES); + String oaKey = KeyValueUtils.getPrefixFromKey(oaIdKey, MOAIDConfigurationConstants.SERVICE_UNIQUEIDENTIFIER); - - return null; + TypedQuery<ConfigProperty> oaConfigQuery = em.createQuery("select dbconfig from ConfigProperty dbconfig where dbconfig.key like :key", ConfigProperty.class); + oaConfigQuery.setParameter("key", oaKey + ".%"); + List<ConfigProperty> oaConfigResult = oaConfigQuery.getResultList(); + + if (oaConfigResult == null) { + Logger.warn("Found no configuration keys with prefix: " + oaKey + ".%"); + return null; + } + Logger.trace("Find " + oaConfigResult.size() + " key/value pairs with prefix: " + oaKey + ".%"); + + //build key/value configuration map from database entries + Map<String, String> result = getKeyValueFromDatabaseDAO( + oaConfigResult.iterator(), oaKey, true); + result.put(MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES, oaType); + + return result; } + /** - * Small helper method. NOTE: may return empty properties, but never {@code null}. - * @param propPrefix the prefix of the desired property. - * @return the {@link Properties} + * Small helper method. NOTE: may return empty configuration properties, but never {@code null}. + * + * @param propPrefix: the prefix of the desired property. + * @param input: List of database objects with key/value information. + * @param removePrefix: Indicates if the prefix should be removed from the result key + * @return the {@link Map} of configuration properties */ - private Properties getGeneralProperiesConfig(Properties input, final String propPrefix) { - - Properties configProp = new Properties(); - for (Object key : input.keySet()) { - if (key.toString().startsWith(propPrefix)) { - String propertyName = key.toString().substring(propPrefix.length()); - configProp.put(propertyName, input.get(key.toString())); - } + private Map<String, String> getKeyValueFromDatabaseDAO(Iterator<ConfigProperty> input, final String prefix, boolean removePrefix) { + Map<String, String> configProp = new HashMap<String, String>(); + while (input.hasNext()) { + ConfigProperty el = input.next(); + if (removePrefix) { + if (el.getKey().startsWith(prefix)) { + String propertyName = KeyValueUtils.removePrefixFromKey(el.getKey(), prefix); + configProp.put(propertyName, el.getValue()); + + } + } else + configProp.put(el.getKey(), el.getValue()); + } return configProp; } - - -// @Override -// public List<String> getAllKeys(){ -// try { -// return Arrays.asList(this.configPropertyDao.getConfigurationIds()); -// } catch (Exception e) { -// log.debug("Error while retrieving a list of all keys in the database."); -// return null; -// } -// } -// -// @Override -// public String get(String key) { -// // return null if key does not exist -// try { -// return configPropertyDao.getStringValue(key); -// -// } catch (Exception e) { -// log.debug("Error while searching 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 { -// T property = configPropertyDao.getObjectValue(key, clazz); -// return property; -// -// } 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, String value) { -// -// try { -// //TODO: add delete -// if (value == null) { -// //configPropertyDao.delete(key); -// return true; -// } else { -// configPropertyDao.setStringValue(key, value); -// return true; -// } -// } 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>(); -// } -// } - } |