Commit f0bc674e8ebc767fcab08c7a1924dec451e9a11e
1 parent
8937b13e
Exists in
master
Tratando ambiguidade dos extratores de configuração
Showing
7 changed files
with
65 additions
and
23 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationArrayValueExtractor.java
@@ -36,17 +36,20 @@ | @@ -36,17 +36,20 @@ | ||
36 | */ | 36 | */ |
37 | package br.gov.frameworkdemoiselle.internal.configuration; | 37 | package br.gov.frameworkdemoiselle.internal.configuration; |
38 | 38 | ||
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | ||
40 | + | ||
39 | import java.lang.reflect.Field; | 41 | import java.lang.reflect.Field; |
40 | 42 | ||
41 | import org.apache.commons.configuration.DataConfiguration; | 43 | import org.apache.commons.configuration.DataConfiguration; |
42 | 44 | ||
45 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
43 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | 46 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; |
44 | 47 | ||
48 | +@Priority(EXTENSIONS_L1_PRIORITY) | ||
45 | public class ConfigurationArrayValueExtractor implements ConfigurationValueExtractor { | 49 | public class ConfigurationArrayValueExtractor implements ConfigurationValueExtractor { |
46 | 50 | ||
47 | @Override | 51 | @Override |
48 | - public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
49 | - Object defaultValue) { | 52 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, Object defaultValue) { |
50 | return configuration.getArray(field.getType().getComponentType(), prefix + key, defaultValue); | 53 | return configuration.getArray(field.getType().getComponentType(), prefix + key, defaultValue); |
51 | } | 54 | } |
52 | 55 |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationClassValueExtractor.java
@@ -36,19 +36,22 @@ | @@ -36,19 +36,22 @@ | ||
36 | */ | 36 | */ |
37 | package br.gov.frameworkdemoiselle.internal.configuration; | 37 | package br.gov.frameworkdemoiselle.internal.configuration; |
38 | 38 | ||
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | ||
40 | + | ||
39 | import java.lang.reflect.Field; | 41 | import java.lang.reflect.Field; |
40 | 42 | ||
41 | import org.apache.commons.configuration.DataConfiguration; | 43 | import org.apache.commons.configuration.DataConfiguration; |
42 | 44 | ||
45 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
43 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | 46 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; |
44 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | 47 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; |
45 | import br.gov.frameworkdemoiselle.util.Reflections; | 48 | import br.gov.frameworkdemoiselle.util.Reflections; |
46 | 49 | ||
50 | +@Priority(EXTENSIONS_L1_PRIORITY) | ||
47 | public class ConfigurationClassValueExtractor implements ConfigurationValueExtractor { | 51 | public class ConfigurationClassValueExtractor implements ConfigurationValueExtractor { |
48 | 52 | ||
49 | @Override | 53 | @Override |
50 | - public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
51 | - Object defaultValue) { | 54 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, Object defaultValue) { |
52 | Object value = defaultValue; | 55 | Object value = defaultValue; |
53 | String canonicalName = configuration.getString(prefix + key); | 56 | String canonicalName = configuration.getString(prefix + key); |
54 | 57 |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
@@ -41,8 +41,9 @@ import static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM; | @@ -41,8 +41,9 @@ import static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM; | ||
41 | 41 | ||
42 | import java.io.Serializable; | 42 | import java.io.Serializable; |
43 | import java.lang.reflect.Field; | 43 | import java.lang.reflect.Field; |
44 | -import java.util.ArrayList; | ||
45 | -import java.util.List; | 44 | +import java.util.Collection; |
45 | +import java.util.HashSet; | ||
46 | +import java.util.Set; | ||
46 | 47 | ||
47 | import javax.inject.Inject; | 48 | import javax.inject.Inject; |
48 | import javax.validation.constraints.NotNull; | 49 | import javax.validation.constraints.NotNull; |
@@ -61,6 +62,7 @@ import br.gov.frameworkdemoiselle.configuration.Configuration; | @@ -61,6 +62,7 @@ import br.gov.frameworkdemoiselle.configuration.Configuration; | ||
61 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | 62 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; |
62 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | 63 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; |
63 | import br.gov.frameworkdemoiselle.internal.bootstrap.ConfigurationBootstrap; | 64 | import br.gov.frameworkdemoiselle.internal.bootstrap.ConfigurationBootstrap; |
65 | +import br.gov.frameworkdemoiselle.internal.implementation.StrategySelector; | ||
64 | import br.gov.frameworkdemoiselle.util.Beans; | 66 | import br.gov.frameworkdemoiselle.util.Beans; |
65 | import br.gov.frameworkdemoiselle.util.Reflections; | 67 | import br.gov.frameworkdemoiselle.util.Reflections; |
66 | 68 | ||
@@ -84,9 +86,9 @@ public class ConfigurationLoader implements Serializable { | @@ -84,9 +86,9 @@ public class ConfigurationLoader implements Serializable { | ||
84 | 86 | ||
85 | private DataConfiguration configuration; | 87 | private DataConfiguration configuration; |
86 | 88 | ||
87 | - private List<Field> fields; | 89 | + private Set<Field> fields; |
88 | 90 | ||
89 | - private List<ConfigurationValueExtractor> extractors; | 91 | + private Set<ConfigurationValueExtractor> extractors; |
90 | 92 | ||
91 | @Inject | 93 | @Inject |
92 | private ConfigurationBootstrap bootstrap; | 94 | private ConfigurationBootstrap bootstrap; |
@@ -165,7 +167,7 @@ public class ConfigurationLoader implements Serializable { | @@ -165,7 +167,7 @@ public class ConfigurationLoader implements Serializable { | ||
165 | } | 167 | } |
166 | 168 | ||
167 | private void loadExtractors() { | 169 | private void loadExtractors() { |
168 | - this.extractors = new ArrayList<ConfigurationValueExtractor>(); | 170 | + this.extractors = new HashSet<ConfigurationValueExtractor>(); |
169 | 171 | ||
170 | for (Class<? extends ConfigurationValueExtractor> extractorClass : this.bootstrap.getCache()) { | 172 | for (Class<? extends ConfigurationValueExtractor> extractorClass : this.bootstrap.getCache()) { |
171 | this.extractors.add(Beans.getReference(extractorClass)); | 173 | this.extractors.add(Beans.getReference(extractorClass)); |
@@ -191,9 +193,9 @@ public class ConfigurationLoader implements Serializable { | @@ -191,9 +193,9 @@ public class ConfigurationLoader implements Serializable { | ||
191 | } | 193 | } |
192 | } | 194 | } |
193 | 195 | ||
194 | - private List<Field> getFields() { | 196 | + private Set<Field> getFields() { |
195 | if (this.fields == null) { | 197 | if (this.fields == null) { |
196 | - this.fields = Reflections.getNonStaticFields(this.object.getClass()); | 198 | + this.fields = new HashSet<Field>(Reflections.getNonStaticFields(this.object.getClass())); |
197 | } | 199 | } |
198 | 200 | ||
199 | return this.fields; | 201 | return this.fields; |
@@ -211,16 +213,23 @@ public class ConfigurationLoader implements Serializable { | @@ -211,16 +213,23 @@ public class ConfigurationLoader implements Serializable { | ||
211 | } | 213 | } |
212 | 214 | ||
213 | private Object getValue(Field field, Class<?> type, String key, Object defaultValue) { | 215 | private Object getValue(Field field, Class<?> type, String key, Object defaultValue) { |
214 | - Object value = null; | 216 | + Collection<ConfigurationValueExtractor> candidates = new HashSet<ConfigurationValueExtractor>(); |
215 | 217 | ||
216 | for (ConfigurationValueExtractor extractor : this.extractors) { | 218 | for (ConfigurationValueExtractor extractor : this.extractors) { |
217 | if (extractor.isSupported(field)) { | 219 | if (extractor.isSupported(field)) { |
218 | - value = extractor.getValue(this.prefix, key, field, configuration, defaultValue); | ||
219 | - break; | 220 | + candidates.add(extractor); |
220 | } | 221 | } |
221 | } | 222 | } |
222 | 223 | ||
223 | - return value; | 224 | + ConfigurationValueExtractor elected = StrategySelector.getInstance(ConfigurationValueExtractor.class, |
225 | + candidates); | ||
226 | + | ||
227 | + if (elected == null) { | ||
228 | + // TODO lançar exceção informando que nenhum extrator foi encontrado para o field e ensinar como implementar | ||
229 | + // um extrator personalizado. | ||
230 | + } | ||
231 | + | ||
232 | + return elected.getValue(this.prefix, key, field, configuration, defaultValue); | ||
224 | } | 233 | } |
225 | 234 | ||
226 | private String getKey(Field field) { | 235 | private String getKey(Field field) { |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationMapValueExtractor.java
@@ -36,6 +36,8 @@ | @@ -36,6 +36,8 @@ | ||
36 | */ | 36 | */ |
37 | package br.gov.frameworkdemoiselle.internal.configuration; | 37 | package br.gov.frameworkdemoiselle.internal.configuration; |
38 | 38 | ||
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | ||
40 | + | ||
39 | import java.lang.reflect.Field; | 41 | import java.lang.reflect.Field; |
40 | import java.util.HashMap; | 42 | import java.util.HashMap; |
41 | import java.util.Iterator; | 43 | import java.util.Iterator; |
@@ -45,13 +47,14 @@ import java.util.regex.Pattern; | @@ -45,13 +47,14 @@ import java.util.regex.Pattern; | ||
45 | 47 | ||
46 | import org.apache.commons.configuration.DataConfiguration; | 48 | import org.apache.commons.configuration.DataConfiguration; |
47 | 49 | ||
50 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
48 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | 51 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; |
49 | 52 | ||
53 | +@Priority(EXTENSIONS_L1_PRIORITY) | ||
50 | public class ConfigurationMapValueExtractor implements ConfigurationValueExtractor { | 54 | public class ConfigurationMapValueExtractor implements ConfigurationValueExtractor { |
51 | 55 | ||
52 | @Override | 56 | @Override |
53 | - public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
54 | - Object defaultValue) { | 57 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, Object defaultValue) { |
55 | @SuppressWarnings("unchecked") | 58 | @SuppressWarnings("unchecked") |
56 | Map<String, Object> value = (Map<String, Object>) defaultValue; | 59 | Map<String, Object> value = (Map<String, Object>) defaultValue; |
57 | 60 |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationPrimitiveOrWrapperValueExtractor.java
@@ -36,6 +36,8 @@ | @@ -36,6 +36,8 @@ | ||
36 | */ | 36 | */ |
37 | package br.gov.frameworkdemoiselle.internal.configuration; | 37 | package br.gov.frameworkdemoiselle.internal.configuration; |
38 | 38 | ||
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | ||
40 | + | ||
39 | import java.lang.reflect.Field; | 41 | import java.lang.reflect.Field; |
40 | import java.util.HashSet; | 42 | import java.util.HashSet; |
41 | import java.util.Set; | 43 | import java.util.Set; |
@@ -44,8 +46,10 @@ import org.apache.commons.configuration.ConversionException; | @@ -44,8 +46,10 @@ import org.apache.commons.configuration.ConversionException; | ||
44 | import org.apache.commons.configuration.DataConfiguration; | 46 | import org.apache.commons.configuration.DataConfiguration; |
45 | import org.apache.commons.lang.ClassUtils; | 47 | import org.apache.commons.lang.ClassUtils; |
46 | 48 | ||
49 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
47 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | 50 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; |
48 | 51 | ||
52 | +@Priority(EXTENSIONS_L1_PRIORITY) | ||
49 | public class ConfigurationPrimitiveOrWrapperValueExtractor implements ConfigurationValueExtractor { | 53 | public class ConfigurationPrimitiveOrWrapperValueExtractor implements ConfigurationValueExtractor { |
50 | 54 | ||
51 | private static final Set<Object> wrappers = new HashSet<Object>(); | 55 | private static final Set<Object> wrappers = new HashSet<Object>(); |
@@ -64,8 +68,7 @@ public class ConfigurationPrimitiveOrWrapperValueExtractor implements Configurat | @@ -64,8 +68,7 @@ public class ConfigurationPrimitiveOrWrapperValueExtractor implements Configurat | ||
64 | 68 | ||
65 | @Override | 69 | @Override |
66 | @SuppressWarnings("unchecked") | 70 | @SuppressWarnings("unchecked") |
67 | - public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
68 | - Object defaultValue) { | 71 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, Object defaultValue) { |
69 | Object value; | 72 | Object value; |
70 | 73 | ||
71 | try { | 74 | try { |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationStringValueExtractor.java
@@ -36,17 +36,20 @@ | @@ -36,17 +36,20 @@ | ||
36 | */ | 36 | */ |
37 | package br.gov.frameworkdemoiselle.internal.configuration; | 37 | package br.gov.frameworkdemoiselle.internal.configuration; |
38 | 38 | ||
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | ||
40 | + | ||
39 | import java.lang.reflect.Field; | 41 | import java.lang.reflect.Field; |
40 | 42 | ||
41 | import org.apache.commons.configuration.DataConfiguration; | 43 | import org.apache.commons.configuration.DataConfiguration; |
42 | 44 | ||
45 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
43 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | 46 | import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; |
44 | 47 | ||
48 | +@Priority(EXTENSIONS_L1_PRIORITY) | ||
45 | public class ConfigurationStringValueExtractor implements ConfigurationValueExtractor { | 49 | public class ConfigurationStringValueExtractor implements ConfigurationValueExtractor { |
46 | 50 | ||
47 | @Override | 51 | @Override |
48 | - public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
49 | - Object defaultValue) { | 52 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, Object defaultValue) { |
50 | return configuration.getString(prefix + key, (String) defaultValue); | 53 | return configuration.getString(prefix + key, (String) defaultValue); |
51 | } | 54 | } |
52 | 55 |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/StrategySelector.java
@@ -40,7 +40,10 @@ import static br.gov.frameworkdemoiselle.annotation.Priority.MIN_PRIORITY; | @@ -40,7 +40,10 @@ import static br.gov.frameworkdemoiselle.annotation.Priority.MIN_PRIORITY; | ||
40 | 40 | ||
41 | import java.io.Serializable; | 41 | import java.io.Serializable; |
42 | import java.util.ArrayList; | 42 | import java.util.ArrayList; |
43 | +import java.util.Collection; | ||
44 | +import java.util.HashMap; | ||
43 | import java.util.List; | 45 | import java.util.List; |
46 | +import java.util.Map; | ||
44 | 47 | ||
45 | import br.gov.frameworkdemoiselle.annotation.Priority; | 48 | import br.gov.frameworkdemoiselle.annotation.Priority; |
46 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | 49 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; |
@@ -72,7 +75,22 @@ public final class StrategySelector implements Serializable { | @@ -72,7 +75,22 @@ public final class StrategySelector implements Serializable { | ||
72 | return bundle; | 75 | return bundle; |
73 | } | 76 | } |
74 | 77 | ||
75 | - public static <T> Class<? extends T> getClass(Class<T> type, List<Class<? extends T>> options) | 78 | + @SuppressWarnings("unchecked") |
79 | + public static <T> T getInstance(Class<T> type, Collection<? extends T> options) throws ConfigurationException { | ||
80 | + | ||
81 | + Map<Class<? extends T>, T> map = new HashMap<Class<? extends T>, T>(); | ||
82 | + | ||
83 | + for (T instance : options) { | ||
84 | + if (instance != null) { | ||
85 | + map.put((Class<T>) instance.getClass(), instance); | ||
86 | + } | ||
87 | + } | ||
88 | + | ||
89 | + Class<? extends T> elected = getClass(type, map.keySet()); | ||
90 | + return map.get(elected); | ||
91 | + } | ||
92 | + | ||
93 | + public static <T> Class<? extends T> getClass(Class<T> type, Collection<Class<? extends T>> options) | ||
76 | throws ConfigurationException { | 94 | throws ConfigurationException { |
77 | Class<? extends T> selected = null; | 95 | Class<? extends T> selected = null; |
78 | 96 | ||
@@ -88,7 +106,7 @@ public final class StrategySelector implements Serializable { | @@ -88,7 +106,7 @@ public final class StrategySelector implements Serializable { | ||
88 | } | 106 | } |
89 | 107 | ||
90 | private static <T> void checkForAmbiguity(Class<T> type, Class<? extends T> selected, | 108 | private static <T> void checkForAmbiguity(Class<T> type, Class<? extends T> selected, |
91 | - List<Class<? extends T>> options) throws ConfigurationException { | 109 | + Collection<Class<? extends T>> options) throws ConfigurationException { |
92 | int selectedPriority = getPriority(selected); | 110 | int selectedPriority = getPriority(selected); |
93 | 111 | ||
94 | List<Class<? extends T>> ambiguous = new ArrayList<Class<? extends T>>(); | 112 | List<Class<? extends T>> ambiguous = new ArrayList<Class<? extends T>>(); |