Commit 86fe2d78dcfc3f296e1c91beb7daf8861f67aa14
1 parent
de9a251e
Exists in
master
Incluindo os logs antigos no novo ConfigurationLoader
Showing
9 changed files
with
42 additions
and
29 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/configuration/ConfigurationValueExtractor.java
| @@ -42,7 +42,7 @@ import org.apache.commons.configuration.Configuration; | @@ -42,7 +42,7 @@ import org.apache.commons.configuration.Configuration; | ||
| 42 | 42 | ||
| 43 | public interface ConfigurationValueExtractor { | 43 | public interface ConfigurationValueExtractor { |
| 44 | 44 | ||
| 45 | - Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue); | 45 | + Object getValue(String prefix, String key, Field field, Configuration configuration); |
| 46 | 46 | ||
| 47 | boolean isSupported(Field field); | 47 | boolean isSupported(Field field); |
| 48 | } | 48 | } |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationArrayValueExtractor.java
| @@ -50,9 +50,8 @@ import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | @@ -50,9 +50,8 @@ import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 50 | public class ConfigurationArrayValueExtractor implements ConfigurationValueExtractor { | 50 | public class ConfigurationArrayValueExtractor implements ConfigurationValueExtractor { |
| 51 | 51 | ||
| 52 | @Override | 52 | @Override |
| 53 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | ||
| 54 | - return new DataConfiguration(configuration).getArray(field.getType().getComponentType(), prefix + key, | ||
| 55 | - defaultValue); | 53 | + public Object getValue(String prefix, String key, Field field, Configuration configuration) { |
| 54 | + return new DataConfiguration(configuration).getArray(field.getType().getComponentType(), prefix + key); | ||
| 56 | } | 55 | } |
| 57 | 56 | ||
| 58 | @Override | 57 | @Override |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationClassValueExtractor.java
| @@ -51,8 +51,8 @@ import br.gov.frameworkdemoiselle.util.Reflections; | @@ -51,8 +51,8 @@ import br.gov.frameworkdemoiselle.util.Reflections; | ||
| 51 | public class ConfigurationClassValueExtractor implements ConfigurationValueExtractor { | 51 | public class ConfigurationClassValueExtractor implements ConfigurationValueExtractor { |
| 52 | 52 | ||
| 53 | @Override | 53 | @Override |
| 54 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | ||
| 55 | - Object value = defaultValue; | 54 | + public Object getValue(String prefix, String key, Field field, Configuration configuration) { |
| 55 | + Object value = null; | ||
| 56 | String canonicalName = configuration.getString(prefix + key); | 56 | String canonicalName = configuration.getString(prefix + key); |
| 57 | 57 | ||
| 58 | if (canonicalName != null) { | 58 | if (canonicalName != null) { |
| @@ -60,7 +60,7 @@ public class ConfigurationClassValueExtractor implements ConfigurationValueExtra | @@ -60,7 +60,7 @@ public class ConfigurationClassValueExtractor implements ConfigurationValueExtra | ||
| 60 | value = Reflections.forName(canonicalName); | 60 | value = Reflections.forName(canonicalName); |
| 61 | } catch (ClassNotFoundException cause) { | 61 | } catch (ClassNotFoundException cause) { |
| 62 | // TODO Lançar a mensagem correta | 62 | // TODO Lançar a mensagem correta |
| 63 | - throw new ConfigurationException(null, cause); | 63 | + throw new ConfigurationException("", cause); |
| 64 | } | 64 | } |
| 65 | } | 65 | } |
| 66 | 66 |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationLoader.java
| @@ -64,6 +64,7 @@ import br.gov.frameworkdemoiselle.util.Beans; | @@ -64,6 +64,7 @@ import br.gov.frameworkdemoiselle.util.Beans; | ||
| 64 | import br.gov.frameworkdemoiselle.util.NameQualifier; | 64 | import br.gov.frameworkdemoiselle.util.NameQualifier; |
| 65 | import br.gov.frameworkdemoiselle.util.Reflections; | 65 | import br.gov.frameworkdemoiselle.util.Reflections; |
| 66 | import br.gov.frameworkdemoiselle.util.ResourceBundle; | 66 | import br.gov.frameworkdemoiselle.util.ResourceBundle; |
| 67 | +import br.gov.frameworkdemoiselle.util.Strings; | ||
| 67 | 68 | ||
| 68 | /** | 69 | /** |
| 69 | * This component loads a config class annotated with {@link Configuration} by filling its attributes with {@link Param} | 70 | * This component loads a config class annotated with {@link Configuration} by filling its attributes with {@link Param} |
| @@ -122,6 +123,11 @@ public class ConfigurationLoader implements Serializable { | @@ -122,6 +123,11 @@ public class ConfigurationLoader implements Serializable { | ||
| 122 | } | 123 | } |
| 123 | 124 | ||
| 124 | private void validateField(Field field) { | 125 | private void validateField(Field field) { |
| 126 | + Name annotation = field.getAnnotation(Name.class); | ||
| 127 | + | ||
| 128 | + if (annotation != null && Strings.isEmpty(annotation.value())) { | ||
| 129 | + throw new ConfigurationException(getBundle().getString("configuration-name-attribute-cant-be-empty")); | ||
| 130 | + } | ||
| 125 | } | 131 | } |
| 126 | 132 | ||
| 127 | private void loadType() { | 133 | private void loadType() { |
| @@ -147,7 +153,7 @@ public class ConfigurationLoader implements Serializable { | @@ -147,7 +153,7 @@ public class ConfigurationLoader implements Serializable { | ||
| 147 | ((FileConfiguration) config).load(); | 153 | ((FileConfiguration) config).load(); |
| 148 | 154 | ||
| 149 | } catch (org.apache.commons.configuration.ConfigurationException cause) { | 155 | } catch (org.apache.commons.configuration.ConfigurationException cause) { |
| 150 | - // TODO Logar como warning. | 156 | + getLogger().warn(getBundle().getString("file-not-found", this.resource)); |
| 151 | config = null; | 157 | config = null; |
| 152 | } | 158 | } |
| 153 | } | 159 | } |
| @@ -159,17 +165,21 @@ public class ConfigurationLoader implements Serializable { | @@ -159,17 +165,21 @@ public class ConfigurationLoader implements Serializable { | ||
| 159 | AbstractConfiguration config; | 165 | AbstractConfiguration config; |
| 160 | 166 | ||
| 161 | switch (this.type) { | 167 | switch (this.type) { |
| 162 | - case SYSTEM: | ||
| 163 | - config = new SystemConfiguration(); | 168 | + case PROPERTIES: |
| 169 | + config = new PropertiesConfiguration(); | ||
| 164 | break; | 170 | break; |
| 165 | 171 | ||
| 166 | case XML: | 172 | case XML: |
| 167 | config = new XMLConfiguration(); | 173 | config = new XMLConfiguration(); |
| 168 | break; | 174 | break; |
| 169 | 175 | ||
| 170 | - default: | ||
| 171 | - config = new PropertiesConfiguration(); | 176 | + case SYSTEM: |
| 177 | + config = new SystemConfiguration(); | ||
| 172 | break; | 178 | break; |
| 179 | + | ||
| 180 | + default: | ||
| 181 | + throw new ConfigurationException(getBundle().getString("configuration-type-not-implemented-yet", | ||
| 182 | + type.name())); | ||
| 173 | } | 183 | } |
| 174 | 184 | ||
| 175 | config.setDelimiterParsingDisabled(true); | 185 | config.setDelimiterParsingDisabled(true); |
| @@ -201,14 +211,22 @@ public class ConfigurationLoader implements Serializable { | @@ -201,14 +211,22 @@ public class ConfigurationLoader implements Serializable { | ||
| 201 | } | 211 | } |
| 202 | 212 | ||
| 203 | Object defaultValue = Reflections.getFieldValue(field, this.object); | 213 | Object defaultValue = Reflections.getFieldValue(field, this.object); |
| 204 | - Object finalValue = getValue(field, field.getType(), getKey(field), defaultValue); | 214 | + Object loadedValue = getValue(field, field.getType(), getKey(field), defaultValue); |
| 215 | + Object finalValue = (loadedValue == null ? defaultValue : loadedValue); | ||
| 216 | + | ||
| 217 | + if (loadedValue == null) { | ||
| 218 | + getLogger().debug(getBundle().getString("configuration-key-not-found", this.prefix + getKey(field))); | ||
| 219 | + } | ||
| 205 | 220 | ||
| 206 | Reflections.setFieldValue(field, this.object, finalValue); | 221 | Reflections.setFieldValue(field, this.object, finalValue); |
| 222 | + getLogger().debug( | ||
| 223 | + getBundle().getString("configuration-field-loaded", this.prefix + getKey(field), field.getName(), | ||
| 224 | + finalValue == null ? "null" : finalValue)); | ||
| 207 | } | 225 | } |
| 208 | 226 | ||
| 209 | private Object getValue(Field field, Class<?> type, String key, Object defaultValue) { | 227 | private Object getValue(Field field, Class<?> type, String key, Object defaultValue) { |
| 210 | ConfigurationValueExtractor extractor = getValueExtractor(field); | 228 | ConfigurationValueExtractor extractor = getValueExtractor(field); |
| 211 | - return extractor.getValue(this.prefix, key, field, this.configuration, defaultValue); | 229 | + return extractor.getValue(this.prefix, key, field, this.configuration); |
| 212 | } | 230 | } |
| 213 | 231 | ||
| 214 | private ConfigurationValueExtractor getValueExtractor(Field field) { | 232 | private ConfigurationValueExtractor getValueExtractor(Field field) { |
| @@ -259,8 +277,8 @@ public class ConfigurationLoader implements Serializable { | @@ -259,8 +277,8 @@ public class ConfigurationLoader implements Serializable { | ||
| 259 | 277 | ||
| 260 | private void validateValue(Field field, Object value) { | 278 | private void validateValue(Field field, Object value) { |
| 261 | if (field.isAnnotationPresent(NotNull.class) && value == null) { | 279 | if (field.isAnnotationPresent(NotNull.class) && value == null) { |
| 262 | - throw new ConfigurationException("", new NullPointerException()); | ||
| 263 | - // TODO: Pegar mensagem do Bundle e verificar como as mensagens de log estão implementadas | 280 | + throw new ConfigurationException(getBundle().getString("configuration-attribute-is-mandatory", |
| 281 | + this.prefix + getKey(field), this.resource), new NullPointerException()); | ||
| 264 | } | 282 | } |
| 265 | } | 283 | } |
| 266 | 284 |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationMapValueExtractor.java
| @@ -54,9 +54,8 @@ import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | @@ -54,9 +54,8 @@ import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 54 | public class ConfigurationMapValueExtractor implements ConfigurationValueExtractor { | 54 | public class ConfigurationMapValueExtractor implements ConfigurationValueExtractor { |
| 55 | 55 | ||
| 56 | @Override | 56 | @Override |
| 57 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | ||
| 58 | - @SuppressWarnings("unchecked") | ||
| 59 | - Map<String, Object> value = (Map<String, Object>) defaultValue; | 57 | + public Object getValue(String prefix, String key, Field field, Configuration configuration) { |
| 58 | + Map<String, Object> value = null; | ||
| 60 | 59 | ||
| 61 | String regexp = "^(" + prefix + ")((.+)\\.)?(" + key + ")$"; | 60 | String regexp = "^(" + prefix + ")((.+)\\.)?(" + key + ")$"; |
| 62 | Pattern pattern = Pattern.compile(regexp); | 61 | Pattern pattern = Pattern.compile(regexp); |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationPrimitiveOrWrapperValueExtractor.java
| @@ -69,14 +69,14 @@ public class ConfigurationPrimitiveOrWrapperValueExtractor implements Configurat | @@ -69,14 +69,14 @@ public class ConfigurationPrimitiveOrWrapperValueExtractor implements Configurat | ||
| 69 | 69 | ||
| 70 | @Override | 70 | @Override |
| 71 | @SuppressWarnings("unchecked") | 71 | @SuppressWarnings("unchecked") |
| 72 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | 72 | + public Object getValue(String prefix, String key, Field field, Configuration configuration) { |
| 73 | Object value; | 73 | Object value; |
| 74 | 74 | ||
| 75 | try { | 75 | try { |
| 76 | - value = new DataConfiguration(configuration).get(ClassUtils.primitiveToWrapper(field.getType()), prefix + key, defaultValue); | 76 | + value = new DataConfiguration(configuration).get(ClassUtils.primitiveToWrapper(field.getType()), prefix + key); |
| 77 | 77 | ||
| 78 | } catch (ConversionException cause) { | 78 | } catch (ConversionException cause) { |
| 79 | - value = defaultValue; | 79 | + value = null; |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | return value; | 82 | return value; |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationStringValueExtractor.java
| @@ -49,8 +49,8 @@ import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | @@ -49,8 +49,8 @@ import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 49 | public class ConfigurationStringValueExtractor implements ConfigurationValueExtractor { | 49 | public class ConfigurationStringValueExtractor implements ConfigurationValueExtractor { |
| 50 | 50 | ||
| 51 | @Override | 51 | @Override |
| 52 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | ||
| 53 | - return configuration.getString(prefix + key, (String) defaultValue); | 52 | + public Object getValue(String prefix, String key, Field field, Configuration configuration) { |
| 53 | + return configuration.getString(prefix + key); | ||
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | @Override | 56 | @Override |
impl/core/src/main/resources/demoiselle-core-bundle.properties
| @@ -40,7 +40,6 @@ ambiguous-strategy-resolution=Foi detectada ambiguidade da interface {0} com as | @@ -40,7 +40,6 @@ ambiguous-strategy-resolution=Foi detectada ambiguidade da interface {0} com as | ||
| 40 | bean-not-found=Voc\u00EA est\u00E1 tentando obter um objeto n\u00E3o reconhecido pelo CDI via Beans.getReference({0}) | 40 | bean-not-found=Voc\u00EA est\u00E1 tentando obter um objeto n\u00E3o reconhecido pelo CDI via Beans.getReference({0}) |
| 41 | more-than-one-exceptionhandler-defined-for-same-class=Foi definido mais de um m\u00E9todo na classe {0} para tratar a exce\u00E7\u00E3o {1} | 41 | more-than-one-exceptionhandler-defined-for-same-class=Foi definido mais de um m\u00E9todo na classe {0} para tratar a exce\u00E7\u00E3o {1} |
| 42 | handling-exception=Tratando a exce\u00E7\u00E3o {0} | 42 | handling-exception=Tratando a exce\u00E7\u00E3o {0} |
| 43 | -ambiguous-key=Existe mais de uma chave declarada no arquivo de configura\u00E7\u00E3o para o campo {0} da classe {1}. Indique qual a chave correta utilizando a anota\u00E7\u00E3o Name | ||
| 44 | taking-off=O Demoiselle ${project.version} decolou | 43 | taking-off=O Demoiselle ${project.version} decolou |
| 45 | engine-off=Desligando os motores do Demoiselle ${project.version} | 44 | engine-off=Desligando os motores do Demoiselle ${project.version} |
| 46 | setting-up-bean-manager=BeanManager dispon\u00EDvel atrav\u00E9s do utilit\u00E1rio {0} | 45 | setting-up-bean-manager=BeanManager dispon\u00EDvel atrav\u00E9s do utilit\u00E1rio {0} |
| @@ -59,16 +58,14 @@ loading-configuration-class=Carregando a classe de configura\u00E7\u00E3o {0} | @@ -59,16 +58,14 @@ loading-configuration-class=Carregando a classe de configura\u00E7\u00E3o {0} | ||
| 59 | configuration-field-loaded=Configura\u00E7\u00E3o {0} atribu\u00EDda a {1} com o valor {2} | 58 | configuration-field-loaded=Configura\u00E7\u00E3o {0} atribu\u00EDda a {1} com o valor {2} |
| 60 | configuration-attribute-is-mandatory=A configura\u00E7\u00E3o {0} \u00E9 obrigat\u00F3ria, mas n\u00E3o foi encontrada em {1} | 59 | configuration-attribute-is-mandatory=A configura\u00E7\u00E3o {0} \u00E9 obrigat\u00F3ria, mas n\u00E3o foi encontrada em {1} |
| 61 | configuration-name-attribute-cant-be-empty=A nota\u00E7\u00E3o Name n\u00E3o pode estar em branco | 60 | configuration-name-attribute-cant-be-empty=A nota\u00E7\u00E3o Name n\u00E3o pode estar em branco |
| 62 | -configuration-key-not-found=Chave de configura\u00E7\u00E3o {0} n\u00E3o encontrada. Conven\u00E7\u00F5es verificadas\: {1} | 61 | +configuration-key-not-found=Chave de configura\u00E7\u00E3o {0} n\u00E3o encontrada |
| 63 | 62 | ||
| 64 | transaction-not-defined=Nenhuma transa\u00E7\u00E3o foi definida. Para utilizar @{0} \u00E9 preciso definir a propriedade frameworkdemoiselle.transaction.class com a estrat\u00E9gia de transa\u00E7\u00E3o desejada no arquivo demoiselle.properties | 63 | transaction-not-defined=Nenhuma transa\u00E7\u00E3o foi definida. Para utilizar @{0} \u00E9 preciso definir a propriedade frameworkdemoiselle.transaction.class com a estrat\u00E9gia de transa\u00E7\u00E3o desejada no arquivo demoiselle.properties |
| 65 | executing-all=Executando todos os \: {0} | 64 | executing-all=Executando todos os \: {0} |
| 66 | custom-context-was-registered=O contexto {0} foi registrado | 65 | custom-context-was-registered=O contexto {0} foi registrado |
| 67 | custom-context-was-unregistered=O contexto {0} foi removido | 66 | custom-context-was-unregistered=O contexto {0} foi removido |
| 68 | 67 | ||
| 69 | -error-creating-configuration-from-resource=Error creating configuration from resource named "{0}" | ||
| 70 | configuration-type-not-implemented-yet=Configuration type "{0}" is not implemented yet | 68 | configuration-type-not-implemented-yet=Configuration type "{0}" is not implemented yet |
| 71 | -error-converting-to-type=Error converting to type "{0}" | ||
| 72 | error-creating-new-instance-for=Error creating a new instance for "{0}" | 69 | error-creating-new-instance-for=Error creating a new instance for "{0}" |
| 73 | executed-successfully={0} execultado com sucesso | 70 | executed-successfully={0} execultado com sucesso |
| 74 | must-declare-one-single-parameter=Voc\u00EA deve declarar um par\u00E2metro \u00FAnico em {0} | 71 | must-declare-one-single-parameter=Voc\u00EA deve declarar um par\u00E2metro \u00FAnico em {0} |
impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/custom/MyValueExtractor.java
| @@ -9,7 +9,7 @@ import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | @@ -9,7 +9,7 @@ import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 9 | public class MyValueExtractor implements ConfigurationValueExtractor { | 9 | public class MyValueExtractor implements ConfigurationValueExtractor { |
| 10 | 10 | ||
| 11 | @Override | 11 | @Override |
| 12 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | 12 | + public Object getValue(String prefix, String key, Field field, Configuration configuration) { |
| 13 | return new MappedClass(); | 13 | return new MappedClass(); |
| 14 | } | 14 | } |
| 15 | 15 |