Commit 6b7e32cb4b062c23633566e7eca5dfacd29cde6a

Authored by Cleverson Sacramento
1 parent a6feb89f
Exists in master

Suporte ao carregamento de Map nas classes de configuração

impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
@@ -43,10 +43,12 @@ import java.lang.reflect.Method; @@ -43,10 +43,12 @@ import java.lang.reflect.Method;
43 import java.lang.reflect.ParameterizedType; 43 import java.lang.reflect.ParameterizedType;
44 import java.lang.reflect.Type; 44 import java.lang.reflect.Type;
45 import java.net.URL; 45 import java.net.URL;
46 -import java.util.HashSet; 46 +import java.util.HashMap;
47 import java.util.Iterator; 47 import java.util.Iterator;
  48 +import java.util.Map;
48 import java.util.Properties; 49 import java.util.Properties;
49 -import java.util.Set; 50 +import java.util.regex.Matcher;
  51 +import java.util.regex.Pattern;
50 52
51 import javax.validation.constraints.NotNull; 53 import javax.validation.constraints.NotNull;
52 54
@@ -114,7 +116,7 @@ public class ConfigurationLoader implements Serializable { @@ -114,7 +116,7 @@ public class ConfigurationLoader implements Serializable {
114 org.apache.commons.configuration.Configuration config = getConfiguration(resource, type); 116 org.apache.commons.configuration.Configuration config = getConfiguration(resource, type);
115 117
116 if (config != null) { 118 if (config != null) {
117 - String key = getKey(field, clazz, config); 119 + Key key = new Key(field, clazz, config);
118 Object value = getValue(key, field, config); 120 Object value = getValue(key, field, config);
119 121
120 validate(field, key, value, resource); 122 validate(field, key, value, resource);
@@ -123,106 +125,21 @@ public class ConfigurationLoader implements Serializable { @@ -123,106 +125,21 @@ public class ConfigurationLoader implements Serializable {
123 } 125 }
124 } 126 }
125 127
126 - private void setValue(Field field, String key, Object object, Object value) { 128 + private void setValue(Field field, Key key, Object object, Object value) {
127 if (value != null) { 129 if (value != null) {
128 Reflections.setFieldValue(field, object, value); 130 Reflections.setFieldValue(field, object, value);
129 - getLogger().debug(getBundle().getString("configuration-field-loaded", key, field.getName(), value)); 131 + getLogger().debug(
  132 + getBundle().getString("configuration-field-loaded", key.toString(), field.getName(), value));
130 } 133 }
131 } 134 }
132 135
133 - private void validate(Field field, String key, Object value, String resource) { 136 + private void validate(Field field, Key key, Object value, String resource) {
134 if (field.isAnnotationPresent(NotNull.class) && value == null) { 137 if (field.isAnnotationPresent(NotNull.class) && value == null) {
135 - throw new ConfigurationException(getBundle().getString("configuration-attribute-is-mandatory", key,  
136 - resource)); 138 + throw new ConfigurationException(getBundle().getString("configuration-attribute-is-mandatory",
  139 + key.toString(), resource));
137 } 140 }
138 } 141 }
139 142
140 - private String getKey(final Field field, final Class<?> clazz,  
141 - final org.apache.commons.configuration.Configuration config) {  
142 -  
143 - final String prefix = getPrefix(field, clazz);  
144 - final StringBuffer key = new StringBuffer();  
145 -  
146 - key.append(prefix);  
147 -  
148 - if (field.isAnnotationPresent(Name.class)) {  
149 - key.append(getKeyByAnnotation(field));  
150 - } else {  
151 - key.append(getKeyByConvention(field, prefix, config));  
152 - }  
153 -  
154 - return key.toString();  
155 - }  
156 -  
157 - private String getPrefix(Field field, Class<?> type) {  
158 - String prefix = "";  
159 -  
160 - Configuration classAnnotation = type.getAnnotation(Configuration.class);  
161 - if (!Strings.isEmpty(classAnnotation.prefix())) {  
162 -  
163 - prefix = classAnnotation.prefix();  
164 -  
165 - if (prefix.charAt(prefix.length() - 1) != '.') {  
166 - getLogger().warn(  
167 - "ATENÇÃO!!! Informe o ponto (.) ao final da declaração do atributo prefix = \"" + prefix  
168 - + "\" da anotação @Configuration da classe " + type.getCanonicalName()  
169 - + " para evitar incompatibilidade com as próximas versões do Demoiselle.");  
170 -  
171 - prefix += ".";  
172 - }  
173 - }  
174 -  
175 - return prefix;  
176 - }  
177 -  
178 - private String getKeyByAnnotation(Field field) {  
179 - String key = null;  
180 -  
181 - Name nameAnnotation = field.getAnnotation(Name.class);  
182 - if (Strings.isEmpty(nameAnnotation.value())) {  
183 - throw new ConfigurationException(getBundle().getString("configuration-name-attribute-cant-be-empty"));  
184 - } else {  
185 - key = nameAnnotation.value();  
186 - }  
187 -  
188 - return key;  
189 - }  
190 -  
191 - private String getKeyByConvention(Field field, String prefix, org.apache.commons.configuration.Configuration config) {  
192 -  
193 - Set<String> conventions = new HashSet<String>();  
194 - conventions.add(field.getName());  
195 - conventions.add(Strings.camelCaseToSymbolSeparated(field.getName(), "."));  
196 - conventions.add(Strings.camelCaseToSymbolSeparated(field.getName(), "_"));  
197 - conventions.add(field.getName().toLowerCase());  
198 - conventions.add(field.getName().toUpperCase());  
199 -  
200 - int matches = 0;  
201 - String key = field.getName();  
202 - for (String convention : conventions) {  
203 - if (config.containsKey(prefix + convention)) {  
204 - key = convention;  
205 - matches++;  
206 - }  
207 - }  
208 -  
209 - if (!field.getName().equals(key)) {  
210 - getLogger().warn(  
211 - "ATENÇÃO!!! Anote o atributo " + field.getName() + " da classe "  
212 - + field.getDeclaringClass().getCanonicalName() + " com @Name(\"" + key  
213 - + "\") para evitar incompatibilidade com as próximas versões do Demoiselle.");  
214 - }  
215 -  
216 - if (matches == 0) {  
217 - getLogger().debug(getBundle().getString("configuration-key-not-found", key, conventions));  
218 - } else if (matches > 1) {  
219 - throw new ConfigurationException(getBundle().getString("ambiguous-key", field.getName(),  
220 - field.getDeclaringClass()));  
221 - }  
222 -  
223 - return key;  
224 - }  
225 -  
226 /** 143 /**
227 * Returns the configuration class according to specified resource name and configuration type. 144 * Returns the configuration class according to specified resource name and configuration type.
228 * 145 *
@@ -273,7 +190,7 @@ public class ConfigurationLoader implements Serializable { @@ -273,7 +190,7 @@ public class ConfigurationLoader implements Serializable {
273 } 190 }
274 191
275 @SuppressWarnings("unchecked") 192 @SuppressWarnings("unchecked")
276 - private <T> T getValue(String key, Field field, org.apache.commons.configuration.Configuration config) { 193 + private <T> T getValue(Key key, Field field, org.apache.commons.configuration.Configuration config) {
277 Object value; 194 Object value;
278 195
279 Class<?> fieldClass = (Class<?>) field.getType(); 196 Class<?> fieldClass = (Class<?>) field.getType();
@@ -281,6 +198,9 @@ public class ConfigurationLoader implements Serializable { @@ -281,6 +198,9 @@ public class ConfigurationLoader implements Serializable {
281 if (fieldClass.isArray()) { 198 if (fieldClass.isArray()) {
282 value = getArray(key, field, config); 199 value = getArray(key, field, config);
283 200
  201 + } else if (fieldClass.equals(Map.class)) {
  202 + value = getMap(key, field, config);
  203 +
284 } else if (fieldClass.equals(Properties.class)) { 204 } else if (fieldClass.equals(Properties.class)) {
285 value = getProperty(key, config); 205 value = getProperty(key, config);
286 206
@@ -294,7 +214,37 @@ public class ConfigurationLoader implements Serializable { @@ -294,7 +214,37 @@ public class ConfigurationLoader implements Serializable {
294 return (T) value; 214 return (T) value;
295 } 215 }
296 216
297 - private <T> Object getArray(String key, Field field, org.apache.commons.configuration.Configuration config) { 217 + private <T> Object getMap(Key key, Field field, org.apache.commons.configuration.Configuration config) {
  218 + Map<String, Object> value = null;
  219 +
  220 + Pattern pattern = Pattern.compile("^(" + key.getPrefix() + ")(.+)\\.(" + key.getName() + ")$");
  221 + Matcher matcher;
  222 +
  223 + String iterKey;
  224 + String mapKey;
  225 + String confKey;
  226 +
  227 + for (@SuppressWarnings("unchecked")
  228 + Iterator<String> iter = config.getKeys(); iter.hasNext();) {
  229 + iterKey = iter.next();
  230 + matcher = pattern.matcher(iterKey);
  231 +
  232 + if (matcher.matches()) {
  233 + mapKey = matcher.group(2);
  234 + confKey = matcher.group(1) + matcher.group(2) + "." + matcher.group(3);
  235 +
  236 + if (value == null) {
  237 + value = new HashMap<String, Object>();
  238 + }
  239 +
  240 + value.put(mapKey, config.getProperty(confKey));
  241 + }
  242 + }
  243 +
  244 + return value;
  245 + }
  246 +
  247 + private <T> Object getArray(Key key, Field field, org.apache.commons.configuration.Configuration config) {
298 Object value = null; 248 Object value = null;
299 249
300 Class<?> fieldClass = (Class<?>) field.getType(); 250 Class<?> fieldClass = (Class<?>) field.getType();
@@ -309,7 +259,7 @@ public class ConfigurationLoader implements Serializable { @@ -309,7 +259,7 @@ public class ConfigurationLoader implements Serializable {
309 methodName += "Array"; 259 methodName += "Array";
310 260
311 method = config.getClass().getMethod(methodName, String.class); 261 method = config.getClass().getMethod(methodName, String.class);
312 - value = method.invoke(config, key); 262 + value = method.invoke(config, key.toString());
313 263
314 } catch (Throwable cause) { 264 } catch (Throwable cause) {
315 throw new ConfigurationException(getBundle().getString("error-converting-to-type", fieldClass.getName()), 265 throw new ConfigurationException(getBundle().getString("error-converting-to-type", fieldClass.getName()),
@@ -319,7 +269,7 @@ public class ConfigurationLoader implements Serializable { @@ -319,7 +269,7 @@ public class ConfigurationLoader implements Serializable {
319 return value; 269 return value;
320 } 270 }
321 271
322 - private <T> Object getBasic(String key, Field field, org.apache.commons.configuration.Configuration config) { 272 + private <T> Object getBasic(Key key, Field field, org.apache.commons.configuration.Configuration config) {
323 Object value = null; 273 Object value = null;
324 274
325 Class<?> fieldClass = (Class<?>) field.getType(); 275 Class<?> fieldClass = (Class<?>) field.getType();
@@ -333,11 +283,11 @@ public class ConfigurationLoader implements Serializable { @@ -333,11 +283,11 @@ public class ConfigurationLoader implements Serializable {
333 283
334 if (!fieldClass.isPrimitive()) { 284 if (!fieldClass.isPrimitive()) {
335 method = config.getClass().getMethod(methodName, String.class, fieldClass); 285 method = config.getClass().getMethod(methodName, String.class, fieldClass);
336 - value = method.invoke(config, key, null); 286 + value = method.invoke(config, key.toString(), null);
337 287
338 - } else if (config.containsKey(key)) { 288 + } else if (config.containsKey(key.toString())) {
339 method = config.getClass().getMethod(methodName, String.class); 289 method = config.getClass().getMethod(methodName, String.class);
340 - value = method.invoke(config, key); 290 + value = method.invoke(config, key.toString());
341 } 291 }
342 292
343 } catch (Throwable cause) { 293 } catch (Throwable cause) {
@@ -348,11 +298,11 @@ public class ConfigurationLoader implements Serializable { @@ -348,11 +298,11 @@ public class ConfigurationLoader implements Serializable {
348 return value; 298 return value;
349 } 299 }
350 300
351 - private <T> Object getClass(String key, Field field, org.apache.commons.configuration.Configuration config) { 301 + private <T> Object getClass(Key key, Field field, org.apache.commons.configuration.Configuration config) {
352 Object value = null; 302 Object value = null;
353 303
354 try { 304 try {
355 - String canonicalName = config.getString(key); 305 + String canonicalName = config.getString(key.toString());
356 306
357 if (canonicalName != null) { 307 if (canonicalName != null) {
358 ClassLoader classLoader = getClassLoaderForClass(canonicalName); 308 ClassLoader classLoader = getClassLoaderForClass(canonicalName);
@@ -395,17 +345,17 @@ public class ConfigurationLoader implements Serializable { @@ -395,17 +345,17 @@ public class ConfigurationLoader implements Serializable {
395 return ""; 345 return "";
396 } 346 }
397 347
398 - private Object getProperty(String key, org.apache.commons.configuration.Configuration config) { 348 + private Object getProperty(Key key, org.apache.commons.configuration.Configuration config) {
399 Object value = null; 349 Object value = null;
400 350
401 @SuppressWarnings("unchecked") 351 @SuppressWarnings("unchecked")
402 - Iterator<String> iterator = config.getKeys(key); 352 + Iterator<String> iterator = config.getKeys(key.toString());
403 if (iterator.hasNext()) { 353 if (iterator.hasNext()) {
404 Properties props = new Properties(); 354 Properties props = new Properties();
405 355
406 while (iterator.hasNext()) { 356 while (iterator.hasNext()) {
407 String fullKey = iterator.next(); 357 String fullKey = iterator.next();
408 - String prefix = key + "."; 358 + String prefix = key.toString() + ".";
409 String unprefixedKey = fullKey.substring(prefix.length()); 359 String unprefixedKey = fullKey.substring(prefix.length());
410 props.put(unprefixedKey, config.getString(fullKey)); 360 props.put(unprefixedKey, config.getString(fullKey));
411 } 361 }
@@ -470,4 +420,61 @@ public class ConfigurationLoader implements Serializable { @@ -470,4 +420,61 @@ public class ConfigurationLoader implements Serializable {
470 420
471 return bootstrap; 421 return bootstrap;
472 } 422 }
  423 +
  424 + private class Key {
  425 +
  426 + private String prefix;
  427 +
  428 + private String name;
  429 +
  430 + private String key;
  431 +
  432 + private Key(final Field field, final Class<?> type, final org.apache.commons.configuration.Configuration config) {
  433 +
  434 + this.prefix = type.getAnnotation(Configuration.class).prefix();
  435 + if (this.prefix == null) {
  436 + this.prefix = "";
  437 + } else {
  438 + this.prefix += ".";
  439 + }
  440 +
  441 + if (field.isAnnotationPresent(Name.class)) {
  442 + this.name = getNameByAnnotation(field);
  443 + } else {
  444 + this.name = getNameByField(field);
  445 + }
  446 +
  447 + this.key = this.prefix + this.name;
  448 + }
  449 +
  450 + private String getNameByAnnotation(Field field) {
  451 + String key = null;
  452 +
  453 + Name nameAnnotation = field.getAnnotation(Name.class);
  454 + if (Strings.isEmpty(nameAnnotation.value())) {
  455 + throw new ConfigurationException(getBundle().getString("configuration-name-attribute-cant-be-empty"));
  456 + } else {
  457 + key = nameAnnotation.value();
  458 + }
  459 +
  460 + return key;
  461 + }
  462 +
  463 + private String getNameByField(Field field) {
  464 + return field.getName();
  465 + }
  466 +
  467 + public String getPrefix() {
  468 + return prefix;
  469 + }
  470 +
  471 + public String getName() {
  472 + return name;
  473 + }
  474 +
  475 + @Override
  476 + public String toString() {
  477 + return this.key;
  478 + }
  479 + }
473 } 480 }