From 7afc724cf97d289dcc685788d7e126055daf42ff Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Wed, 3 Apr 2013 16:33:13 -0300 Subject: [PATCH] Implementação do carregamento de configuração do tipo Map --- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------- impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/AbstractMapFieldConfig.java | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/ConfigurationMapFieldTest.java | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/PropertiesMapFieldConfig.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/test/resources/configuration/field/map/demoiselle.properties | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 293 insertions(+), 21 deletions(-) create mode 100644 impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/AbstractMapFieldConfig.java create mode 100644 impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/ConfigurationMapFieldTest.java create mode 100644 impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/PropertiesMapFieldConfig.java create mode 100644 impl/core/src/test/resources/configuration/field/map/demoiselle.properties diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java index c906293..25d9e08 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java @@ -41,9 +41,12 @@ import static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM; import java.io.Serializable; import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.validation.constraints.NotNull; @@ -158,7 +161,16 @@ public class ConfigurationLoader implements Serializable { } private void loadPrefix() { - this.prefix = this.object.getClass().getAnnotation(Configuration.class).prefix(); + String prefix = this.object.getClass().getAnnotation(Configuration.class).prefix(); + + if (prefix.endsWith(".")) { + // prefix = prefix.substring(0, prefix.length() - 1); + // TODO Lançar warning pedindo para retirar o ponto (.)? + } else if (!prefix.isEmpty()) { + prefix += "."; + } + + this.prefix = prefix; } private void loadFields() { @@ -180,29 +192,65 @@ public class ConfigurationLoader implements Serializable { return; } - String key = getKey(field); + Object defaultValue = Reflections.getFieldValue(field, this.object); + Object finalValue = getValue(field.getType(), getKey(field), defaultValue); + + Reflections.setFieldValue(field, this.object, finalValue); + } + + private Object getValue(Class type, String key, Object defaultValue) { Object value; - if (field.getType().isArray()) { - value = getArrayValue(field, key); - } else if (field.getType() == Class.class) { - value = getClassValue(field, key); + if (type.isArray()) { + value = getArrayValue(type, key, defaultValue); + + } else if (type == Map.class) { + value = getMapValue(type, key, defaultValue); + + } else if (type == Class.class) { + value = getClassValue(type, key, defaultValue); + } else { - value = getPrimitiveOrWrappedValue(field, key); + value = getPrimitiveOrWrappedValue(type, key, defaultValue); } - Reflections.setFieldValue(field, this.object, value); + return value; } - private Object getArrayValue(Field field, String key) { - return this.configuration.getArray(field.getType().getComponentType(), key, - Reflections.getFieldValue(field, this.object)); + private Object getArrayValue(Class type, String key, Object defaultValue) { + return this.configuration.getArray(type.getComponentType(), this.prefix + key, defaultValue); } - private Object getClassValue(Field field, String key) { - Object value = null; + private Object getMapValue(Class type, String key, Object defaultValue) { + @SuppressWarnings("unchecked") + Map value = (Map) defaultValue; - String canonicalName = this.configuration.getString(key.toString()); + String regexp = "^(" + this.prefix + ")((.+)\\.)?(" + key + ")$"; + Pattern pattern = Pattern.compile(regexp); + + for (Iterator iter = this.configuration.getKeys(); iter.hasNext();) { + String iterKey = iter.next(); + Matcher matcher = pattern.matcher(iterKey); + + if (matcher.matches()) { + String confKey = matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2)) + + matcher.group(4); + + if (value == null) { + value = new HashMap(); + } + + String mapKey = matcher.group(3) == null ? "default" : matcher.group(3); + value.put(mapKey, this.configuration.getString(confKey)); + } + } + + return value; + } + + private Object getClassValue(Class type, String key, Object defaultValue) { + Object value = defaultValue; + String canonicalName = this.configuration.getString(this.prefix + key); if (canonicalName != null) { ClassLoader classLoader = Reflections.getClassLoaderForClass(canonicalName); @@ -219,13 +267,11 @@ public class ConfigurationLoader implements Serializable { } @SuppressWarnings("unchecked") - private Object getPrimitiveOrWrappedValue(Field field, String key) { + private Object getPrimitiveOrWrappedValue(Class type, String key, Object defaultValue) { Object value; - Object defaultValue = Reflections.getFieldValue(field, this.object); try { - Class type = ClassUtils.primitiveToWrapper(field.getType()); - value = this.configuration.get(type, key, defaultValue); + value = this.configuration.get(ClassUtils.primitiveToWrapper(type), this.prefix + key, defaultValue); } catch (ConversionException cause) { value = defaultValue; @@ -235,7 +281,7 @@ public class ConfigurationLoader implements Serializable { } private String getKey(Field field) { - String key = this.prefix; + String key = ""; if (field.isAnnotationPresent(Name.class)) { key += field.getAnnotation(Name.class).value(); diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/AbstractMapFieldConfig.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/AbstractMapFieldConfig.java new file mode 100644 index 0000000..f7d2644 --- /dev/null +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/AbstractMapFieldConfig.java @@ -0,0 +1,54 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.configuration.field.map; + +import java.util.Map; + +public abstract class AbstractMapFieldConfig { + + private Map stringWithDefinedKeyMap; + + private Map stringWithUndefinedKeyMap; + + public Map getStringWithUndefinedKeyMap() { + return stringWithUndefinedKeyMap; + } + + public Map getStringWithDefinedKeyMap() { + return stringWithDefinedKeyMap; + } +} diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/ConfigurationMapFieldTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/ConfigurationMapFieldTest.java new file mode 100644 index 0000000..e81dbc4 --- /dev/null +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/ConfigurationMapFieldTest.java @@ -0,0 +1,89 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.configuration.field.map; + +import static junit.framework.Assert.assertEquals; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +import javax.inject.Inject; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.asset.FileAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import br.gov.frameworkdemoiselle.configuration.AbstractConfigurationTest; + +@RunWith(Arquillian.class) +public class ConfigurationMapFieldTest extends AbstractConfigurationTest { + + @Inject + private PropertiesMapFieldConfig propertiesConfig; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = createConfigurationDeployment(); + + deployment.addPackages(true, ConfigurationMapFieldTest.class.getPackage()); + deployment.addAsResource(new FileAsset(new File( + "src/test/resources/configuration/field/map/demoiselle.properties")), "demoiselle.properties"); + + return deployment; + } + + @Test + public void loadStringWithDefinedKeyMap() { + Map expected = new HashMap(); + expected.put("item1", "demoiselle"); + expected.put("item2", "framework"); + + assertEquals(expected, propertiesConfig.getStringWithDefinedKeyMap()); + } + + @Test + public void loadStringWithUndefinedKeyMap() { + Map expected = new HashMap(); + expected.put("default", "undefined"); + + assertEquals(expected, propertiesConfig.getStringWithUndefinedKeyMap()); + } +} diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/PropertiesMapFieldConfig.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/PropertiesMapFieldConfig.java new file mode 100644 index 0000000..5bfb5c2 --- /dev/null +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/field/map/PropertiesMapFieldConfig.java @@ -0,0 +1,44 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.configuration.field.map; + +import static br.gov.frameworkdemoiselle.configuration.ConfigType.PROPERTIES; +import br.gov.frameworkdemoiselle.configuration.Configuration; + +@Configuration(type = PROPERTIES) +public class PropertiesMapFieldConfig extends AbstractMapFieldConfig { +} diff --git a/impl/core/src/test/resources/configuration/field/map/demoiselle.properties b/impl/core/src/test/resources/configuration/field/map/demoiselle.properties new file mode 100644 index 0000000..6bf9d78 --- /dev/null +++ b/impl/core/src/test/resources/configuration/field/map/demoiselle.properties @@ -0,0 +1,39 @@ +# Demoiselle Framework +# Copyright (C) 2010 SERPRO +# ---------------------------------------------------------------------------- +# This file is part of Demoiselle Framework. +# +# Demoiselle Framework is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public License version 3 +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License version 3 +# along with this program; if not, see +# or write to the Free Software Foundation, Inc., 51 Franklin Street, +# Fifth Floor, Boston, MA 02110-1301, USA. +# ---------------------------------------------------------------------------- +# Este arquivo é parte do Framework Demoiselle. +# +# O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou +# modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação +# do Software Livre (FSF). +# +# Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA +# GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou +# APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português +# para maiores detalhes. +# +# Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título +# "LICENCA.txt", junto com esse programa. Se não, acesse +# ou escreva para a Fundação do Software Livre (FSF) Inc., +# 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + +item1.stringWithDefinedKeyMap=demoiselle +item2.stringWithDefinedKeyMap=framework + +stringWithUndefinedKeyMap=undefined -- libgit2 0.21.2