From 6b7e32cb4b062c23633566e7eca5dfacd29cde6a Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Wed, 6 Feb 2013 14:40:17 -0300 Subject: [PATCH] Suporte ao carregamento de Map nas classes de configuração --- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java | 219 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------- 1 file changed, 113 insertions(+), 106 deletions(-) diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java index 21fcd55..5a1cb87 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java @@ -43,10 +43,12 @@ import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.URL; -import java.util.HashSet; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import java.util.Properties; -import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.validation.constraints.NotNull; @@ -114,7 +116,7 @@ public class ConfigurationLoader implements Serializable { org.apache.commons.configuration.Configuration config = getConfiguration(resource, type); if (config != null) { - String key = getKey(field, clazz, config); + Key key = new Key(field, clazz, config); Object value = getValue(key, field, config); validate(field, key, value, resource); @@ -123,106 +125,21 @@ public class ConfigurationLoader implements Serializable { } } - private void setValue(Field field, String key, Object object, Object value) { + private void setValue(Field field, Key key, Object object, Object value) { if (value != null) { Reflections.setFieldValue(field, object, value); - getLogger().debug(getBundle().getString("configuration-field-loaded", key, field.getName(), value)); + getLogger().debug( + getBundle().getString("configuration-field-loaded", key.toString(), field.getName(), value)); } } - private void validate(Field field, String key, Object value, String resource) { + private void validate(Field field, Key key, Object value, String resource) { if (field.isAnnotationPresent(NotNull.class) && value == null) { - throw new ConfigurationException(getBundle().getString("configuration-attribute-is-mandatory", key, - resource)); + throw new ConfigurationException(getBundle().getString("configuration-attribute-is-mandatory", + key.toString(), resource)); } } - private String getKey(final Field field, final Class clazz, - final org.apache.commons.configuration.Configuration config) { - - final String prefix = getPrefix(field, clazz); - final StringBuffer key = new StringBuffer(); - - key.append(prefix); - - if (field.isAnnotationPresent(Name.class)) { - key.append(getKeyByAnnotation(field)); - } else { - key.append(getKeyByConvention(field, prefix, config)); - } - - return key.toString(); - } - - private String getPrefix(Field field, Class type) { - String prefix = ""; - - Configuration classAnnotation = type.getAnnotation(Configuration.class); - if (!Strings.isEmpty(classAnnotation.prefix())) { - - prefix = classAnnotation.prefix(); - - if (prefix.charAt(prefix.length() - 1) != '.') { - getLogger().warn( - "ATENÇÃO!!! Informe o ponto (.) ao final da declaração do atributo prefix = \"" + prefix - + "\" da anotação @Configuration da classe " + type.getCanonicalName() - + " para evitar incompatibilidade com as próximas versões do Demoiselle."); - - prefix += "."; - } - } - - return prefix; - } - - private String getKeyByAnnotation(Field field) { - String key = null; - - Name nameAnnotation = field.getAnnotation(Name.class); - if (Strings.isEmpty(nameAnnotation.value())) { - throw new ConfigurationException(getBundle().getString("configuration-name-attribute-cant-be-empty")); - } else { - key = nameAnnotation.value(); - } - - return key; - } - - private String getKeyByConvention(Field field, String prefix, org.apache.commons.configuration.Configuration config) { - - Set conventions = new HashSet(); - conventions.add(field.getName()); - conventions.add(Strings.camelCaseToSymbolSeparated(field.getName(), ".")); - conventions.add(Strings.camelCaseToSymbolSeparated(field.getName(), "_")); - conventions.add(field.getName().toLowerCase()); - conventions.add(field.getName().toUpperCase()); - - int matches = 0; - String key = field.getName(); - for (String convention : conventions) { - if (config.containsKey(prefix + convention)) { - key = convention; - matches++; - } - } - - if (!field.getName().equals(key)) { - getLogger().warn( - "ATENÇÃO!!! Anote o atributo " + field.getName() + " da classe " - + field.getDeclaringClass().getCanonicalName() + " com @Name(\"" + key - + "\") para evitar incompatibilidade com as próximas versões do Demoiselle."); - } - - if (matches == 0) { - getLogger().debug(getBundle().getString("configuration-key-not-found", key, conventions)); - } else if (matches > 1) { - throw new ConfigurationException(getBundle().getString("ambiguous-key", field.getName(), - field.getDeclaringClass())); - } - - return key; - } - /** * Returns the configuration class according to specified resource name and configuration type. * @@ -273,7 +190,7 @@ public class ConfigurationLoader implements Serializable { } @SuppressWarnings("unchecked") - private T getValue(String key, Field field, org.apache.commons.configuration.Configuration config) { + private T getValue(Key key, Field field, org.apache.commons.configuration.Configuration config) { Object value; Class fieldClass = (Class) field.getType(); @@ -281,6 +198,9 @@ public class ConfigurationLoader implements Serializable { if (fieldClass.isArray()) { value = getArray(key, field, config); + } else if (fieldClass.equals(Map.class)) { + value = getMap(key, field, config); + } else if (fieldClass.equals(Properties.class)) { value = getProperty(key, config); @@ -294,7 +214,37 @@ public class ConfigurationLoader implements Serializable { return (T) value; } - private Object getArray(String key, Field field, org.apache.commons.configuration.Configuration config) { + private Object getMap(Key key, Field field, org.apache.commons.configuration.Configuration config) { + Map value = null; + + Pattern pattern = Pattern.compile("^(" + key.getPrefix() + ")(.+)\\.(" + key.getName() + ")$"); + Matcher matcher; + + String iterKey; + String mapKey; + String confKey; + + for (@SuppressWarnings("unchecked") + Iterator iter = config.getKeys(); iter.hasNext();) { + iterKey = iter.next(); + matcher = pattern.matcher(iterKey); + + if (matcher.matches()) { + mapKey = matcher.group(2); + confKey = matcher.group(1) + matcher.group(2) + "." + matcher.group(3); + + if (value == null) { + value = new HashMap(); + } + + value.put(mapKey, config.getProperty(confKey)); + } + } + + return value; + } + + private Object getArray(Key key, Field field, org.apache.commons.configuration.Configuration config) { Object value = null; Class fieldClass = (Class) field.getType(); @@ -309,7 +259,7 @@ public class ConfigurationLoader implements Serializable { methodName += "Array"; method = config.getClass().getMethod(methodName, String.class); - value = method.invoke(config, key); + value = method.invoke(config, key.toString()); } catch (Throwable cause) { throw new ConfigurationException(getBundle().getString("error-converting-to-type", fieldClass.getName()), @@ -319,7 +269,7 @@ public class ConfigurationLoader implements Serializable { return value; } - private Object getBasic(String key, Field field, org.apache.commons.configuration.Configuration config) { + private Object getBasic(Key key, Field field, org.apache.commons.configuration.Configuration config) { Object value = null; Class fieldClass = (Class) field.getType(); @@ -333,11 +283,11 @@ public class ConfigurationLoader implements Serializable { if (!fieldClass.isPrimitive()) { method = config.getClass().getMethod(methodName, String.class, fieldClass); - value = method.invoke(config, key, null); + value = method.invoke(config, key.toString(), null); - } else if (config.containsKey(key)) { + } else if (config.containsKey(key.toString())) { method = config.getClass().getMethod(methodName, String.class); - value = method.invoke(config, key); + value = method.invoke(config, key.toString()); } } catch (Throwable cause) { @@ -348,11 +298,11 @@ public class ConfigurationLoader implements Serializable { return value; } - private Object getClass(String key, Field field, org.apache.commons.configuration.Configuration config) { + private Object getClass(Key key, Field field, org.apache.commons.configuration.Configuration config) { Object value = null; try { - String canonicalName = config.getString(key); + String canonicalName = config.getString(key.toString()); if (canonicalName != null) { ClassLoader classLoader = getClassLoaderForClass(canonicalName); @@ -395,17 +345,17 @@ public class ConfigurationLoader implements Serializable { return ""; } - private Object getProperty(String key, org.apache.commons.configuration.Configuration config) { + private Object getProperty(Key key, org.apache.commons.configuration.Configuration config) { Object value = null; @SuppressWarnings("unchecked") - Iterator iterator = config.getKeys(key); + Iterator iterator = config.getKeys(key.toString()); if (iterator.hasNext()) { Properties props = new Properties(); while (iterator.hasNext()) { String fullKey = iterator.next(); - String prefix = key + "."; + String prefix = key.toString() + "."; String unprefixedKey = fullKey.substring(prefix.length()); props.put(unprefixedKey, config.getString(fullKey)); } @@ -470,4 +420,61 @@ public class ConfigurationLoader implements Serializable { return bootstrap; } + + private class Key { + + private String prefix; + + private String name; + + private String key; + + private Key(final Field field, final Class type, final org.apache.commons.configuration.Configuration config) { + + this.prefix = type.getAnnotation(Configuration.class).prefix(); + if (this.prefix == null) { + this.prefix = ""; + } else { + this.prefix += "."; + } + + if (field.isAnnotationPresent(Name.class)) { + this.name = getNameByAnnotation(field); + } else { + this.name = getNameByField(field); + } + + this.key = this.prefix + this.name; + } + + private String getNameByAnnotation(Field field) { + String key = null; + + Name nameAnnotation = field.getAnnotation(Name.class); + if (Strings.isEmpty(nameAnnotation.value())) { + throw new ConfigurationException(getBundle().getString("configuration-name-attribute-cant-be-empty")); + } else { + key = nameAnnotation.value(); + } + + return key; + } + + private String getNameByField(Field field) { + return field.getName(); + } + + public String getPrefix() { + return prefix; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return this.key; + } + } } -- libgit2 0.21.2