Commit d0568bd520ed6b76ac0a2750683ecddcfbdfa9f7

Authored by Emerson Oliveira
2 parents 7e349fdc 7afc724c
Exists in master

Merge branch '2.4.0' of git@github.com:demoiselle/framework.git into 2.4.0

impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
... ... @@ -41,9 +41,12 @@ import static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM;
41 41  
42 42 import java.io.Serializable;
43 43 import java.lang.reflect.Field;
44   -import java.lang.reflect.ParameterizedType;
45   -import java.lang.reflect.Type;
  44 +import java.util.HashMap;
  45 +import java.util.Iterator;
46 46 import java.util.List;
  47 +import java.util.Map;
  48 +import java.util.regex.Matcher;
  49 +import java.util.regex.Pattern;
47 50  
48 51 import javax.validation.constraints.NotNull;
49 52  
... ... @@ -158,7 +161,16 @@ public class ConfigurationLoader implements Serializable {
158 161 }
159 162  
160 163 private void loadPrefix() {
161   - this.prefix = this.object.getClass().getAnnotation(Configuration.class).prefix();
  164 + String prefix = this.object.getClass().getAnnotation(Configuration.class).prefix();
  165 +
  166 + if (prefix.endsWith(".")) {
  167 + // prefix = prefix.substring(0, prefix.length() - 1);
  168 + // TODO Lançar warning pedindo para retirar o ponto (.)?
  169 + } else if (!prefix.isEmpty()) {
  170 + prefix += ".";
  171 + }
  172 +
  173 + this.prefix = prefix;
162 174 }
163 175  
164 176 private void loadFields() {
... ... @@ -180,29 +192,65 @@ public class ConfigurationLoader implements Serializable {
180 192 return;
181 193 }
182 194  
183   - String key = getKey(field);
  195 + Object defaultValue = Reflections.getFieldValue(field, this.object);
  196 + Object finalValue = getValue(field.getType(), getKey(field), defaultValue);
  197 +
  198 + Reflections.setFieldValue(field, this.object, finalValue);
  199 + }
  200 +
  201 + private Object getValue(Class<?> type, String key, Object defaultValue) {
184 202 Object value;
185 203  
186   - if (field.getType().isArray()) {
187   - value = getArrayValue(field, key);
188   - } else if (field.getType() == Class.class) {
189   - value = getClassValue(field, key);
  204 + if (type.isArray()) {
  205 + value = getArrayValue(type, key, defaultValue);
  206 +
  207 + } else if (type == Map.class) {
  208 + value = getMapValue(type, key, defaultValue);
  209 +
  210 + } else if (type == Class.class) {
  211 + value = getClassValue(type, key, defaultValue);
  212 +
190 213 } else {
191   - value = getPrimitiveOrWrappedValue(field, key);
  214 + value = getPrimitiveOrWrappedValue(type, key, defaultValue);
192 215 }
193 216  
194   - Reflections.setFieldValue(field, this.object, value);
  217 + return value;
195 218 }
196 219  
197   - private Object getArrayValue(Field field, String key) {
198   - return this.configuration.getArray(field.getType().getComponentType(), key,
199   - Reflections.getFieldValue(field, this.object));
  220 + private Object getArrayValue(Class<?> type, String key, Object defaultValue) {
  221 + return this.configuration.getArray(type.getComponentType(), this.prefix + key, defaultValue);
200 222 }
201 223  
202   - private Object getClassValue(Field field, String key) {
203   - Object value = null;
  224 + private Object getMapValue(Class<?> type, String key, Object defaultValue) {
  225 + @SuppressWarnings("unchecked")
  226 + Map<String, Object> value = (Map<String, Object>) defaultValue;
204 227  
205   - String canonicalName = this.configuration.getString(key.toString());
  228 + String regexp = "^(" + this.prefix + ")((.+)\\.)?(" + key + ")$";
  229 + Pattern pattern = Pattern.compile(regexp);
  230 +
  231 + for (Iterator<String> iter = this.configuration.getKeys(); iter.hasNext();) {
  232 + String iterKey = iter.next();
  233 + Matcher matcher = pattern.matcher(iterKey);
  234 +
  235 + if (matcher.matches()) {
  236 + String confKey = matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2))
  237 + + matcher.group(4);
  238 +
  239 + if (value == null) {
  240 + value = new HashMap<String, Object>();
  241 + }
  242 +
  243 + String mapKey = matcher.group(3) == null ? "default" : matcher.group(3);
  244 + value.put(mapKey, this.configuration.getString(confKey));
  245 + }
  246 + }
  247 +
  248 + return value;
  249 + }
  250 +
  251 + private Object getClassValue(Class<?> type, String key, Object defaultValue) {
  252 + Object value = defaultValue;
  253 + String canonicalName = this.configuration.getString(this.prefix + key);
206 254  
207 255 if (canonicalName != null) {
208 256 ClassLoader classLoader = Reflections.getClassLoaderForClass(canonicalName);
... ... @@ -219,13 +267,11 @@ public class ConfigurationLoader implements Serializable {
219 267 }
220 268  
221 269 @SuppressWarnings("unchecked")
222   - private Object getPrimitiveOrWrappedValue(Field field, String key) {
  270 + private Object getPrimitiveOrWrappedValue(Class<?> type, String key, Object defaultValue) {
223 271 Object value;
224   - Object defaultValue = Reflections.getFieldValue(field, this.object);
225 272  
226 273 try {
227   - Class<Object> type = ClassUtils.primitiveToWrapper(field.getType());
228   - value = this.configuration.get(type, key, defaultValue);
  274 + value = this.configuration.get(ClassUtils.primitiveToWrapper(type), this.prefix + key, defaultValue);
229 275  
230 276 } catch (ConversionException cause) {
231 277 value = defaultValue;
... ... @@ -235,7 +281,7 @@ public class ConfigurationLoader implements Serializable {
235 281 }
236 282  
237 283 private String getKey(Field field) {
238   - String key = this.prefix;
  284 + String key = "";
239 285  
240 286 if (field.isAnnotationPresent(Name.class)) {
241 287 key += field.getAnnotation(Name.class).value();
... ...
impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/AbstractMapFieldConfig.java 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.configuration.field.map;
  38 +
  39 +import java.util.Map;
  40 +
  41 +public abstract class AbstractMapFieldConfig {
  42 +
  43 + private Map<String, String> stringWithDefinedKeyMap;
  44 +
  45 + private Map<String, Integer> stringWithUndefinedKeyMap;
  46 +
  47 + public Map<String, Integer> getStringWithUndefinedKeyMap() {
  48 + return stringWithUndefinedKeyMap;
  49 + }
  50 +
  51 + public Map<String, String> getStringWithDefinedKeyMap() {
  52 + return stringWithDefinedKeyMap;
  53 + }
  54 +}
... ...
impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/ConfigurationMapFieldTest.java 0 → 100644
... ... @@ -0,0 +1,89 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.configuration.field.map;
  38 +
  39 +import static junit.framework.Assert.assertEquals;
  40 +
  41 +import java.io.File;
  42 +import java.util.HashMap;
  43 +import java.util.Map;
  44 +
  45 +import javax.inject.Inject;
  46 +
  47 +import org.jboss.arquillian.container.test.api.Deployment;
  48 +import org.jboss.arquillian.junit.Arquillian;
  49 +import org.jboss.shrinkwrap.api.asset.FileAsset;
  50 +import org.jboss.shrinkwrap.api.spec.JavaArchive;
  51 +import org.junit.Test;
  52 +import org.junit.runner.RunWith;
  53 +
  54 +import br.gov.frameworkdemoiselle.configuration.AbstractConfigurationTest;
  55 +
  56 +@RunWith(Arquillian.class)
  57 +public class ConfigurationMapFieldTest extends AbstractConfigurationTest {
  58 +
  59 + @Inject
  60 + private PropertiesMapFieldConfig propertiesConfig;
  61 +
  62 + @Deployment
  63 + public static JavaArchive createDeployment() {
  64 + JavaArchive deployment = createConfigurationDeployment();
  65 +
  66 + deployment.addPackages(true, ConfigurationMapFieldTest.class.getPackage());
  67 + deployment.addAsResource(new FileAsset(new File(
  68 + "src/test/resources/configuration/field/map/demoiselle.properties")), "demoiselle.properties");
  69 +
  70 + return deployment;
  71 + }
  72 +
  73 + @Test
  74 + public void loadStringWithDefinedKeyMap() {
  75 + Map<String, String> expected = new HashMap<String, String>();
  76 + expected.put("item1", "demoiselle");
  77 + expected.put("item2", "framework");
  78 +
  79 + assertEquals(expected, propertiesConfig.getStringWithDefinedKeyMap());
  80 + }
  81 +
  82 + @Test
  83 + public void loadStringWithUndefinedKeyMap() {
  84 + Map<String, String> expected = new HashMap<String, String>();
  85 + expected.put("default", "undefined");
  86 +
  87 + assertEquals(expected, propertiesConfig.getStringWithUndefinedKeyMap());
  88 + }
  89 +}
... ...
impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/PropertiesMapFieldConfig.java 0 → 100644
... ... @@ -0,0 +1,44 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.configuration.field.map;
  38 +
  39 +import static br.gov.frameworkdemoiselle.configuration.ConfigType.PROPERTIES;
  40 +import br.gov.frameworkdemoiselle.configuration.Configuration;
  41 +
  42 +@Configuration(type = PROPERTIES)
  43 +public class PropertiesMapFieldConfig extends AbstractMapFieldConfig {
  44 +}
... ...
impl/core/src/test/resources/configuration/field/map/demoiselle.properties 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +# Demoiselle Framework
  2 +# Copyright (C) 2010 SERPRO
  3 +# ----------------------------------------------------------------------------
  4 +# This file is part of Demoiselle Framework.
  5 +#
  6 +# Demoiselle Framework is free software; you can redistribute it and/or
  7 +# modify it under the terms of the GNU Lesser General Public License version 3
  8 +# as published by the Free Software Foundation.
  9 +#
  10 +# This program is distributed in the hope that it will be useful,
  11 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13 +# GNU General Public License for more details.
  14 +#
  15 +# You should have received a copy of the GNU Lesser General Public License version 3
  16 +# along with this program; if not, see <http://www.gnu.org/licenses/>
  17 +# or write to the Free Software Foundation, Inc., 51 Franklin Street,
  18 +# Fifth Floor, Boston, MA 02110-1301, USA.
  19 +# ----------------------------------------------------------------------------
  20 +# Este arquivo é parte do Framework Demoiselle.
  21 +#
  22 +# O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  23 +# modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  24 +# do Software Livre (FSF).
  25 +#
  26 +# Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  27 +# GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  28 +# APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  29 +# para maiores detalhes.
  30 +#
  31 +# Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  32 +# "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  33 +# ou escreva para a Fundação do Software Livre (FSF) Inc.,
  34 +# 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  35 +
  36 +item1.stringWithDefinedKeyMap=demoiselle
  37 +item2.stringWithDefinedKeyMap=framework
  38 +
  39 +stringWithUndefinedKeyMap=undefined
... ...