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 | 42 | |
| 43 | 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 | 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 | 50 | public class ConfigurationArrayValueExtractor implements ConfigurationValueExtractor { |
| 51 | 51 | |
| 52 | 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 | 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 | 51 | public class ConfigurationClassValueExtractor implements ConfigurationValueExtractor { |
| 52 | 52 | |
| 53 | 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 | 56 | String canonicalName = configuration.getString(prefix + key); |
| 57 | 57 | |
| 58 | 58 | if (canonicalName != null) { |
| ... | ... | @@ -60,7 +60,7 @@ public class ConfigurationClassValueExtractor implements ConfigurationValueExtra |
| 60 | 60 | value = Reflections.forName(canonicalName); |
| 61 | 61 | } catch (ClassNotFoundException cause) { |
| 62 | 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 | 64 | import br.gov.frameworkdemoiselle.util.NameQualifier; |
| 65 | 65 | import br.gov.frameworkdemoiselle.util.Reflections; |
| 66 | 66 | import br.gov.frameworkdemoiselle.util.ResourceBundle; |
| 67 | +import br.gov.frameworkdemoiselle.util.Strings; | |
| 67 | 68 | |
| 68 | 69 | /** |
| 69 | 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 | 123 | } |
| 123 | 124 | |
| 124 | 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 | 133 | private void loadType() { |
| ... | ... | @@ -147,7 +153,7 @@ public class ConfigurationLoader implements Serializable { |
| 147 | 153 | ((FileConfiguration) config).load(); |
| 148 | 154 | |
| 149 | 155 | } catch (org.apache.commons.configuration.ConfigurationException cause) { |
| 150 | - // TODO Logar como warning. | |
| 156 | + getLogger().warn(getBundle().getString("file-not-found", this.resource)); | |
| 151 | 157 | config = null; |
| 152 | 158 | } |
| 153 | 159 | } |
| ... | ... | @@ -159,17 +165,21 @@ public class ConfigurationLoader implements Serializable { |
| 159 | 165 | AbstractConfiguration config; |
| 160 | 166 | |
| 161 | 167 | switch (this.type) { |
| 162 | - case SYSTEM: | |
| 163 | - config = new SystemConfiguration(); | |
| 168 | + case PROPERTIES: | |
| 169 | + config = new PropertiesConfiguration(); | |
| 164 | 170 | break; |
| 165 | 171 | |
| 166 | 172 | case XML: |
| 167 | 173 | config = new XMLConfiguration(); |
| 168 | 174 | break; |
| 169 | 175 | |
| 170 | - default: | |
| 171 | - config = new PropertiesConfiguration(); | |
| 176 | + case SYSTEM: | |
| 177 | + config = new SystemConfiguration(); | |
| 172 | 178 | break; |
| 179 | + | |
| 180 | + default: | |
| 181 | + throw new ConfigurationException(getBundle().getString("configuration-type-not-implemented-yet", | |
| 182 | + type.name())); | |
| 173 | 183 | } |
| 174 | 184 | |
| 175 | 185 | config.setDelimiterParsingDisabled(true); |
| ... | ... | @@ -201,14 +211,22 @@ public class ConfigurationLoader implements Serializable { |
| 201 | 211 | } |
| 202 | 212 | |
| 203 | 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 | 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 | 227 | private Object getValue(Field field, Class<?> type, String key, Object defaultValue) { |
| 210 | 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 | 232 | private ConfigurationValueExtractor getValueExtractor(Field field) { |
| ... | ... | @@ -259,8 +277,8 @@ public class ConfigurationLoader implements Serializable { |
| 259 | 277 | |
| 260 | 278 | private void validateValue(Field field, Object value) { |
| 261 | 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 | 54 | public class ConfigurationMapValueExtractor implements ConfigurationValueExtractor { |
| 55 | 55 | |
| 56 | 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 | 60 | String regexp = "^(" + prefix + ")((.+)\\.)?(" + key + ")$"; |
| 62 | 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 | 69 | |
| 70 | 70 | @Override |
| 71 | 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 | 73 | Object value; |
| 74 | 74 | |
| 75 | 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 | 78 | } catch (ConversionException cause) { |
| 79 | - value = defaultValue; | |
| 79 | + value = null; | |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 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 | 49 | public class ConfigurationStringValueExtractor implements ConfigurationValueExtractor { |
| 50 | 50 | |
| 51 | 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 | 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 | 40 | bean-not-found=Voc\u00EA est\u00E1 tentando obter um objeto n\u00E3o reconhecido pelo CDI via Beans.getReference({0}) |
| 41 | 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 | 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 | 43 | taking-off=O Demoiselle ${project.version} decolou |
| 45 | 44 | engine-off=Desligando os motores do Demoiselle ${project.version} |
| 46 | 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 | 58 | configuration-field-loaded=Configura\u00E7\u00E3o {0} atribu\u00EDda a {1} com o valor {2} |
| 60 | 59 | configuration-attribute-is-mandatory=A configura\u00E7\u00E3o {0} \u00E9 obrigat\u00F3ria, mas n\u00E3o foi encontrada em {1} |
| 61 | 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 | 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 | 64 | executing-all=Executando todos os \: {0} |
| 66 | 65 | custom-context-was-registered=O contexto {0} foi registrado |
| 67 | 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 | 68 | configuration-type-not-implemented-yet=Configuration type "{0}" is not implemented yet |
| 71 | -error-converting-to-type=Error converting to type "{0}" | |
| 72 | 69 | error-creating-new-instance-for=Error creating a new instance for "{0}" |
| 73 | 70 | executed-successfully={0} execultado com sucesso |
| 74 | 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 | 9 | public class MyValueExtractor implements ConfigurationValueExtractor { |
| 10 | 10 | |
| 11 | 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 | 13 | return new MappedClass(); |
| 14 | 14 | } |
| 15 | 15 | ... | ... |