Commit 291648c405d37e51e2ffc5a7664d8e329cf40fd3

Authored by Dancovich
1 parent 08dd8f32
Exists in master

Refatorado extrator de configuração em Map para inverter a ordem dos

elementos. Agora o formato é prefixo.nome_do_mapa.chave=valor
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationMapValueExtractor.java
... ... @@ -50,12 +50,6 @@ import org.apache.commons.configuration.Configuration;
50 50 import br.gov.frameworkdemoiselle.annotation.Priority;
51 51 import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor;
52 52  
53   -/**
54   - *
55   - * TODO Adicionar verificação da existência de duas ou mais configurações JDBC com mesmo nome. Lançar INFO ou Exceção.
56   - *
57   - */
58   -
59 53 @Priority(L2_PRIORITY)
60 54 public class ConfigurationMapValueExtractor implements ConfigurationValueExtractor {
61 55  
... ... @@ -63,7 +57,7 @@ public class ConfigurationMapValueExtractor implements ConfigurationValueExtract
63 57 public Object getValue(String prefix, String key, Field field, Configuration configuration) throws Exception {
64 58 Map<String, Object> value = null;
65 59  
66   - String regexp = "^(" + prefix + ")((.+)\\.)?(" + key + ")$";
  60 + String regexp = "^(" + prefix + ")(" + key + ")(\\.(\\w+))?$";
67 61 Pattern pattern = Pattern.compile(regexp);
68 62  
69 63 for (Iterator<String> iter = configuration.getKeys(); iter.hasNext();) {
... ... @@ -71,14 +65,16 @@ public class ConfigurationMapValueExtractor implements ConfigurationValueExtract
71 65 Matcher matcher = pattern.matcher(iterKey);
72 66  
73 67 if (matcher.matches()) {
74   - String confKey = matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2))
75   - + matcher.group(4);
  68 + String confKey = matcher.group(1) + matcher.group(2) + ( matcher.group(3)!=null ? matcher.group(3) : "" );
  69 +
  70 + /*matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2))
  71 + + matcher.group(4);*/
76 72  
77 73 if (value == null) {
78 74 value = new HashMap<String, Object>();
79 75 }
80 76  
81   - String mapKey = matcher.group(3) == null ? "default" : matcher.group(3);
  77 + String mapKey = matcher.group(4) == null ? "default" : matcher.group(4);
82 78 value.put(mapKey, configuration.getString(confKey));
83 79 }
84 80 }
... ...
impl/core/src/test/java/configuration/field/map/PropertiesMapFieldConfig.java
... ... @@ -39,6 +39,6 @@ package configuration.field.map;
39 39 import static br.gov.frameworkdemoiselle.configuration.ConfigType.PROPERTIES;
40 40 import br.gov.frameworkdemoiselle.configuration.Configuration;
41 41  
42   -@Configuration(type = PROPERTIES)
  42 +@Configuration(type = PROPERTIES,prefix="configuration.test")
43 43 public class PropertiesMapFieldConfig extends AbstractMapFieldConfig {
44 44 }
... ...
impl/core/src/test/resources/configuration/field/map/demoiselle.properties
... ... @@ -33,9 +33,9 @@
33 33 # ou escreva para a Fundação do Software Livre (FSF) Inc.,
34 34 # 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
35 35  
36   -item1.stringWithDefinedKeyMap=demoiselle
37   -item2.stringWithDefinedKeyMap=framework
38   -item1.emptyValueMap=
39   -item2.emptyValueMap=
  36 +configuration.test.stringWithDefinedKeyMap.item1=demoiselle
  37 +configuration.test.stringWithDefinedKeyMap.item2=framework
  38 +configuration.test.emptyValueMap.item1=
  39 +configuration.test.emptyValueMap.item2=
40 40  
41   -stringWithUndefinedKeyMap=undefined
  41 +configuration.test.stringWithUndefinedKeyMap=undefined
... ...
impl/core/src/test/resources/configuration/field/map/demoiselle.xml
... ... @@ -36,17 +36,15 @@
36 36 -->
37 37  
38 38 <configuration>
39   - <item1>
40   - <stringWithDefinedKeyMap>demoiselle</stringWithDefinedKeyMap>
41   - </item1>
42   - <item2>
43   - <stringWithDefinedKeyMap>framework</stringWithDefinedKeyMap>
44   - </item2>
45   - <stringWithUndefinedKeyMap>undefined</stringWithUndefinedKeyMap>
46   - <item1>
47   - <emptyValueMap></emptyValueMap>
48   - </item1>
49   - <item2>
50   - <emptyValueMap></emptyValueMap>
51   - </item2>
  39 + <stringWithDefinedKeyMap>
  40 + <item1>demoiselle</item1>
  41 + <item2>framework</item2>
  42 + </stringWithDefinedKeyMap>
  43 +
  44 + <stringWithUndefinedKeyMap>undefined</stringWithUndefinedKeyMap>
  45 +
  46 + <emptyValueMap>
  47 + <item1></item1>
  48 + <item2></item2>
  49 + </emptyValueMap>
52 50 </configuration>
... ...
impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/JDBCConfig.java
... ... @@ -37,10 +37,10 @@
37 37 package br.gov.frameworkdemoiselle.internal.configuration;
38 38  
39 39 import java.io.Serializable;
40   -import java.util.Map;
41 40  
42 41 import br.gov.frameworkdemoiselle.annotation.Name;
43 42 import br.gov.frameworkdemoiselle.configuration.Configuration;
  43 +
44 44 /**
45 45 * Provide used to access the configurations of the JDBC connection
46 46 *
... ... @@ -56,41 +56,41 @@ public class JDBCConfig implements Serializable {
56 56 private String defaultDataSourceName;
57 57  
58 58 @Name("jndi.name")
59   - private Map<String, String> jndiName;
  59 + private JDBCConfigurationStore jndiName;
60 60  
61 61 @Name("driver.class")
62   - private Map<String, String> driverClass;
  62 + private JDBCConfigurationStore driverClass;
63 63  
64 64 @Name("url")
65   - private Map<String, String> url;
  65 + private JDBCConfigurationStore url;
66 66  
67 67 @Name("username")
68   - private Map<String, String> username;
  68 + private JDBCConfigurationStore username;
69 69  
70 70 @Name("password")
71   - private Map<String, String> password;
  71 + private JDBCConfigurationStore password;
72 72  
73 73 public String getDefaultDataSourceName() {
74 74 return defaultDataSourceName;
75 75 }
76 76  
77   - public Map<String, String> getJndiName() {
  77 + public JDBCConfigurationStore getJndiName() {
78 78 return jndiName;
79 79 }
80 80  
81   - public Map<String, String> getDriverClass() {
  81 + public JDBCConfigurationStore getDriverClass() {
82 82 return driverClass;
83 83 }
84 84  
85   - public Map<String, String> getUrl() {
  85 + public JDBCConfigurationStore getUrl() {
86 86 return url;
87 87 }
88 88  
89   - public Map<String, String> getUsername() {
  89 + public JDBCConfigurationStore getUsername() {
90 90 return username;
91 91 }
92 92  
93   - public Map<String, String> getPassword() {
  93 + public JDBCConfigurationStore getPassword() {
94 94 return password;
95 95 }
96 96 }
... ...
impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/JDBCConfigValueExtractor.java 0 → 100644
... ... @@ -0,0 +1,90 @@
  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 static br.gov.frameworkdemoiselle.annotation.Priority.L2_PRIORITY;
  40 +
  41 +import java.lang.reflect.Field;
  42 +import java.util.Iterator;
  43 +import java.util.regex.Matcher;
  44 +import java.util.regex.Pattern;
  45 +
  46 +import org.apache.commons.configuration.Configuration;
  47 +
  48 +import br.gov.frameworkdemoiselle.annotation.Priority;
  49 +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor;
  50 +
  51 +/**
  52 + *
  53 + * TODO Adicionar verificação da existência de duas ou mais configurações JDBC com mesmo nome. Lançar INFO ou Exceção.
  54 + *
  55 + */
  56 +@Priority(L2_PRIORITY)
  57 +public class JDBCConfigValueExtractor implements ConfigurationValueExtractor {
  58 +
  59 + @Override
  60 + public Object getValue(String prefix, String key, Field field, Configuration configuration) throws Exception {
  61 + JDBCConfigurationStore value = null;
  62 +
  63 + String regexp = "^(" + prefix + ")((.+)\\.)?(" + key + ")$";
  64 + Pattern pattern = Pattern.compile(regexp);
  65 +
  66 + for (Iterator<String> iter = configuration.getKeys(); iter.hasNext();) {
  67 + String iterKey = iter.next();
  68 + Matcher matcher = pattern.matcher(iterKey);
  69 +
  70 + if (matcher.matches()) {
  71 + String confKey = matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2))
  72 + + matcher.group(4);
  73 +
  74 + if (value == null) {
  75 + value = new JDBCConfigurationStore();
  76 + }
  77 +
  78 + String mapKey = matcher.group(3) == null ? "default" : matcher.group(3);
  79 + value.put(mapKey, configuration.getString(confKey));
  80 + }
  81 + }
  82 +
  83 + return value;
  84 + }
  85 +
  86 + @Override
  87 + public boolean isSupported(Field field) {
  88 + return field.getType() == JDBCConfigurationStore.class;
  89 + }
  90 +}
... ...
impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/JDBCConfigurationStore.java 0 → 100644
... ... @@ -0,0 +1,80 @@
  1 +package br.gov.frameworkdemoiselle.internal.configuration;
  2 +
  3 +import java.io.Serializable;
  4 +import java.util.Collection;
  5 +import java.util.HashMap;
  6 +import java.util.Map;
  7 +import java.util.Map.Entry;
  8 +import java.util.Set;
  9 +
  10 +
  11 +public class JDBCConfigurationStore implements Cloneable, Serializable {
  12 +
  13 + private static final long serialVersionUID = 1L;
  14 +
  15 + private HashMap<String, String> properties = new HashMap<String, String>();
  16 +
  17 + public int size() {
  18 + return properties.size();
  19 + }
  20 +
  21 + public boolean isEmpty() {
  22 + return properties.isEmpty();
  23 + }
  24 +
  25 + public String get(Object key) {
  26 + return properties.get(key);
  27 + }
  28 +
  29 + public boolean equals(Object o) {
  30 + return properties.equals(o);
  31 + }
  32 +
  33 + public boolean containsKey(Object key) {
  34 + return properties.containsKey(key);
  35 + }
  36 +
  37 + public String put(String key, String value) {
  38 + return properties.put(key, value);
  39 + }
  40 +
  41 + public int hashCode() {
  42 + return properties.hashCode();
  43 + }
  44 +
  45 + public String toString() {
  46 + return properties.toString();
  47 + }
  48 +
  49 + public void putAll(Map<? extends String, ? extends String> m) {
  50 + properties.putAll(m);
  51 + }
  52 +
  53 + public String remove(Object key) {
  54 + return properties.remove(key);
  55 + }
  56 +
  57 + public void clear() {
  58 + properties.clear();
  59 + }
  60 +
  61 + public boolean containsValue(Object value) {
  62 + return properties.containsValue(value);
  63 + }
  64 +
  65 + public Object clone() {
  66 + return properties.clone();
  67 + }
  68 +
  69 + public Set<String> keySet() {
  70 + return properties.keySet();
  71 + }
  72 +
  73 + public Collection<String> values() {
  74 + return properties.values();
  75 + }
  76 +
  77 + public Set<Entry<String, String>> entrySet() {
  78 + return properties.entrySet();
  79 + }
  80 +}
... ...