From 6a4fb17d1d1a8edeefd39482dd35fa70b7dfb809 Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Thu, 4 Apr 2013 09:33:42 -0300 Subject: [PATCH] Melhorias na implementação do CofigurationLoader --- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java | 10 +++++----- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java | 57 +++++++++++++++++++++++---------------------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderBackup.java | 470 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3 files changed, 28 insertions(+), 509 deletions(-) delete mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderBackup.java diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java index 3c757ff..17ae584 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java @@ -1,8 +1,8 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; -import java.util.List; +import java.util.HashSet; import javax.enterprise.event.Observes; import javax.enterprise.inject.spi.AnnotatedType; @@ -17,7 +17,7 @@ public abstract class AbstractStrategyBootstrap implements Extension { private Class strategyClass; - private List> cache; + private Collection> cache; protected abstract Logger getLogger(); @@ -29,9 +29,9 @@ public abstract class AbstractStrategyBootstrap implements Extension { return this.strategyClass; } - public List> getCache() { + public Collection> getCache() { if (this.cache == null) { - this.cache = Collections.synchronizedList(new ArrayList>()); + this.cache = Collections.synchronizedSet(new HashSet>()); } return this.cache; 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 d7b31e3..6391727 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 @@ -43,9 +43,7 @@ import java.io.Serializable; import java.lang.reflect.Field; import java.util.Collection; import java.util.HashSet; -import java.util.Set; -import javax.inject.Inject; import javax.validation.constraints.NotNull; import org.apache.commons.configuration.AbstractConfiguration; @@ -85,33 +83,32 @@ public class ConfigurationLoader implements Serializable { private org.apache.commons.configuration.Configuration configuration; - private Set fields; - - private Set extractors; - - @Inject - private ConfigurationBootstrap bootstrap; + private Collection fields; public void load(Object object) throws ConfigurationException { this.object = object; + loadFields(); validateFields(); loadType(); loadResource(); loadConfiguration(); - loadExtractors(); if (this.configuration != null) { loadPrefix(); - loadFields(); + loadValues(); } validateValues(); } + private void loadFields() { + this.fields = Reflections.getNonStaticFields(this.object.getClass()); + } + private void validateFields() { - for (Field field : getFields()) { + for (Field field : this.fields) { validateField(field); } } @@ -165,14 +162,6 @@ public class ConfigurationLoader implements Serializable { this.configuration = conf; } - private void loadExtractors() { - this.extractors = new HashSet(); - - for (Class extractorClass : this.bootstrap.getCache()) { - this.extractors.add(Beans.getReference(extractorClass)); - } - } - private void loadPrefix() { String prefix = this.object.getClass().getAnnotation(Configuration.class).prefix(); @@ -186,21 +175,13 @@ public class ConfigurationLoader implements Serializable { this.prefix = prefix; } - private void loadFields() { - for (Field field : getFields()) { - loadField(field); - } - } - - private Set getFields() { - if (this.fields == null) { - this.fields = new HashSet(Reflections.getNonStaticFields(this.object.getClass())); + private void loadValues() { + for (Field field : this.fields) { + loadValue(field); } - - return this.fields; } - private void loadField(Field field) { + private void loadValue(Field field) { if (hasIgnore(field)) { return; } @@ -212,9 +193,17 @@ public class ConfigurationLoader implements Serializable { } private Object getValue(Field field, Class type, String key, Object defaultValue) { + ConfigurationValueExtractor extractor = getValueExtractor(field); + return extractor.getValue(this.prefix, key, field, this.configuration, defaultValue); + } + + private ConfigurationValueExtractor getValueExtractor(Field field) { Collection candidates = new HashSet(); + ConfigurationBootstrap bootstrap = Beans.getReference(ConfigurationBootstrap.class); + + for (Class extractorClass : bootstrap.getCache()) { + ConfigurationValueExtractor extractor = Beans.getReference(extractorClass); - for (ConfigurationValueExtractor extractor : this.extractors) { if (extractor.isSupported(field)) { candidates.add(extractor); } @@ -228,7 +217,7 @@ public class ConfigurationLoader implements Serializable { // um extrator personalizado. } - return elected.getValue(this.prefix, key, field, configuration, defaultValue); + return elected; } private String getKey(Field field) { @@ -248,7 +237,7 @@ public class ConfigurationLoader implements Serializable { } private void validateValues() { - for (Field field : getFields()) { + for (Field field : this.fields) { validateValue(field); } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderBackup.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderBackup.java deleted file mode 100644 index d000408..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderBackup.java +++ /dev/null @@ -1,470 +0,0 @@ -/* -/* - * 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.internal.configuration; - -import java.io.FileNotFoundException; -import java.io.Serializable; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.net.URL; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.validation.constraints.NotNull; - -import org.apache.commons.configuration.DataConfiguration; -import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.commons.configuration.SystemConfiguration; -import org.apache.commons.configuration.XMLConfiguration; -import org.slf4j.Logger; - -import br.gov.frameworkdemoiselle.annotation.Ignore; -import br.gov.frameworkdemoiselle.annotation.Name; -import br.gov.frameworkdemoiselle.configuration.ConfigType; -import br.gov.frameworkdemoiselle.configuration.Configuration; -import br.gov.frameworkdemoiselle.configuration.ConfigurationException; -import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; -import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; -import br.gov.frameworkdemoiselle.util.Reflections; -import br.gov.frameworkdemoiselle.util.ResourceBundle; -import br.gov.frameworkdemoiselle.util.Strings; - -/** - * This component loads a config class annotated with {@link Configuration} by filling its attributes with {@link Param} - * according to a {@link ConfigType}. - * - * @author SERPRO - */ -public class ConfigurationLoaderBackup implements Serializable { - - private static final long serialVersionUID = 1L; - - private ResourceBundle bundle; - - private Logger logger; - - /** - * Loads a config class filling it with the corresponding values. - * - * @param object - * config object - * @throws ConfigurationException - */ - public void load(Object object) throws ConfigurationException { - Class config = object.getClass(); - - getLogger().debug(getBundle().getString("loading-configuration-class", config.getName())); - - for (Field field : Reflections.getNonStaticFields(config)) { - loadField(field, object, config); - } - } - - private void loadField(Field field, Object object, Class clazz) { - if (!field.isAnnotationPresent(Ignore.class) && clazz.isAnnotationPresent(Configuration.class)) { - String resource = clazz.getAnnotation(Configuration.class).resource(); - ConfigType type = clazz.getAnnotation(Configuration.class).type(); - org.apache.commons.configuration.Configuration config = getConfiguration(resource, type); - - if (config != null) { - Key key = new Key(field, clazz, config); - Object value = getValue(key, field, config); - validate(field, key, value, resource); - setValue(field, key, object, value); - } - } - } - - private void setValue(Field field, Key key, Object object, Object value) { - if (value != null) { - Reflections.setFieldValue(field, object, value); - getLogger().debug( - getBundle().getString("configuration-field-loaded", key.toString(), field.getName(), value)); - } - } - - private void validate(Field field, Key key, Object value, String resource) { - if (field.isAnnotationPresent(NotNull.class) && value == null) { - throw new ConfigurationException(getBundle().getString("configuration-attribute-is-mandatory", - key.toString(), resource)); - } - } - - /** - * Returns the configuration class according to specified resource name and configuration type. - * - * @param resource - * @param type - * @return a configuration - */ - private org.apache.commons.configuration.Configuration getConfiguration(String resource, ConfigType type) { - org.apache.commons.configuration.Configuration result = null; - - try { - URL url; - - switch (type) { - case SYSTEM: - result = new SystemConfiguration(); - break; - - case PROPERTIES: - url = getResourceAsURL(resource + ".properties"); - - if (url != null) { - result = new DataConfiguration(new PropertiesConfiguration(url)); - } else { - getLogger().warn(getBundle().getString("resource-not-found", resource + ".properties")); - } - - break; - - case XML: - url = getResourceAsURL(resource + ".xml"); - - if (url != null) { - result = new DataConfiguration(new XMLConfiguration(url)); - } else { - getLogger().warn(getBundle().getString("resource-not-found", resource + ".xml")); - } - - break; - - default: - throw new ConfigurationException(getBundle().getString("configuration-type-not-implemented-yet", - type.name())); - } - - } catch (Exception cause) { - throw new ConfigurationException(getBundle().getString("error-creating-configuration-from-resource", - resource), cause); - } - - return result; - } - - @SuppressWarnings("unchecked") - private T getValue(Key key, Field field, org.apache.commons.configuration.Configuration config) { - Object value; - - Class fieldClass = (Class) field.getType(); - - if (fieldClass.isArray()) { - value = getArray(key, field, config); - - } else if (fieldClass.equals(Map.class)) { - value = getMap(key, field, config); - - } else if (fieldClass.equals(Properties.class)) { - value = getProperty(key, config); - - } else if (fieldClass.equals(Class.class)) { - value = getClass(key, field, config); - - } else { - value = getBasic(key, field, config); - } - - return (T) value; - } - - @SuppressWarnings("unchecked") - private Object getMap(Key key, Field field, org.apache.commons.configuration.Configuration config) { - Map value = null; - - String regexp = "^(" + key.getPrefix() + ")((.+)\\.)?(" + key.getName() + ")$"; - Pattern pattern = Pattern.compile(regexp); - Matcher matcher; - - String iterKey; - String mapKey; - String confKey; - - for (Iterator iter = config.getKeys(); iter.hasNext();) { - iterKey = iter.next(); - matcher = pattern.matcher(iterKey); - - if (matcher.matches()) { - confKey = matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2)) + matcher.group(4); - - if (value == null) { - value = new HashMap(); - } - - mapKey = matcher.group(3) == null ? "default" : matcher.group(3); - value.put(mapKey, config.getProperty(confKey)); - } - } - - return value; - } - - private Object getArray(Key key, Field field, org.apache.commons.configuration.Configuration config) { - Object value = null; - - Class fieldClass = (Class) field.getType(); - - try { - Method method; - String methodName = "get"; - - methodName += Strings.firstToUpper(fieldClass.getSimpleName()); - methodName = Strings.removeChars(methodName, '[', ']'); - - methodName += "Array"; - - method = config.getClass().getMethod(methodName, String.class); - value = method.invoke(config, key.toString()); - - } catch (Throwable cause) { - throw new ConfigurationException(getBundle().getString("error-converting-to-type", fieldClass.getName()), - cause); - } - - return value; - } - - private Object getBasic(Key key, Field field, org.apache.commons.configuration.Configuration config) { - Object value = null; - - Class fieldClass = (Class) field.getType(); - - try { - Method method; - String methodName = "get"; - - methodName += discoveryGenericType(field); - methodName += Strings.firstToUpper(fieldClass.getSimpleName()); - - if (!fieldClass.isPrimitive()) { - method = config.getClass().getMethod(methodName, String.class, fieldClass); - value = method.invoke(config, key.toString(), null); - - } else if (config.containsKey(key.toString())) { - method = config.getClass().getMethod(methodName, String.class); - value = method.invoke(config, key.toString()); - } - - } catch (Throwable cause) { - throw new ConfigurationException(getBundle().getString("error-converting-to-type", fieldClass.getName()), - cause); - } - - return value; - } - - private Object getClass(Key key, Field field, org.apache.commons.configuration.Configuration config) { - Object value = null; - - try { - String canonicalName = config.getString(key.toString()); - - if (canonicalName != null) { - ClassLoader classLoader = getClassLoaderForClass(canonicalName); - value = Class.forName(canonicalName, true, classLoader); - } - - } catch (Exception cause) { - // TODO Lançar a mensagem correta - throw new ConfigurationException(null, cause); - } - - return value; - } - - /** - * Discovery the Generic's type. for example: the generic's type of List list is an Integer type - * - * @param field - * @return - */ - private String discoveryGenericType(Field field) { - - Type genericFieldType = field.getGenericType(); - - if (genericFieldType instanceof ParameterizedType) { - ParameterizedType type = (ParameterizedType) genericFieldType; - Type[] fieldArgumentTypes = type.getActualTypeArguments(); - for (Type fieldArgumentType : fieldArgumentTypes) { - @SuppressWarnings("rawtypes") - Class fieldArgumentClass = (Class) fieldArgumentType; - - if ("String".equals(fieldArgumentClass.getSimpleName())) { - return ""; - } - - return fieldArgumentClass.getSimpleName(); - } - } - - return ""; - } - - private Object getProperty(Key key, org.apache.commons.configuration.Configuration config) { - Object value = null; - - @SuppressWarnings("unchecked") - Iterator iterator = config.getKeys(key.toString()); - if (iterator.hasNext()) { - Properties props = new Properties(); - - while (iterator.hasNext()) { - String fullKey = iterator.next(); - String prefix = key.toString() + "."; - String unprefixedKey = fullKey.substring(prefix.length()); - props.put(unprefixedKey, config.getString(fullKey)); - } - - value = props; - } - - return value; - } - - public static ClassLoader getClassLoaderForClass(final String canonicalName) throws FileNotFoundException { - return getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class"); - } - - public static ClassLoader getClassLoaderForResource(final String resource) throws FileNotFoundException { - final String stripped = resource.startsWith("/") ? resource.substring(1) : resource; - - URL url = null; - ClassLoader result = Thread.currentThread().getContextClassLoader(); - - if (result != null) { - url = result.getResource(stripped); - } - - if (url == null) { - result = ConfigurationLoader.class.getClassLoader(); - url = ConfigurationLoader.class.getClassLoader().getResource(stripped); - } - - if (url == null) { - result = null; - } - - return result; - } - - public static URL getResourceAsURL(final String resource) throws FileNotFoundException { - ClassLoader classLoader = getClassLoaderForResource(resource); - return classLoader != null ? classLoader.getResource(resource) : null; - } - - private ResourceBundle getBundle() { - if (bundle == null) { - bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); - } - - return bundle; - } - - private Logger getLogger() { - if (logger == null) { - logger = LoggerProducer.create(ConfigurationLoader.class); - } - - return logger; - } - - private final class Key { - - private String prefix; - - private String name; - - private String key; - - private Key(final Field field, final Class type, final org.apache.commons.configuration.Configuration config) { - - this.prefix = type.getAnnotation(Configuration.class).prefix(); - if (this.prefix == null) { - this.prefix = ""; - } - - if (field.isAnnotationPresent(Name.class)) { - this.name = getNameByAnnotation(field); - } else { - this.name = getNameByField(field); - } - - this.key = this.prefix + this.name; - - if (!config.containsKey(this.key)) { - getLogger().debug(getBundle().getString("key-not-found", this.key)); - } - } - - private String getNameByAnnotation(Field field) { - String result; - - Name nameAnnotation = field.getAnnotation(Name.class); - if (Strings.isEmpty(nameAnnotation.value())) { - throw new ConfigurationException(getBundle().getString("configuration-name-attribute-cant-be-empty")); - } else { - result = nameAnnotation.value(); - } - - return result; - } - - private String getNameByField(Field field) { - return field.getName(); - } - - public String getPrefix() { - return prefix; - } - - public String getName() { - return name; - } - - @Override - public String toString() { - return this.key; - } - } -} -- libgit2 0.21.2