Commit c07d38170c0148e7bfd568ca8efc78a4f5c3b275
1 parent
7afc724c
Exists in
master
Criando estratégias de extração de valores das classes de configuração
Showing
10 changed files
with
933 additions
and
90 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/configuration/ConfigurationValueExtractor.java
0 → 100644
| @@ -0,0 +1,48 @@ | @@ -0,0 +1,48 @@ | ||
| 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; | ||
| 38 | + | ||
| 39 | +import java.lang.reflect.Field; | ||
| 40 | + | ||
| 41 | +import org.apache.commons.configuration.DataConfiguration; | ||
| 42 | + | ||
| 43 | +public interface ConfigurationValueExtractor { | ||
| 44 | + | ||
| 45 | + Object getValue(String prefix, String key, Field field, DataConfiguration configuration, Object defaultValue); | ||
| 46 | + | ||
| 47 | + boolean isSupported(Field field); | ||
| 48 | +} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ConfigurationBootstrap.java
| @@ -53,13 +53,18 @@ import javassist.NotFoundException; | @@ -53,13 +53,18 @@ import javassist.NotFoundException; | ||
| 53 | import javax.enterprise.event.Observes; | 53 | import javax.enterprise.event.Observes; |
| 54 | import javax.enterprise.inject.spi.AnnotatedType; | 54 | import javax.enterprise.inject.spi.AnnotatedType; |
| 55 | import javax.enterprise.inject.spi.BeanManager; | 55 | import javax.enterprise.inject.spi.BeanManager; |
| 56 | -import javax.enterprise.inject.spi.Extension; | ||
| 57 | import javax.enterprise.inject.spi.ProcessAnnotatedType; | 56 | import javax.enterprise.inject.spi.ProcessAnnotatedType; |
| 58 | 57 | ||
| 58 | +import org.slf4j.Logger; | ||
| 59 | + | ||
| 59 | import br.gov.frameworkdemoiselle.configuration.Configuration; | 60 | import br.gov.frameworkdemoiselle.configuration.Configuration; |
| 61 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 60 | import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationImpl; | 62 | import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationImpl; |
| 63 | +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; | ||
| 64 | + | ||
| 65 | +public class ConfigurationBootstrap extends AbstractStrategyBootstrap<ConfigurationValueExtractor> { | ||
| 61 | 66 | ||
| 62 | -public class ConfigurationBootstrap implements Extension { | 67 | + private Logger logger; |
| 63 | 68 | ||
| 64 | private static final Map<ClassLoader, Map<String, Class<Object>>> cacheClassLoader = Collections | 69 | private static final Map<ClassLoader, Map<String, Class<Object>>> cacheClassLoader = Collections |
| 65 | .synchronizedMap(new HashMap<ClassLoader, Map<String, Class<Object>>>()); | 70 | .synchronizedMap(new HashMap<ClassLoader, Map<String, Class<Object>>>()); |
| @@ -130,4 +135,13 @@ public class ConfigurationBootstrap implements Extension { | @@ -130,4 +135,13 @@ public class ConfigurationBootstrap implements Extension { | ||
| 130 | 135 | ||
| 131 | return clazzProxy; | 136 | return clazzProxy; |
| 132 | } | 137 | } |
| 138 | + | ||
| 139 | + @Override | ||
| 140 | + protected Logger getLogger() { | ||
| 141 | + if (logger == null) { | ||
| 142 | + logger = LoggerProducer.create(TransactionBootstrap.class); | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + return logger; | ||
| 146 | + } | ||
| 133 | } | 147 | } |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationArrayValueExtractor.java
0 → 100644
| @@ -0,0 +1,57 @@ | @@ -0,0 +1,57 @@ | ||
| 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.internal.configuration; | ||
| 38 | + | ||
| 39 | +import java.lang.reflect.Field; | ||
| 40 | + | ||
| 41 | +import org.apache.commons.configuration.DataConfiguration; | ||
| 42 | + | ||
| 43 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 44 | + | ||
| 45 | +public class ConfigurationArrayValueExtractor implements ConfigurationValueExtractor { | ||
| 46 | + | ||
| 47 | + @Override | ||
| 48 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
| 49 | + Object defaultValue) { | ||
| 50 | + return configuration.getArray(field.getType().getComponentType(), prefix + key, defaultValue); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @Override | ||
| 54 | + public boolean isSupported(Field field) { | ||
| 55 | + return field.getType().isArray(); | ||
| 56 | + } | ||
| 57 | +} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationClassValueExtractor.java
0 → 100644
| @@ -0,0 +1,73 @@ | @@ -0,0 +1,73 @@ | ||
| 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.internal.configuration; | ||
| 38 | + | ||
| 39 | +import java.lang.reflect.Field; | ||
| 40 | + | ||
| 41 | +import org.apache.commons.configuration.DataConfiguration; | ||
| 42 | + | ||
| 43 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | ||
| 44 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 45 | +import br.gov.frameworkdemoiselle.util.Reflections; | ||
| 46 | + | ||
| 47 | +public class ConfigurationClassValueExtractor implements ConfigurationValueExtractor { | ||
| 48 | + | ||
| 49 | + @Override | ||
| 50 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
| 51 | + Object defaultValue) { | ||
| 52 | + Object value = defaultValue; | ||
| 53 | + String canonicalName = configuration.getString(prefix + key); | ||
| 54 | + | ||
| 55 | + if (canonicalName != null) { | ||
| 56 | + ClassLoader classLoader = Reflections.getClassLoaderForClass(canonicalName); | ||
| 57 | + | ||
| 58 | + try { | ||
| 59 | + value = Class.forName(canonicalName, true, classLoader); | ||
| 60 | + } catch (ClassNotFoundException cause) { | ||
| 61 | + // TODO Lançar a mensagem correta | ||
| 62 | + throw new ConfigurationException(null, cause); | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + return value; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + @Override | ||
| 70 | + public boolean isSupported(Field field) { | ||
| 71 | + return field.getType() == Class.class; | ||
| 72 | + } | ||
| 73 | +} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
| @@ -41,29 +41,27 @@ import static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM; | @@ -41,29 +41,27 @@ 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.HashMap; | ||
| 45 | -import java.util.Iterator; | 44 | +import java.util.ArrayList; |
| 46 | import java.util.List; | 45 | import java.util.List; |
| 47 | -import java.util.Map; | ||
| 48 | -import java.util.regex.Matcher; | ||
| 49 | -import java.util.regex.Pattern; | ||
| 50 | 46 | ||
| 47 | +import javax.inject.Inject; | ||
| 51 | import javax.validation.constraints.NotNull; | 48 | import javax.validation.constraints.NotNull; |
| 52 | 49 | ||
| 53 | import org.apache.commons.configuration.AbstractConfiguration; | 50 | import org.apache.commons.configuration.AbstractConfiguration; |
| 54 | -import org.apache.commons.configuration.ConversionException; | ||
| 55 | import org.apache.commons.configuration.DataConfiguration; | 51 | import org.apache.commons.configuration.DataConfiguration; |
| 56 | import org.apache.commons.configuration.FileConfiguration; | 52 | import org.apache.commons.configuration.FileConfiguration; |
| 57 | import org.apache.commons.configuration.PropertiesConfiguration; | 53 | import org.apache.commons.configuration.PropertiesConfiguration; |
| 58 | import org.apache.commons.configuration.SystemConfiguration; | 54 | import org.apache.commons.configuration.SystemConfiguration; |
| 59 | import org.apache.commons.configuration.XMLConfiguration; | 55 | import org.apache.commons.configuration.XMLConfiguration; |
| 60 | -import org.apache.commons.lang.ClassUtils; | ||
| 61 | 56 | ||
| 62 | import br.gov.frameworkdemoiselle.annotation.Ignore; | 57 | import br.gov.frameworkdemoiselle.annotation.Ignore; |
| 63 | import br.gov.frameworkdemoiselle.annotation.Name; | 58 | import br.gov.frameworkdemoiselle.annotation.Name; |
| 64 | import br.gov.frameworkdemoiselle.configuration.ConfigType; | 59 | import br.gov.frameworkdemoiselle.configuration.ConfigType; |
| 65 | import br.gov.frameworkdemoiselle.configuration.Configuration; | 60 | import br.gov.frameworkdemoiselle.configuration.Configuration; |
| 66 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | 61 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; |
| 62 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 63 | +import br.gov.frameworkdemoiselle.internal.bootstrap.ConfigurationBootstrap; | ||
| 64 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
| 67 | import br.gov.frameworkdemoiselle.util.Reflections; | 65 | import br.gov.frameworkdemoiselle.util.Reflections; |
| 68 | 66 | ||
| 69 | /** | 67 | /** |
| @@ -88,6 +86,11 @@ public class ConfigurationLoader implements Serializable { | @@ -88,6 +86,11 @@ public class ConfigurationLoader implements Serializable { | ||
| 88 | 86 | ||
| 89 | private List<Field> fields; | 87 | private List<Field> fields; |
| 90 | 88 | ||
| 89 | + private List<ConfigurationValueExtractor> extractors; | ||
| 90 | + | ||
| 91 | + @Inject | ||
| 92 | + private ConfigurationBootstrap bootstrap; | ||
| 93 | + | ||
| 91 | public void load(Object object) throws ConfigurationException { | 94 | public void load(Object object) throws ConfigurationException { |
| 92 | this.object = object; | 95 | this.object = object; |
| 93 | 96 | ||
| @@ -96,6 +99,7 @@ public class ConfigurationLoader implements Serializable { | @@ -96,6 +99,7 @@ public class ConfigurationLoader implements Serializable { | ||
| 96 | loadType(); | 99 | loadType(); |
| 97 | loadResource(); | 100 | loadResource(); |
| 98 | loadConfiguration(); | 101 | loadConfiguration(); |
| 102 | + loadExtractors(); | ||
| 99 | 103 | ||
| 100 | if (this.configuration != null) { | 104 | if (this.configuration != null) { |
| 101 | loadPrefix(); | 105 | loadPrefix(); |
| @@ -118,6 +122,15 @@ public class ConfigurationLoader implements Serializable { | @@ -118,6 +122,15 @@ public class ConfigurationLoader implements Serializable { | ||
| 118 | this.type = object.getClass().getAnnotation(Configuration.class).type(); | 122 | this.type = object.getClass().getAnnotation(Configuration.class).type(); |
| 119 | } | 123 | } |
| 120 | 124 | ||
| 125 | + private void loadResource() { | ||
| 126 | + if (this.type != SYSTEM) { | ||
| 127 | + String name = this.object.getClass().getAnnotation(Configuration.class).resource(); | ||
| 128 | + String extension = this.type.toString().toLowerCase(); | ||
| 129 | + | ||
| 130 | + this.resource = name + "." + extension; | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + | ||
| 121 | private void loadConfiguration() { | 134 | private void loadConfiguration() { |
| 122 | AbstractConfiguration conf; | 135 | AbstractConfiguration conf; |
| 123 | 136 | ||
| @@ -151,12 +164,11 @@ public class ConfigurationLoader implements Serializable { | @@ -151,12 +164,11 @@ public class ConfigurationLoader implements Serializable { | ||
| 151 | this.configuration = (conf == null ? null : new DataConfiguration(conf)); | 164 | this.configuration = (conf == null ? null : new DataConfiguration(conf)); |
| 152 | } | 165 | } |
| 153 | 166 | ||
| 154 | - private void loadResource() { | ||
| 155 | - if (this.type != SYSTEM) { | ||
| 156 | - String name = this.object.getClass().getAnnotation(Configuration.class).resource(); | ||
| 157 | - String extension = this.type.toString().toLowerCase(); | 167 | + private void loadExtractors() { |
| 168 | + this.extractors = new ArrayList<ConfigurationValueExtractor>(); | ||
| 158 | 169 | ||
| 159 | - this.resource = name + "." + extension; | 170 | + for (Class<? extends ConfigurationValueExtractor> extractorClass : this.bootstrap.getCache()) { |
| 171 | + this.extractors.add(Beans.getReference(extractorClass)); | ||
| 160 | } | 172 | } |
| 161 | } | 173 | } |
| 162 | 174 | ||
| @@ -193,93 +205,24 @@ public class ConfigurationLoader implements Serializable { | @@ -193,93 +205,24 @@ public class ConfigurationLoader implements Serializable { | ||
| 193 | } | 205 | } |
| 194 | 206 | ||
| 195 | Object defaultValue = Reflections.getFieldValue(field, this.object); | 207 | Object defaultValue = Reflections.getFieldValue(field, this.object); |
| 196 | - Object finalValue = getValue(field.getType(), getKey(field), defaultValue); | 208 | + Object finalValue = getValue(field, field.getType(), getKey(field), defaultValue); |
| 197 | 209 | ||
| 198 | Reflections.setFieldValue(field, this.object, finalValue); | 210 | Reflections.setFieldValue(field, this.object, finalValue); |
| 199 | } | 211 | } |
| 200 | 212 | ||
| 201 | - private Object getValue(Class<?> type, String key, Object defaultValue) { | ||
| 202 | - Object value; | ||
| 203 | - | ||
| 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 | - | ||
| 213 | - } else { | ||
| 214 | - value = getPrimitiveOrWrappedValue(type, key, defaultValue); | ||
| 215 | - } | ||
| 216 | - | ||
| 217 | - return value; | ||
| 218 | - } | 213 | + private Object getValue(Field field, Class<?> type, String key, Object defaultValue) { |
| 214 | + Object value = null; | ||
| 219 | 215 | ||
| 220 | - private Object getArrayValue(Class<?> type, String key, Object defaultValue) { | ||
| 221 | - return this.configuration.getArray(type.getComponentType(), this.prefix + key, defaultValue); | ||
| 222 | - } | ||
| 223 | - | ||
| 224 | - private Object getMapValue(Class<?> type, String key, Object defaultValue) { | ||
| 225 | - @SuppressWarnings("unchecked") | ||
| 226 | - Map<String, Object> value = (Map<String, Object>) defaultValue; | ||
| 227 | - | ||
| 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); | ||
| 254 | - | ||
| 255 | - if (canonicalName != null) { | ||
| 256 | - ClassLoader classLoader = Reflections.getClassLoaderForClass(canonicalName); | ||
| 257 | - | ||
| 258 | - try { | ||
| 259 | - value = Class.forName(canonicalName, true, classLoader); | ||
| 260 | - } catch (ClassNotFoundException cause) { | ||
| 261 | - // TODO Lançar a mensagem correta | ||
| 262 | - throw new ConfigurationException(null, cause); | 216 | + for (ConfigurationValueExtractor extractor : this.extractors) { |
| 217 | + if (extractor.isSupported(field)) { | ||
| 218 | + value = extractor.getValue(this.prefix, key, field, configuration, defaultValue); | ||
| 219 | + break; | ||
| 263 | } | 220 | } |
| 264 | } | 221 | } |
| 265 | 222 | ||
| 266 | return value; | 223 | return value; |
| 267 | } | 224 | } |
| 268 | 225 | ||
| 269 | - @SuppressWarnings("unchecked") | ||
| 270 | - private Object getPrimitiveOrWrappedValue(Class<?> type, String key, Object defaultValue) { | ||
| 271 | - Object value; | ||
| 272 | - | ||
| 273 | - try { | ||
| 274 | - value = this.configuration.get(ClassUtils.primitiveToWrapper(type), this.prefix + key, defaultValue); | ||
| 275 | - | ||
| 276 | - } catch (ConversionException cause) { | ||
| 277 | - value = defaultValue; | ||
| 278 | - } | ||
| 279 | - | ||
| 280 | - return value; | ||
| 281 | - } | ||
| 282 | - | ||
| 283 | private String getKey(Field field) { | 226 | private String getKey(Field field) { |
| 284 | String key = ""; | 227 | String key = ""; |
| 285 | 228 |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderBackup.java
0 → 100644
| @@ -0,0 +1,470 @@ | @@ -0,0 +1,470 @@ | ||
| 1 | +/* | ||
| 2 | +/* | ||
| 3 | + * Demoiselle Framework | ||
| 4 | + * Copyright (C) 2010 SERPRO | ||
| 5 | + * ---------------------------------------------------------------------------- | ||
| 6 | + * This file is part of Demoiselle Framework. | ||
| 7 | + * | ||
| 8 | + * Demoiselle Framework is free software; you can redistribute it and/or | ||
| 9 | + * modify it under the terms of the GNU Lesser General Public License version 3 | ||
| 10 | + * as published by the Free Software Foundation. | ||
| 11 | + * | ||
| 12 | + * This program is distributed in the hope that it will be useful, | ||
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | + * GNU General Public License for more details. | ||
| 16 | + * | ||
| 17 | + * You should have received a copy of the GNU Lesser General Public License version 3 | ||
| 18 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
| 19 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
| 20 | + * Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 21 | + * ---------------------------------------------------------------------------- | ||
| 22 | + * Este arquivo é parte do Framework Demoiselle. | ||
| 23 | + * | ||
| 24 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
| 25 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
| 26 | + * do Software Livre (FSF). | ||
| 27 | + * | ||
| 28 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
| 29 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
| 30 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
| 31 | + * para maiores detalhes. | ||
| 32 | + * | ||
| 33 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
| 34 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
| 35 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
| 36 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
| 37 | + */ | ||
| 38 | +package br.gov.frameworkdemoiselle.internal.configuration; | ||
| 39 | + | ||
| 40 | +import java.io.FileNotFoundException; | ||
| 41 | +import java.io.Serializable; | ||
| 42 | +import java.lang.reflect.Field; | ||
| 43 | +import java.lang.reflect.Method; | ||
| 44 | +import java.lang.reflect.ParameterizedType; | ||
| 45 | +import java.lang.reflect.Type; | ||
| 46 | +import java.net.URL; | ||
| 47 | +import java.util.HashMap; | ||
| 48 | +import java.util.Iterator; | ||
| 49 | +import java.util.Map; | ||
| 50 | +import java.util.Properties; | ||
| 51 | +import java.util.regex.Matcher; | ||
| 52 | +import java.util.regex.Pattern; | ||
| 53 | + | ||
| 54 | +import javax.validation.constraints.NotNull; | ||
| 55 | + | ||
| 56 | +import org.apache.commons.configuration.DataConfiguration; | ||
| 57 | +import org.apache.commons.configuration.PropertiesConfiguration; | ||
| 58 | +import org.apache.commons.configuration.SystemConfiguration; | ||
| 59 | +import org.apache.commons.configuration.XMLConfiguration; | ||
| 60 | +import org.slf4j.Logger; | ||
| 61 | + | ||
| 62 | +import br.gov.frameworkdemoiselle.annotation.Ignore; | ||
| 63 | +import br.gov.frameworkdemoiselle.annotation.Name; | ||
| 64 | +import br.gov.frameworkdemoiselle.configuration.ConfigType; | ||
| 65 | +import br.gov.frameworkdemoiselle.configuration.Configuration; | ||
| 66 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | ||
| 67 | +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; | ||
| 68 | +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | ||
| 69 | +import br.gov.frameworkdemoiselle.util.Reflections; | ||
| 70 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 71 | +import br.gov.frameworkdemoiselle.util.Strings; | ||
| 72 | + | ||
| 73 | +/** | ||
| 74 | + * This component loads a config class annotated with {@link Configuration} by filling its attributes with {@link Param} | ||
| 75 | + * according to a {@link ConfigType}. | ||
| 76 | + * | ||
| 77 | + * @author SERPRO | ||
| 78 | + */ | ||
| 79 | +public class ConfigurationLoaderBackup implements Serializable { | ||
| 80 | + | ||
| 81 | + private static final long serialVersionUID = 1L; | ||
| 82 | + | ||
| 83 | + private ResourceBundle bundle; | ||
| 84 | + | ||
| 85 | + private Logger logger; | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * Loads a config class filling it with the corresponding values. | ||
| 89 | + * | ||
| 90 | + * @param object | ||
| 91 | + * config object | ||
| 92 | + * @throws ConfigurationException | ||
| 93 | + */ | ||
| 94 | + public void load(Object object) throws ConfigurationException { | ||
| 95 | + Class<?> config = object.getClass(); | ||
| 96 | + | ||
| 97 | + getLogger().debug(getBundle().getString("loading-configuration-class", config.getName())); | ||
| 98 | + | ||
| 99 | + for (Field field : Reflections.getNonStaticFields(config)) { | ||
| 100 | + loadField(field, object, config); | ||
| 101 | + } | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + private void loadField(Field field, Object object, Class<?> clazz) { | ||
| 105 | + if (!field.isAnnotationPresent(Ignore.class) && clazz.isAnnotationPresent(Configuration.class)) { | ||
| 106 | + String resource = clazz.getAnnotation(Configuration.class).resource(); | ||
| 107 | + ConfigType type = clazz.getAnnotation(Configuration.class).type(); | ||
| 108 | + org.apache.commons.configuration.Configuration config = getConfiguration(resource, type); | ||
| 109 | + | ||
| 110 | + if (config != null) { | ||
| 111 | + Key key = new Key(field, clazz, config); | ||
| 112 | + Object value = getValue(key, field, config); | ||
| 113 | + validate(field, key, value, resource); | ||
| 114 | + setValue(field, key, object, value); | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + private void setValue(Field field, Key key, Object object, Object value) { | ||
| 120 | + if (value != null) { | ||
| 121 | + Reflections.setFieldValue(field, object, value); | ||
| 122 | + getLogger().debug( | ||
| 123 | + getBundle().getString("configuration-field-loaded", key.toString(), field.getName(), value)); | ||
| 124 | + } | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + private void validate(Field field, Key key, Object value, String resource) { | ||
| 128 | + if (field.isAnnotationPresent(NotNull.class) && value == null) { | ||
| 129 | + throw new ConfigurationException(getBundle().getString("configuration-attribute-is-mandatory", | ||
| 130 | + key.toString(), resource)); | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + /** | ||
| 135 | + * Returns the configuration class according to specified resource name and configuration type. | ||
| 136 | + * | ||
| 137 | + * @param resource | ||
| 138 | + * @param type | ||
| 139 | + * @return a configuration | ||
| 140 | + */ | ||
| 141 | + private org.apache.commons.configuration.Configuration getConfiguration(String resource, ConfigType type) { | ||
| 142 | + org.apache.commons.configuration.Configuration result = null; | ||
| 143 | + | ||
| 144 | + try { | ||
| 145 | + URL url; | ||
| 146 | + | ||
| 147 | + switch (type) { | ||
| 148 | + case SYSTEM: | ||
| 149 | + result = new SystemConfiguration(); | ||
| 150 | + break; | ||
| 151 | + | ||
| 152 | + case PROPERTIES: | ||
| 153 | + url = getResourceAsURL(resource + ".properties"); | ||
| 154 | + | ||
| 155 | + if (url != null) { | ||
| 156 | + result = new DataConfiguration(new PropertiesConfiguration(url)); | ||
| 157 | + } else { | ||
| 158 | + getLogger().warn(getBundle().getString("resource-not-found", resource + ".properties")); | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + break; | ||
| 162 | + | ||
| 163 | + case XML: | ||
| 164 | + url = getResourceAsURL(resource + ".xml"); | ||
| 165 | + | ||
| 166 | + if (url != null) { | ||
| 167 | + result = new DataConfiguration(new XMLConfiguration(url)); | ||
| 168 | + } else { | ||
| 169 | + getLogger().warn(getBundle().getString("resource-not-found", resource + ".xml")); | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + break; | ||
| 173 | + | ||
| 174 | + default: | ||
| 175 | + throw new ConfigurationException(getBundle().getString("configuration-type-not-implemented-yet", | ||
| 176 | + type.name())); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + } catch (Exception cause) { | ||
| 180 | + throw new ConfigurationException(getBundle().getString("error-creating-configuration-from-resource", | ||
| 181 | + resource), cause); | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + return result; | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + @SuppressWarnings("unchecked") | ||
| 188 | + private <T> T getValue(Key key, Field field, org.apache.commons.configuration.Configuration config) { | ||
| 189 | + Object value; | ||
| 190 | + | ||
| 191 | + Class<?> fieldClass = (Class<?>) field.getType(); | ||
| 192 | + | ||
| 193 | + if (fieldClass.isArray()) { | ||
| 194 | + value = getArray(key, field, config); | ||
| 195 | + | ||
| 196 | + } else if (fieldClass.equals(Map.class)) { | ||
| 197 | + value = getMap(key, field, config); | ||
| 198 | + | ||
| 199 | + } else if (fieldClass.equals(Properties.class)) { | ||
| 200 | + value = getProperty(key, config); | ||
| 201 | + | ||
| 202 | + } else if (fieldClass.equals(Class.class)) { | ||
| 203 | + value = getClass(key, field, config); | ||
| 204 | + | ||
| 205 | + } else { | ||
| 206 | + value = getBasic(key, field, config); | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + return (T) value; | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + @SuppressWarnings("unchecked") | ||
| 213 | + private <T> Object getMap(Key key, Field field, org.apache.commons.configuration.Configuration config) { | ||
| 214 | + Map<String, Object> value = null; | ||
| 215 | + | ||
| 216 | + String regexp = "^(" + key.getPrefix() + ")((.+)\\.)?(" + key.getName() + ")$"; | ||
| 217 | + Pattern pattern = Pattern.compile(regexp); | ||
| 218 | + Matcher matcher; | ||
| 219 | + | ||
| 220 | + String iterKey; | ||
| 221 | + String mapKey; | ||
| 222 | + String confKey; | ||
| 223 | + | ||
| 224 | + for (Iterator<String> iter = config.getKeys(); iter.hasNext();) { | ||
| 225 | + iterKey = iter.next(); | ||
| 226 | + matcher = pattern.matcher(iterKey); | ||
| 227 | + | ||
| 228 | + if (matcher.matches()) { | ||
| 229 | + confKey = matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2)) + matcher.group(4); | ||
| 230 | + | ||
| 231 | + if (value == null) { | ||
| 232 | + value = new HashMap<String, Object>(); | ||
| 233 | + } | ||
| 234 | + | ||
| 235 | + mapKey = matcher.group(3) == null ? "default" : matcher.group(3); | ||
| 236 | + value.put(mapKey, config.getProperty(confKey)); | ||
| 237 | + } | ||
| 238 | + } | ||
| 239 | + | ||
| 240 | + return value; | ||
| 241 | + } | ||
| 242 | + | ||
| 243 | + private <T> Object getArray(Key key, Field field, org.apache.commons.configuration.Configuration config) { | ||
| 244 | + Object value = null; | ||
| 245 | + | ||
| 246 | + Class<?> fieldClass = (Class<?>) field.getType(); | ||
| 247 | + | ||
| 248 | + try { | ||
| 249 | + Method method; | ||
| 250 | + String methodName = "get"; | ||
| 251 | + | ||
| 252 | + methodName += Strings.firstToUpper(fieldClass.getSimpleName()); | ||
| 253 | + methodName = Strings.removeChars(methodName, '[', ']'); | ||
| 254 | + | ||
| 255 | + methodName += "Array"; | ||
| 256 | + | ||
| 257 | + method = config.getClass().getMethod(methodName, String.class); | ||
| 258 | + value = method.invoke(config, key.toString()); | ||
| 259 | + | ||
| 260 | + } catch (Throwable cause) { | ||
| 261 | + throw new ConfigurationException(getBundle().getString("error-converting-to-type", fieldClass.getName()), | ||
| 262 | + cause); | ||
| 263 | + } | ||
| 264 | + | ||
| 265 | + return value; | ||
| 266 | + } | ||
| 267 | + | ||
| 268 | + private <T> Object getBasic(Key key, Field field, org.apache.commons.configuration.Configuration config) { | ||
| 269 | + Object value = null; | ||
| 270 | + | ||
| 271 | + Class<?> fieldClass = (Class<?>) field.getType(); | ||
| 272 | + | ||
| 273 | + try { | ||
| 274 | + Method method; | ||
| 275 | + String methodName = "get"; | ||
| 276 | + | ||
| 277 | + methodName += discoveryGenericType(field); | ||
| 278 | + methodName += Strings.firstToUpper(fieldClass.getSimpleName()); | ||
| 279 | + | ||
| 280 | + if (!fieldClass.isPrimitive()) { | ||
| 281 | + method = config.getClass().getMethod(methodName, String.class, fieldClass); | ||
| 282 | + value = method.invoke(config, key.toString(), null); | ||
| 283 | + | ||
| 284 | + } else if (config.containsKey(key.toString())) { | ||
| 285 | + method = config.getClass().getMethod(methodName, String.class); | ||
| 286 | + value = method.invoke(config, key.toString()); | ||
| 287 | + } | ||
| 288 | + | ||
| 289 | + } catch (Throwable cause) { | ||
| 290 | + throw new ConfigurationException(getBundle().getString("error-converting-to-type", fieldClass.getName()), | ||
| 291 | + cause); | ||
| 292 | + } | ||
| 293 | + | ||
| 294 | + return value; | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | + private <T> Object getClass(Key key, Field field, org.apache.commons.configuration.Configuration config) { | ||
| 298 | + Object value = null; | ||
| 299 | + | ||
| 300 | + try { | ||
| 301 | + String canonicalName = config.getString(key.toString()); | ||
| 302 | + | ||
| 303 | + if (canonicalName != null) { | ||
| 304 | + ClassLoader classLoader = getClassLoaderForClass(canonicalName); | ||
| 305 | + value = Class.forName(canonicalName, true, classLoader); | ||
| 306 | + } | ||
| 307 | + | ||
| 308 | + } catch (Exception cause) { | ||
| 309 | + // TODO Lançar a mensagem correta | ||
| 310 | + throw new ConfigurationException(null, cause); | ||
| 311 | + } | ||
| 312 | + | ||
| 313 | + return value; | ||
| 314 | + } | ||
| 315 | + | ||
| 316 | + /** | ||
| 317 | + * Discovery the Generic's type. for example: the generic's type of List<Integer> list is an Integer type | ||
| 318 | + * | ||
| 319 | + * @param field | ||
| 320 | + * @return | ||
| 321 | + */ | ||
| 322 | + private String discoveryGenericType(Field field) { | ||
| 323 | + | ||
| 324 | + Type genericFieldType = field.getGenericType(); | ||
| 325 | + | ||
| 326 | + if (genericFieldType instanceof ParameterizedType) { | ||
| 327 | + ParameterizedType type = (ParameterizedType) genericFieldType; | ||
| 328 | + Type[] fieldArgumentTypes = type.getActualTypeArguments(); | ||
| 329 | + for (Type fieldArgumentType : fieldArgumentTypes) { | ||
| 330 | + @SuppressWarnings("rawtypes") | ||
| 331 | + Class fieldArgumentClass = (Class) fieldArgumentType; | ||
| 332 | + | ||
| 333 | + if ("String".equals(fieldArgumentClass.getSimpleName())) { | ||
| 334 | + return ""; | ||
| 335 | + } | ||
| 336 | + | ||
| 337 | + return fieldArgumentClass.getSimpleName(); | ||
| 338 | + } | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + return ""; | ||
| 342 | + } | ||
| 343 | + | ||
| 344 | + private Object getProperty(Key key, org.apache.commons.configuration.Configuration config) { | ||
| 345 | + Object value = null; | ||
| 346 | + | ||
| 347 | + @SuppressWarnings("unchecked") | ||
| 348 | + Iterator<String> iterator = config.getKeys(key.toString()); | ||
| 349 | + if (iterator.hasNext()) { | ||
| 350 | + Properties props = new Properties(); | ||
| 351 | + | ||
| 352 | + while (iterator.hasNext()) { | ||
| 353 | + String fullKey = iterator.next(); | ||
| 354 | + String prefix = key.toString() + "."; | ||
| 355 | + String unprefixedKey = fullKey.substring(prefix.length()); | ||
| 356 | + props.put(unprefixedKey, config.getString(fullKey)); | ||
| 357 | + } | ||
| 358 | + | ||
| 359 | + value = props; | ||
| 360 | + } | ||
| 361 | + | ||
| 362 | + return value; | ||
| 363 | + } | ||
| 364 | + | ||
| 365 | + public static ClassLoader getClassLoaderForClass(final String canonicalName) throws FileNotFoundException { | ||
| 366 | + return getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class"); | ||
| 367 | + } | ||
| 368 | + | ||
| 369 | + public static ClassLoader getClassLoaderForResource(final String resource) throws FileNotFoundException { | ||
| 370 | + final String stripped = resource.startsWith("/") ? resource.substring(1) : resource; | ||
| 371 | + | ||
| 372 | + URL url = null; | ||
| 373 | + ClassLoader result = Thread.currentThread().getContextClassLoader(); | ||
| 374 | + | ||
| 375 | + if (result != null) { | ||
| 376 | + url = result.getResource(stripped); | ||
| 377 | + } | ||
| 378 | + | ||
| 379 | + if (url == null) { | ||
| 380 | + result = ConfigurationLoader.class.getClassLoader(); | ||
| 381 | + url = ConfigurationLoader.class.getClassLoader().getResource(stripped); | ||
| 382 | + } | ||
| 383 | + | ||
| 384 | + if (url == null) { | ||
| 385 | + result = null; | ||
| 386 | + } | ||
| 387 | + | ||
| 388 | + return result; | ||
| 389 | + } | ||
| 390 | + | ||
| 391 | + public static URL getResourceAsURL(final String resource) throws FileNotFoundException { | ||
| 392 | + ClassLoader classLoader = getClassLoaderForResource(resource); | ||
| 393 | + return classLoader != null ? classLoader.getResource(resource) : null; | ||
| 394 | + } | ||
| 395 | + | ||
| 396 | + private ResourceBundle getBundle() { | ||
| 397 | + if (bundle == null) { | ||
| 398 | + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); | ||
| 399 | + } | ||
| 400 | + | ||
| 401 | + return bundle; | ||
| 402 | + } | ||
| 403 | + | ||
| 404 | + private Logger getLogger() { | ||
| 405 | + if (logger == null) { | ||
| 406 | + logger = LoggerProducer.create(ConfigurationLoader.class); | ||
| 407 | + } | ||
| 408 | + | ||
| 409 | + return logger; | ||
| 410 | + } | ||
| 411 | + | ||
| 412 | + private final class Key { | ||
| 413 | + | ||
| 414 | + private String prefix; | ||
| 415 | + | ||
| 416 | + private String name; | ||
| 417 | + | ||
| 418 | + private String key; | ||
| 419 | + | ||
| 420 | + private Key(final Field field, final Class<?> type, final org.apache.commons.configuration.Configuration config) { | ||
| 421 | + | ||
| 422 | + this.prefix = type.getAnnotation(Configuration.class).prefix(); | ||
| 423 | + if (this.prefix == null) { | ||
| 424 | + this.prefix = ""; | ||
| 425 | + } | ||
| 426 | + | ||
| 427 | + if (field.isAnnotationPresent(Name.class)) { | ||
| 428 | + this.name = getNameByAnnotation(field); | ||
| 429 | + } else { | ||
| 430 | + this.name = getNameByField(field); | ||
| 431 | + } | ||
| 432 | + | ||
| 433 | + this.key = this.prefix + this.name; | ||
| 434 | + | ||
| 435 | + if (!config.containsKey(this.key)) { | ||
| 436 | + getLogger().debug(getBundle().getString("key-not-found", this.key)); | ||
| 437 | + } | ||
| 438 | + } | ||
| 439 | + | ||
| 440 | + private String getNameByAnnotation(Field field) { | ||
| 441 | + String result; | ||
| 442 | + | ||
| 443 | + Name nameAnnotation = field.getAnnotation(Name.class); | ||
| 444 | + if (Strings.isEmpty(nameAnnotation.value())) { | ||
| 445 | + throw new ConfigurationException(getBundle().getString("configuration-name-attribute-cant-be-empty")); | ||
| 446 | + } else { | ||
| 447 | + result = nameAnnotation.value(); | ||
| 448 | + } | ||
| 449 | + | ||
| 450 | + return result; | ||
| 451 | + } | ||
| 452 | + | ||
| 453 | + private String getNameByField(Field field) { | ||
| 454 | + return field.getName(); | ||
| 455 | + } | ||
| 456 | + | ||
| 457 | + public String getPrefix() { | ||
| 458 | + return prefix; | ||
| 459 | + } | ||
| 460 | + | ||
| 461 | + public String getName() { | ||
| 462 | + return name; | ||
| 463 | + } | ||
| 464 | + | ||
| 465 | + @Override | ||
| 466 | + public String toString() { | ||
| 467 | + return this.key; | ||
| 468 | + } | ||
| 469 | + } | ||
| 470 | +} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationMapValueExtractor.java
0 → 100644
| @@ -0,0 +1,85 @@ | @@ -0,0 +1,85 @@ | ||
| 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.internal.configuration; | ||
| 38 | + | ||
| 39 | +import java.lang.reflect.Field; | ||
| 40 | +import java.util.HashMap; | ||
| 41 | +import java.util.Iterator; | ||
| 42 | +import java.util.Map; | ||
| 43 | +import java.util.regex.Matcher; | ||
| 44 | +import java.util.regex.Pattern; | ||
| 45 | + | ||
| 46 | +import org.apache.commons.configuration.DataConfiguration; | ||
| 47 | + | ||
| 48 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 49 | + | ||
| 50 | +public class ConfigurationMapValueExtractor implements ConfigurationValueExtractor { | ||
| 51 | + | ||
| 52 | + @Override | ||
| 53 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
| 54 | + Object defaultValue) { | ||
| 55 | + @SuppressWarnings("unchecked") | ||
| 56 | + Map<String, Object> value = (Map<String, Object>) defaultValue; | ||
| 57 | + | ||
| 58 | + String regexp = "^(" + prefix + ")((.+)\\.)?(" + key + ")$"; | ||
| 59 | + Pattern pattern = Pattern.compile(regexp); | ||
| 60 | + | ||
| 61 | + for (Iterator<String> iter = configuration.getKeys(); iter.hasNext();) { | ||
| 62 | + String iterKey = iter.next(); | ||
| 63 | + Matcher matcher = pattern.matcher(iterKey); | ||
| 64 | + | ||
| 65 | + if (matcher.matches()) { | ||
| 66 | + String confKey = matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2)) | ||
| 67 | + + matcher.group(4); | ||
| 68 | + | ||
| 69 | + if (value == null) { | ||
| 70 | + value = new HashMap<String, Object>(); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + String mapKey = matcher.group(3) == null ? "default" : matcher.group(3); | ||
| 74 | + value.put(mapKey, configuration.getString(confKey)); | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + return value; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + @Override | ||
| 82 | + public boolean isSupported(Field field) { | ||
| 83 | + return field.getType() == Map.class; | ||
| 84 | + } | ||
| 85 | +} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationPrimitiveOrWrapperValueExtractor.java
0 → 100644
| @@ -0,0 +1,85 @@ | @@ -0,0 +1,85 @@ | ||
| 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.internal.configuration; | ||
| 38 | + | ||
| 39 | +import java.lang.reflect.Field; | ||
| 40 | +import java.util.HashSet; | ||
| 41 | +import java.util.Set; | ||
| 42 | + | ||
| 43 | +import org.apache.commons.configuration.ConversionException; | ||
| 44 | +import org.apache.commons.configuration.DataConfiguration; | ||
| 45 | +import org.apache.commons.lang.ClassUtils; | ||
| 46 | + | ||
| 47 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 48 | + | ||
| 49 | +public class ConfigurationPrimitiveOrWrapperValueExtractor implements ConfigurationValueExtractor { | ||
| 50 | + | ||
| 51 | + private static final Set<Object> wrappers = new HashSet<Object>(); | ||
| 52 | + | ||
| 53 | + static { | ||
| 54 | + wrappers.add(Boolean.class); | ||
| 55 | + wrappers.add(Byte.class); | ||
| 56 | + wrappers.add(Character.class); | ||
| 57 | + wrappers.add(Short.class); | ||
| 58 | + wrappers.add(Integer.class); | ||
| 59 | + wrappers.add(Long.class); | ||
| 60 | + wrappers.add(Double.class); | ||
| 61 | + wrappers.add(Float.class); | ||
| 62 | + wrappers.add(Void.TYPE); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + @Override | ||
| 66 | + @SuppressWarnings("unchecked") | ||
| 67 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
| 68 | + Object defaultValue) { | ||
| 69 | + Object value; | ||
| 70 | + | ||
| 71 | + try { | ||
| 72 | + value = configuration.get(ClassUtils.primitiveToWrapper(field.getType()), prefix + key, defaultValue); | ||
| 73 | + | ||
| 74 | + } catch (ConversionException cause) { | ||
| 75 | + value = defaultValue; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + return value; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + @Override | ||
| 82 | + public boolean isSupported(Field field) { | ||
| 83 | + return field.getType().isPrimitive() || wrappers.contains(field.getType()); | ||
| 84 | + } | ||
| 85 | +} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationStringValueExtractor.java
0 → 100644
| @@ -0,0 +1,57 @@ | @@ -0,0 +1,57 @@ | ||
| 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.internal.configuration; | ||
| 38 | + | ||
| 39 | +import java.lang.reflect.Field; | ||
| 40 | + | ||
| 41 | +import org.apache.commons.configuration.DataConfiguration; | ||
| 42 | + | ||
| 43 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 44 | + | ||
| 45 | +public class ConfigurationStringValueExtractor implements ConfigurationValueExtractor { | ||
| 46 | + | ||
| 47 | + @Override | ||
| 48 | + public Object getValue(String prefix, String key, Field field, DataConfiguration configuration, | ||
| 49 | + Object defaultValue) { | ||
| 50 | + return configuration.getString(prefix + key, (String) defaultValue); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @Override | ||
| 54 | + public boolean isSupported(Field field) { | ||
| 55 | + return field.getType() == String.class; | ||
| 56 | + } | ||
| 57 | +} |
impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/AbstractConfigurationTest.java
| @@ -48,7 +48,12 @@ import br.gov.frameworkdemoiselle.annotation.Ignore; | @@ -48,7 +48,12 @@ import br.gov.frameworkdemoiselle.annotation.Ignore; | ||
| 48 | import br.gov.frameworkdemoiselle.annotation.Name; | 48 | import br.gov.frameworkdemoiselle.annotation.Name; |
| 49 | import br.gov.frameworkdemoiselle.internal.bootstrap.ConfigurationBootstrap; | 49 | import br.gov.frameworkdemoiselle.internal.bootstrap.ConfigurationBootstrap; |
| 50 | import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; | 50 | import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; |
| 51 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationArrayValueExtractor; | ||
| 52 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationClassValueExtractor; | ||
| 51 | import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | 53 | import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; |
| 54 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationMapValueExtractor; | ||
| 55 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationPrimitiveOrWrapperValueExtractor; | ||
| 56 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationStringValueExtractor; | ||
| 52 | import br.gov.frameworkdemoiselle.internal.producer.LocaleProducer; | 57 | import br.gov.frameworkdemoiselle.internal.producer.LocaleProducer; |
| 53 | import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; | 58 | import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; |
| 54 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | 59 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; |
| @@ -65,6 +70,12 @@ public abstract class AbstractConfigurationTest { | @@ -65,6 +70,12 @@ public abstract class AbstractConfigurationTest { | ||
| 65 | result.add(CoreBootstrap.class); | 70 | result.add(CoreBootstrap.class); |
| 66 | result.add(ConfigurationBootstrap.class); | 71 | result.add(ConfigurationBootstrap.class); |
| 67 | result.add(ConfigurationLoader.class); | 72 | result.add(ConfigurationLoader.class); |
| 73 | + result.add(ConfigurationValueExtractor.class); | ||
| 74 | + result.add(ConfigurationArrayValueExtractor.class); | ||
| 75 | + result.add(ConfigurationMapValueExtractor.class); | ||
| 76 | + result.add(ConfigurationClassValueExtractor.class); | ||
| 77 | + result.add(ConfigurationStringValueExtractor.class); | ||
| 78 | + result.add(ConfigurationPrimitiveOrWrapperValueExtractor.class); | ||
| 68 | result.add(Beans.class); | 79 | result.add(Beans.class); |
| 69 | result.add(ResourceBundleProducer.class); | 80 | result.add(ResourceBundleProducer.class); |
| 70 | result.add(LoggerProducer.class); | 81 | result.add(LoggerProducer.class); |