Commit fc7a84cc0f8b0d21fc80ea476cffacbd8018da27
1 parent
0c9ec847
Exists in
master
Movendo as classes de carregamento de configuração para o pacote
correto
Showing
15 changed files
with
633 additions
and
637 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationArrayValueExtractor.java
... | ... | @@ -1,62 +0,0 @@ |
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.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | - | |
41 | -import java.lang.reflect.Field; | |
42 | - | |
43 | -import org.apache.commons.configuration.Configuration; | |
44 | -import org.apache.commons.configuration.DataConfiguration; | |
45 | - | |
46 | -import br.gov.frameworkdemoiselle.annotation.Priority; | |
47 | -import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
48 | - | |
49 | -@Priority(EXTENSIONS_L1_PRIORITY) | |
50 | -public class ConfigurationArrayValueExtractor implements ConfigurationValueExtractor { | |
51 | - | |
52 | - @Override | |
53 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
54 | - return new DataConfiguration(configuration).getArray(field.getType().getComponentType(), prefix + key, | |
55 | - defaultValue); | |
56 | - } | |
57 | - | |
58 | - @Override | |
59 | - public boolean isSupported(Field field) { | |
60 | - return field.getType().isArray(); | |
61 | - } | |
62 | -} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationClassValueExtractor.java
... | ... | @@ -1,76 +0,0 @@ |
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.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | - | |
41 | -import java.lang.reflect.Field; | |
42 | - | |
43 | -import org.apache.commons.configuration.Configuration; | |
44 | - | |
45 | -import br.gov.frameworkdemoiselle.annotation.Priority; | |
46 | -import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | |
47 | -import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
48 | -import br.gov.frameworkdemoiselle.util.Reflections; | |
49 | - | |
50 | -@Priority(EXTENSIONS_L1_PRIORITY) | |
51 | -public class ConfigurationClassValueExtractor implements ConfigurationValueExtractor { | |
52 | - | |
53 | - @Override | |
54 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
55 | - Object value = defaultValue; | |
56 | - String canonicalName = configuration.getString(prefix + key); | |
57 | - | |
58 | - if (canonicalName != null) { | |
59 | - ClassLoader classLoader = Reflections.getClassLoaderForClass(canonicalName); | |
60 | - | |
61 | - try { | |
62 | - value = Class.forName(canonicalName, true, classLoader); | |
63 | - } catch (ClassNotFoundException cause) { | |
64 | - // TODO Lançar a mensagem correta | |
65 | - throw new ConfigurationException(null, cause); | |
66 | - } | |
67 | - } | |
68 | - | |
69 | - return value; | |
70 | - } | |
71 | - | |
72 | - @Override | |
73 | - public boolean isSupported(Field field) { | |
74 | - return field.getType() == Class.class; | |
75 | - } | |
76 | -} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
... | ... | @@ -1,251 +0,0 @@ |
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 static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM; | |
41 | - | |
42 | -import java.io.Serializable; | |
43 | -import java.lang.reflect.Field; | |
44 | -import java.util.Collection; | |
45 | -import java.util.HashSet; | |
46 | - | |
47 | -import javax.validation.constraints.NotNull; | |
48 | - | |
49 | -import org.apache.commons.configuration.AbstractConfiguration; | |
50 | -import org.apache.commons.configuration.FileConfiguration; | |
51 | -import org.apache.commons.configuration.PropertiesConfiguration; | |
52 | -import org.apache.commons.configuration.SystemConfiguration; | |
53 | -import org.apache.commons.configuration.XMLConfiguration; | |
54 | - | |
55 | -import br.gov.frameworkdemoiselle.annotation.Ignore; | |
56 | -import br.gov.frameworkdemoiselle.annotation.Name; | |
57 | -import br.gov.frameworkdemoiselle.configuration.ConfigType; | |
58 | -import br.gov.frameworkdemoiselle.configuration.Configuration; | |
59 | -import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | |
60 | -import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
61 | -import br.gov.frameworkdemoiselle.internal.bootstrap.ConfigurationBootstrap; | |
62 | -import br.gov.frameworkdemoiselle.internal.implementation.StrategySelector; | |
63 | -import br.gov.frameworkdemoiselle.util.Beans; | |
64 | -import br.gov.frameworkdemoiselle.util.Reflections; | |
65 | - | |
66 | -/** | |
67 | - * This component loads a config class annotated with {@link Configuration} by filling its attributes with {@link Param} | |
68 | - * according to a {@link ConfigType}. | |
69 | - * | |
70 | - * @author SERPRO | |
71 | - */ | |
72 | -public class ConfigurationLoader implements Serializable { | |
73 | - | |
74 | - private static final long serialVersionUID = 1L; | |
75 | - | |
76 | - private Object object; | |
77 | - | |
78 | - private ConfigType type; | |
79 | - | |
80 | - private String resource; | |
81 | - | |
82 | - private String prefix; | |
83 | - | |
84 | - private org.apache.commons.configuration.Configuration configuration; | |
85 | - | |
86 | - private Collection<Field> fields; | |
87 | - | |
88 | - public void load(Object object) throws ConfigurationException { | |
89 | - this.object = object; | |
90 | - | |
91 | - loadFields(); | |
92 | - validateFields(); | |
93 | - | |
94 | - loadType(); | |
95 | - loadResource(); | |
96 | - loadConfiguration(); | |
97 | - | |
98 | - if (this.configuration != null) { | |
99 | - loadPrefix(); | |
100 | - loadValues(); | |
101 | - } | |
102 | - | |
103 | - validateValues(); | |
104 | - } | |
105 | - | |
106 | - private void loadFields() { | |
107 | - this.fields = Reflections.getNonStaticFields(this.object.getClass()); | |
108 | - } | |
109 | - | |
110 | - private void validateFields() { | |
111 | - for (Field field : this.fields) { | |
112 | - validateField(field); | |
113 | - } | |
114 | - } | |
115 | - | |
116 | - private void validateField(Field field) { | |
117 | - } | |
118 | - | |
119 | - private void loadType() { | |
120 | - this.type = object.getClass().getAnnotation(Configuration.class).type(); | |
121 | - } | |
122 | - | |
123 | - private void loadResource() { | |
124 | - if (this.type != SYSTEM) { | |
125 | - String name = this.object.getClass().getAnnotation(Configuration.class).resource(); | |
126 | - String extension = this.type.toString().toLowerCase(); | |
127 | - | |
128 | - this.resource = name + "." + extension; | |
129 | - } | |
130 | - } | |
131 | - | |
132 | - private void loadConfiguration() { | |
133 | - AbstractConfiguration conf; | |
134 | - | |
135 | - switch (this.type) { | |
136 | - case SYSTEM: | |
137 | - conf = new SystemConfiguration(); | |
138 | - break; | |
139 | - | |
140 | - case XML: | |
141 | - conf = new XMLConfiguration(); | |
142 | - break; | |
143 | - | |
144 | - default: | |
145 | - conf = new PropertiesConfiguration(); | |
146 | - break; | |
147 | - } | |
148 | - | |
149 | - conf.setDelimiterParsingDisabled(true); | |
150 | - | |
151 | - if (conf instanceof FileConfiguration) { | |
152 | - ((FileConfiguration) conf).setURL(Reflections.getResourceAsURL(this.resource)); | |
153 | - | |
154 | - try { | |
155 | - ((FileConfiguration) conf).load(); | |
156 | - | |
157 | - } catch (org.apache.commons.configuration.ConfigurationException cause) { | |
158 | - conf = null; | |
159 | - } | |
160 | - } | |
161 | - | |
162 | - this.configuration = conf; | |
163 | - } | |
164 | - | |
165 | - private void loadPrefix() { | |
166 | - String prefix = this.object.getClass().getAnnotation(Configuration.class).prefix(); | |
167 | - | |
168 | - if (prefix.endsWith(".")) { | |
169 | - // prefix = prefix.substring(0, prefix.length() - 1); | |
170 | - // TODO Lançar warning pedindo para retirar o ponto (.)? | |
171 | - } else if (!prefix.isEmpty()) { | |
172 | - prefix += "."; | |
173 | - } | |
174 | - | |
175 | - this.prefix = prefix; | |
176 | - } | |
177 | - | |
178 | - private void loadValues() { | |
179 | - for (Field field : this.fields) { | |
180 | - loadValue(field); | |
181 | - } | |
182 | - } | |
183 | - | |
184 | - private void loadValue(Field field) { | |
185 | - if (hasIgnore(field)) { | |
186 | - return; | |
187 | - } | |
188 | - | |
189 | - Object defaultValue = Reflections.getFieldValue(field, this.object); | |
190 | - Object finalValue = getValue(field, field.getType(), getKey(field), defaultValue); | |
191 | - | |
192 | - Reflections.setFieldValue(field, this.object, finalValue); | |
193 | - } | |
194 | - | |
195 | - private Object getValue(Field field, Class<?> type, String key, Object defaultValue) { | |
196 | - ConfigurationValueExtractor extractor = getValueExtractor(field); | |
197 | - return extractor.getValue(this.prefix, key, field, this.configuration, defaultValue); | |
198 | - } | |
199 | - | |
200 | - private ConfigurationValueExtractor getValueExtractor(Field field) { | |
201 | - Collection<ConfigurationValueExtractor> candidates = new HashSet<ConfigurationValueExtractor>(); | |
202 | - ConfigurationBootstrap bootstrap = Beans.getReference(ConfigurationBootstrap.class); | |
203 | - | |
204 | - for (Class<? extends ConfigurationValueExtractor> extractorClass : bootstrap.getCache()) { | |
205 | - ConfigurationValueExtractor extractor = Beans.getReference(extractorClass); | |
206 | - | |
207 | - if (extractor.isSupported(field)) { | |
208 | - candidates.add(extractor); | |
209 | - } | |
210 | - } | |
211 | - | |
212 | - ConfigurationValueExtractor elected = StrategySelector.getInstance(ConfigurationValueExtractor.class, | |
213 | - candidates); | |
214 | - | |
215 | - if (elected == null) { | |
216 | - // TODO lançar exceção informando que nenhum extrator foi encontrado para o field e ensinar como implementar | |
217 | - // um extrator personalizado. | |
218 | - } | |
219 | - | |
220 | - return elected; | |
221 | - } | |
222 | - | |
223 | - private String getKey(Field field) { | |
224 | - String key = ""; | |
225 | - | |
226 | - if (field.isAnnotationPresent(Name.class)) { | |
227 | - key += field.getAnnotation(Name.class).value(); | |
228 | - } else { | |
229 | - key += field.getName(); | |
230 | - } | |
231 | - | |
232 | - return key; | |
233 | - } | |
234 | - | |
235 | - private boolean hasIgnore(Field field) { | |
236 | - return field.isAnnotationPresent(Ignore.class); | |
237 | - } | |
238 | - | |
239 | - private void validateValues() { | |
240 | - for (Field field : this.fields) { | |
241 | - validateValue(field); | |
242 | - } | |
243 | - } | |
244 | - | |
245 | - private void validateValue(Field field) { | |
246 | - if (field.isAnnotationPresent(NotNull.class) && Reflections.getFieldValue(field, this.object) == null) { | |
247 | - throw new ConfigurationException("", new NullPointerException()); | |
248 | - // TODO: Pegar mensagem do Bundle e verificar como as mensagens de log estão implementadas | |
249 | - } | |
250 | - } | |
251 | -} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationMapValueExtractor.java
... | ... | @@ -1,88 +0,0 @@ |
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.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | - | |
41 | -import java.lang.reflect.Field; | |
42 | -import java.util.HashMap; | |
43 | -import java.util.Iterator; | |
44 | -import java.util.Map; | |
45 | -import java.util.regex.Matcher; | |
46 | -import java.util.regex.Pattern; | |
47 | - | |
48 | -import org.apache.commons.configuration.Configuration; | |
49 | - | |
50 | -import br.gov.frameworkdemoiselle.annotation.Priority; | |
51 | -import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
52 | - | |
53 | -@Priority(EXTENSIONS_L1_PRIORITY) | |
54 | -public class ConfigurationMapValueExtractor implements ConfigurationValueExtractor { | |
55 | - | |
56 | - @Override | |
57 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
58 | - @SuppressWarnings("unchecked") | |
59 | - Map<String, Object> value = (Map<String, Object>) defaultValue; | |
60 | - | |
61 | - String regexp = "^(" + prefix + ")((.+)\\.)?(" + key + ")$"; | |
62 | - Pattern pattern = Pattern.compile(regexp); | |
63 | - | |
64 | - for (Iterator<String> iter = configuration.getKeys(); iter.hasNext();) { | |
65 | - String iterKey = iter.next(); | |
66 | - Matcher matcher = pattern.matcher(iterKey); | |
67 | - | |
68 | - if (matcher.matches()) { | |
69 | - String confKey = matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2)) | |
70 | - + matcher.group(4); | |
71 | - | |
72 | - if (value == null) { | |
73 | - value = new HashMap<String, Object>(); | |
74 | - } | |
75 | - | |
76 | - String mapKey = matcher.group(3) == null ? "default" : matcher.group(3); | |
77 | - value.put(mapKey, configuration.getString(confKey)); | |
78 | - } | |
79 | - } | |
80 | - | |
81 | - return value; | |
82 | - } | |
83 | - | |
84 | - @Override | |
85 | - public boolean isSupported(Field field) { | |
86 | - return field.getType() == Map.class; | |
87 | - } | |
88 | -} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationPrimitiveOrWrapperValueExtractor.java
... | ... | @@ -1,89 +0,0 @@ |
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.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | - | |
41 | -import java.lang.reflect.Field; | |
42 | -import java.util.HashSet; | |
43 | -import java.util.Set; | |
44 | - | |
45 | -import org.apache.commons.configuration.Configuration; | |
46 | -import org.apache.commons.configuration.ConversionException; | |
47 | -import org.apache.commons.configuration.DataConfiguration; | |
48 | -import org.apache.commons.lang.ClassUtils; | |
49 | - | |
50 | -import br.gov.frameworkdemoiselle.annotation.Priority; | |
51 | -import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
52 | - | |
53 | -@Priority(EXTENSIONS_L1_PRIORITY) | |
54 | -public class ConfigurationPrimitiveOrWrapperValueExtractor implements ConfigurationValueExtractor { | |
55 | - | |
56 | - private static final Set<Object> wrappers = new HashSet<Object>(); | |
57 | - | |
58 | - static { | |
59 | - wrappers.add(Boolean.class); | |
60 | - wrappers.add(Byte.class); | |
61 | - wrappers.add(Character.class); | |
62 | - wrappers.add(Short.class); | |
63 | - wrappers.add(Integer.class); | |
64 | - wrappers.add(Long.class); | |
65 | - wrappers.add(Double.class); | |
66 | - wrappers.add(Float.class); | |
67 | - wrappers.add(Void.TYPE); | |
68 | - } | |
69 | - | |
70 | - @Override | |
71 | - @SuppressWarnings("unchecked") | |
72 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
73 | - Object value; | |
74 | - | |
75 | - try { | |
76 | - value = new DataConfiguration(configuration).get(ClassUtils.primitiveToWrapper(field.getType()), prefix + key, defaultValue); | |
77 | - | |
78 | - } catch (ConversionException cause) { | |
79 | - value = defaultValue; | |
80 | - } | |
81 | - | |
82 | - return value; | |
83 | - } | |
84 | - | |
85 | - @Override | |
86 | - public boolean isSupported(Field field) { | |
87 | - return field.getType().isPrimitive() || wrappers.contains(field.getType()); | |
88 | - } | |
89 | -} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationStringValueExtractor.java
... | ... | @@ -1,60 +0,0 @@ |
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.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | - | |
41 | -import java.lang.reflect.Field; | |
42 | - | |
43 | -import org.apache.commons.configuration.Configuration; | |
44 | - | |
45 | -import br.gov.frameworkdemoiselle.annotation.Priority; | |
46 | -import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
47 | - | |
48 | -@Priority(EXTENSIONS_L1_PRIORITY) | |
49 | -public class ConfigurationStringValueExtractor implements ConfigurationValueExtractor { | |
50 | - | |
51 | - @Override | |
52 | - public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
53 | - return configuration.getString(prefix + key, (String) defaultValue); | |
54 | - } | |
55 | - | |
56 | - @Override | |
57 | - public boolean isSupported(Field field) { | |
58 | - return field.getType() == String.class; | |
59 | - } | |
60 | -} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationArrayValueExtractor.java
0 → 100644
... | ... | @@ -0,0 +1,62 @@ |
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.implementation; | |
38 | + | |
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | + | |
41 | +import java.lang.reflect.Field; | |
42 | + | |
43 | +import org.apache.commons.configuration.Configuration; | |
44 | +import org.apache.commons.configuration.DataConfiguration; | |
45 | + | |
46 | +import br.gov.frameworkdemoiselle.annotation.Priority; | |
47 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
48 | + | |
49 | +@Priority(EXTENSIONS_L1_PRIORITY) | |
50 | +public class ConfigurationArrayValueExtractor implements ConfigurationValueExtractor { | |
51 | + | |
52 | + @Override | |
53 | + public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
54 | + return new DataConfiguration(configuration).getArray(field.getType().getComponentType(), prefix + key, | |
55 | + defaultValue); | |
56 | + } | |
57 | + | |
58 | + @Override | |
59 | + public boolean isSupported(Field field) { | |
60 | + return field.getType().isArray(); | |
61 | + } | |
62 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationClassValueExtractor.java
0 → 100644
... | ... | @@ -0,0 +1,76 @@ |
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.implementation; | |
38 | + | |
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | + | |
41 | +import java.lang.reflect.Field; | |
42 | + | |
43 | +import org.apache.commons.configuration.Configuration; | |
44 | + | |
45 | +import br.gov.frameworkdemoiselle.annotation.Priority; | |
46 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | |
47 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
48 | +import br.gov.frameworkdemoiselle.util.Reflections; | |
49 | + | |
50 | +@Priority(EXTENSIONS_L1_PRIORITY) | |
51 | +public class ConfigurationClassValueExtractor implements ConfigurationValueExtractor { | |
52 | + | |
53 | + @Override | |
54 | + public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
55 | + Object value = defaultValue; | |
56 | + String canonicalName = configuration.getString(prefix + key); | |
57 | + | |
58 | + if (canonicalName != null) { | |
59 | + ClassLoader classLoader = Reflections.getClassLoaderForClass(canonicalName); | |
60 | + | |
61 | + try { | |
62 | + value = Class.forName(canonicalName, true, classLoader); | |
63 | + } catch (ClassNotFoundException cause) { | |
64 | + // TODO Lançar a mensagem correta | |
65 | + throw new ConfigurationException(null, cause); | |
66 | + } | |
67 | + } | |
68 | + | |
69 | + return value; | |
70 | + } | |
71 | + | |
72 | + @Override | |
73 | + public boolean isSupported(Field field) { | |
74 | + return field.getType() == Class.class; | |
75 | + } | |
76 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationImpl.java
... | ... | @@ -3,7 +3,6 @@ package br.gov.frameworkdemoiselle.internal.implementation; |
3 | 3 | import java.io.Serializable; |
4 | 4 | |
5 | 5 | import br.gov.frameworkdemoiselle.annotation.Ignore; |
6 | -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | |
7 | 6 | import br.gov.frameworkdemoiselle.util.Beans; |
8 | 7 | |
9 | 8 | public class ConfigurationImpl implements Serializable { | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationLoader.java
0 → 100644
... | ... | @@ -0,0 +1,250 @@ |
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.implementation; | |
39 | + | |
40 | +import static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM; | |
41 | + | |
42 | +import java.io.Serializable; | |
43 | +import java.lang.reflect.Field; | |
44 | +import java.util.Collection; | |
45 | +import java.util.HashSet; | |
46 | + | |
47 | +import javax.validation.constraints.NotNull; | |
48 | + | |
49 | +import org.apache.commons.configuration.AbstractConfiguration; | |
50 | +import org.apache.commons.configuration.FileConfiguration; | |
51 | +import org.apache.commons.configuration.PropertiesConfiguration; | |
52 | +import org.apache.commons.configuration.SystemConfiguration; | |
53 | +import org.apache.commons.configuration.XMLConfiguration; | |
54 | + | |
55 | +import br.gov.frameworkdemoiselle.annotation.Ignore; | |
56 | +import br.gov.frameworkdemoiselle.annotation.Name; | |
57 | +import br.gov.frameworkdemoiselle.configuration.ConfigType; | |
58 | +import br.gov.frameworkdemoiselle.configuration.Configuration; | |
59 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | |
60 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
61 | +import br.gov.frameworkdemoiselle.internal.bootstrap.ConfigurationBootstrap; | |
62 | +import br.gov.frameworkdemoiselle.util.Beans; | |
63 | +import br.gov.frameworkdemoiselle.util.Reflections; | |
64 | + | |
65 | +/** | |
66 | + * This component loads a config class annotated with {@link Configuration} by filling its attributes with {@link Param} | |
67 | + * according to a {@link ConfigType}. | |
68 | + * | |
69 | + * @author SERPRO | |
70 | + */ | |
71 | +public class ConfigurationLoader implements Serializable { | |
72 | + | |
73 | + private static final long serialVersionUID = 1L; | |
74 | + | |
75 | + private Object object; | |
76 | + | |
77 | + private ConfigType type; | |
78 | + | |
79 | + private String resource; | |
80 | + | |
81 | + private String prefix; | |
82 | + | |
83 | + private org.apache.commons.configuration.Configuration configuration; | |
84 | + | |
85 | + private Collection<Field> fields; | |
86 | + | |
87 | + public void load(Object object) throws ConfigurationException { | |
88 | + this.object = object; | |
89 | + | |
90 | + loadFields(); | |
91 | + validateFields(); | |
92 | + | |
93 | + loadType(); | |
94 | + loadResource(); | |
95 | + loadConfiguration(); | |
96 | + | |
97 | + if (this.configuration != null) { | |
98 | + loadPrefix(); | |
99 | + loadValues(); | |
100 | + } | |
101 | + | |
102 | + validateValues(); | |
103 | + } | |
104 | + | |
105 | + private void loadFields() { | |
106 | + this.fields = Reflections.getNonStaticFields(this.object.getClass()); | |
107 | + } | |
108 | + | |
109 | + private void validateFields() { | |
110 | + for (Field field : this.fields) { | |
111 | + validateField(field); | |
112 | + } | |
113 | + } | |
114 | + | |
115 | + private void validateField(Field field) { | |
116 | + } | |
117 | + | |
118 | + private void loadType() { | |
119 | + this.type = object.getClass().getAnnotation(Configuration.class).type(); | |
120 | + } | |
121 | + | |
122 | + private void loadResource() { | |
123 | + if (this.type != SYSTEM) { | |
124 | + String name = this.object.getClass().getAnnotation(Configuration.class).resource(); | |
125 | + String extension = this.type.toString().toLowerCase(); | |
126 | + | |
127 | + this.resource = name + "." + extension; | |
128 | + } | |
129 | + } | |
130 | + | |
131 | + private void loadConfiguration() { | |
132 | + AbstractConfiguration conf; | |
133 | + | |
134 | + switch (this.type) { | |
135 | + case SYSTEM: | |
136 | + conf = new SystemConfiguration(); | |
137 | + break; | |
138 | + | |
139 | + case XML: | |
140 | + conf = new XMLConfiguration(); | |
141 | + break; | |
142 | + | |
143 | + default: | |
144 | + conf = new PropertiesConfiguration(); | |
145 | + break; | |
146 | + } | |
147 | + | |
148 | + conf.setDelimiterParsingDisabled(true); | |
149 | + | |
150 | + if (conf instanceof FileConfiguration) { | |
151 | + ((FileConfiguration) conf).setURL(Reflections.getResourceAsURL(this.resource)); | |
152 | + | |
153 | + try { | |
154 | + ((FileConfiguration) conf).load(); | |
155 | + | |
156 | + } catch (org.apache.commons.configuration.ConfigurationException cause) { | |
157 | + conf = null; | |
158 | + } | |
159 | + } | |
160 | + | |
161 | + this.configuration = conf; | |
162 | + } | |
163 | + | |
164 | + private void loadPrefix() { | |
165 | + String prefix = this.object.getClass().getAnnotation(Configuration.class).prefix(); | |
166 | + | |
167 | + if (prefix.endsWith(".")) { | |
168 | + // prefix = prefix.substring(0, prefix.length() - 1); | |
169 | + // TODO Lançar warning pedindo para retirar o ponto (.)? | |
170 | + } else if (!prefix.isEmpty()) { | |
171 | + prefix += "."; | |
172 | + } | |
173 | + | |
174 | + this.prefix = prefix; | |
175 | + } | |
176 | + | |
177 | + private void loadValues() { | |
178 | + for (Field field : this.fields) { | |
179 | + loadValue(field); | |
180 | + } | |
181 | + } | |
182 | + | |
183 | + private void loadValue(Field field) { | |
184 | + if (hasIgnore(field)) { | |
185 | + return; | |
186 | + } | |
187 | + | |
188 | + Object defaultValue = Reflections.getFieldValue(field, this.object); | |
189 | + Object finalValue = getValue(field, field.getType(), getKey(field), defaultValue); | |
190 | + | |
191 | + Reflections.setFieldValue(field, this.object, finalValue); | |
192 | + } | |
193 | + | |
194 | + private Object getValue(Field field, Class<?> type, String key, Object defaultValue) { | |
195 | + ConfigurationValueExtractor extractor = getValueExtractor(field); | |
196 | + return extractor.getValue(this.prefix, key, field, this.configuration, defaultValue); | |
197 | + } | |
198 | + | |
199 | + private ConfigurationValueExtractor getValueExtractor(Field field) { | |
200 | + Collection<ConfigurationValueExtractor> candidates = new HashSet<ConfigurationValueExtractor>(); | |
201 | + ConfigurationBootstrap bootstrap = Beans.getReference(ConfigurationBootstrap.class); | |
202 | + | |
203 | + for (Class<? extends ConfigurationValueExtractor> extractorClass : bootstrap.getCache()) { | |
204 | + ConfigurationValueExtractor extractor = Beans.getReference(extractorClass); | |
205 | + | |
206 | + if (extractor.isSupported(field)) { | |
207 | + candidates.add(extractor); | |
208 | + } | |
209 | + } | |
210 | + | |
211 | + ConfigurationValueExtractor elected = StrategySelector.getInstance(ConfigurationValueExtractor.class, | |
212 | + candidates); | |
213 | + | |
214 | + if (elected == null) { | |
215 | + // TODO lançar exceção informando que nenhum extrator foi encontrado para o field e ensinar como implementar | |
216 | + // um extrator personalizado. | |
217 | + } | |
218 | + | |
219 | + return elected; | |
220 | + } | |
221 | + | |
222 | + private String getKey(Field field) { | |
223 | + String key = ""; | |
224 | + | |
225 | + if (field.isAnnotationPresent(Name.class)) { | |
226 | + key += field.getAnnotation(Name.class).value(); | |
227 | + } else { | |
228 | + key += field.getName(); | |
229 | + } | |
230 | + | |
231 | + return key; | |
232 | + } | |
233 | + | |
234 | + private boolean hasIgnore(Field field) { | |
235 | + return field.isAnnotationPresent(Ignore.class); | |
236 | + } | |
237 | + | |
238 | + private void validateValues() { | |
239 | + for (Field field : this.fields) { | |
240 | + validateValue(field); | |
241 | + } | |
242 | + } | |
243 | + | |
244 | + private void validateValue(Field field) { | |
245 | + if (field.isAnnotationPresent(NotNull.class) && Reflections.getFieldValue(field, this.object) == null) { | |
246 | + throw new ConfigurationException("", new NullPointerException()); | |
247 | + // TODO: Pegar mensagem do Bundle e verificar como as mensagens de log estão implementadas | |
248 | + } | |
249 | + } | |
250 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationMapValueExtractor.java
0 → 100644
... | ... | @@ -0,0 +1,88 @@ |
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.implementation; | |
38 | + | |
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | + | |
41 | +import java.lang.reflect.Field; | |
42 | +import java.util.HashMap; | |
43 | +import java.util.Iterator; | |
44 | +import java.util.Map; | |
45 | +import java.util.regex.Matcher; | |
46 | +import java.util.regex.Pattern; | |
47 | + | |
48 | +import org.apache.commons.configuration.Configuration; | |
49 | + | |
50 | +import br.gov.frameworkdemoiselle.annotation.Priority; | |
51 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
52 | + | |
53 | +@Priority(EXTENSIONS_L1_PRIORITY) | |
54 | +public class ConfigurationMapValueExtractor implements ConfigurationValueExtractor { | |
55 | + | |
56 | + @Override | |
57 | + public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
58 | + @SuppressWarnings("unchecked") | |
59 | + Map<String, Object> value = (Map<String, Object>) defaultValue; | |
60 | + | |
61 | + String regexp = "^(" + prefix + ")((.+)\\.)?(" + key + ")$"; | |
62 | + Pattern pattern = Pattern.compile(regexp); | |
63 | + | |
64 | + for (Iterator<String> iter = configuration.getKeys(); iter.hasNext();) { | |
65 | + String iterKey = iter.next(); | |
66 | + Matcher matcher = pattern.matcher(iterKey); | |
67 | + | |
68 | + if (matcher.matches()) { | |
69 | + String confKey = matcher.group(1) + (matcher.group(2) == null ? "" : matcher.group(2)) | |
70 | + + matcher.group(4); | |
71 | + | |
72 | + if (value == null) { | |
73 | + value = new HashMap<String, Object>(); | |
74 | + } | |
75 | + | |
76 | + String mapKey = matcher.group(3) == null ? "default" : matcher.group(3); | |
77 | + value.put(mapKey, configuration.getString(confKey)); | |
78 | + } | |
79 | + } | |
80 | + | |
81 | + return value; | |
82 | + } | |
83 | + | |
84 | + @Override | |
85 | + public boolean isSupported(Field field) { | |
86 | + return field.getType() == Map.class; | |
87 | + } | |
88 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationPrimitiveOrWrapperValueExtractor.java
0 → 100644
... | ... | @@ -0,0 +1,89 @@ |
1 | +/* | |
2 | + * Demoiselle Framework | |
3 | + * Copyright (C) 2010 SERPRO | |
4 | + * ---------------------------------------------------------------------------- | |
5 | + * This file is part of Demoiselle Framework. | |
6 | + * | |
7 | + * Demoiselle Framework is free software; you can redistribute it and/or | |
8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | |
9 | + * as published by the Free Software Foundation. | |
10 | + * | |
11 | + * This program is distributed in the hope that it will be useful, | |
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | + * GNU General Public License for more details. | |
15 | + * | |
16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | |
17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | |
18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | + * ---------------------------------------------------------------------------- | |
21 | + * Este arquivo é parte do Framework Demoiselle. | |
22 | + * | |
23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
25 | + * do Software Livre (FSF). | |
26 | + * | |
27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
30 | + * para maiores detalhes. | |
31 | + * | |
32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
36 | + */ | |
37 | +package br.gov.frameworkdemoiselle.internal.implementation; | |
38 | + | |
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | + | |
41 | +import java.lang.reflect.Field; | |
42 | +import java.util.HashSet; | |
43 | +import java.util.Set; | |
44 | + | |
45 | +import org.apache.commons.configuration.Configuration; | |
46 | +import org.apache.commons.configuration.ConversionException; | |
47 | +import org.apache.commons.configuration.DataConfiguration; | |
48 | +import org.apache.commons.lang.ClassUtils; | |
49 | + | |
50 | +import br.gov.frameworkdemoiselle.annotation.Priority; | |
51 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
52 | + | |
53 | +@Priority(EXTENSIONS_L1_PRIORITY) | |
54 | +public class ConfigurationPrimitiveOrWrapperValueExtractor implements ConfigurationValueExtractor { | |
55 | + | |
56 | + private static final Set<Object> wrappers = new HashSet<Object>(); | |
57 | + | |
58 | + static { | |
59 | + wrappers.add(Boolean.class); | |
60 | + wrappers.add(Byte.class); | |
61 | + wrappers.add(Character.class); | |
62 | + wrappers.add(Short.class); | |
63 | + wrappers.add(Integer.class); | |
64 | + wrappers.add(Long.class); | |
65 | + wrappers.add(Double.class); | |
66 | + wrappers.add(Float.class); | |
67 | + wrappers.add(Void.TYPE); | |
68 | + } | |
69 | + | |
70 | + @Override | |
71 | + @SuppressWarnings("unchecked") | |
72 | + public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
73 | + Object value; | |
74 | + | |
75 | + try { | |
76 | + value = new DataConfiguration(configuration).get(ClassUtils.primitiveToWrapper(field.getType()), prefix + key, defaultValue); | |
77 | + | |
78 | + } catch (ConversionException cause) { | |
79 | + value = defaultValue; | |
80 | + } | |
81 | + | |
82 | + return value; | |
83 | + } | |
84 | + | |
85 | + @Override | |
86 | + public boolean isSupported(Field field) { | |
87 | + return field.getType().isPrimitive() || wrappers.contains(field.getType()); | |
88 | + } | |
89 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationStringValueExtractor.java
0 → 100644
... | ... | @@ -0,0 +1,60 @@ |
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.implementation; | |
38 | + | |
39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | |
40 | + | |
41 | +import java.lang.reflect.Field; | |
42 | + | |
43 | +import org.apache.commons.configuration.Configuration; | |
44 | + | |
45 | +import br.gov.frameworkdemoiselle.annotation.Priority; | |
46 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | |
47 | + | |
48 | +@Priority(EXTENSIONS_L1_PRIORITY) | |
49 | +public class ConfigurationStringValueExtractor implements ConfigurationValueExtractor { | |
50 | + | |
51 | + @Override | |
52 | + public Object getValue(String prefix, String key, Field field, Configuration configuration, Object defaultValue) { | |
53 | + return configuration.getString(prefix + key, (String) defaultValue); | |
54 | + } | |
55 | + | |
56 | + @Override | |
57 | + public boolean isSupported(Field field) { | |
58 | + return field.getType() == String.class; | |
59 | + } | |
60 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java
... | ... | @@ -47,8 +47,6 @@ import java.util.ArrayList; |
47 | 47 | import java.util.Arrays; |
48 | 48 | import java.util.List; |
49 | 49 | |
50 | -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | |
51 | - | |
52 | 50 | public final class Reflections { |
53 | 51 | |
54 | 52 | private Reflections() { |
... | ... | @@ -178,8 +176,8 @@ public final class Reflections { |
178 | 176 | } |
179 | 177 | |
180 | 178 | if (url == null) { |
181 | - result = ConfigurationLoader.class.getClassLoader(); | |
182 | - url = ConfigurationLoader.class.getClassLoader().getResource(stripped); | |
179 | + result = Reflections.class.getClassLoader(); | |
180 | + url = Reflections.class.getClassLoader().getResource(stripped); | |
183 | 181 | } |
184 | 182 | |
185 | 183 | if (url == null) { | ... | ... |
impl/core/src/test/java/br/gov/frameworkdemoiselle/configuration/AbstractConfigurationTest.java
... | ... | @@ -48,12 +48,12 @@ import br.gov.frameworkdemoiselle.annotation.Ignore; |
48 | 48 | import br.gov.frameworkdemoiselle.annotation.Name; |
49 | 49 | import br.gov.frameworkdemoiselle.internal.bootstrap.ConfigurationBootstrap; |
50 | 50 | import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; |
51 | -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationArrayValueExtractor; | |
52 | -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationClassValueExtractor; | |
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; | |
51 | +import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationArrayValueExtractor; | |
52 | +import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationClassValueExtractor; | |
53 | +import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationLoader; | |
54 | +import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationMapValueExtractor; | |
55 | +import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationPrimitiveOrWrapperValueExtractor; | |
56 | +import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationStringValueExtractor; | |
57 | 57 | import br.gov.frameworkdemoiselle.internal.producer.LocaleProducer; |
58 | 58 | import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; |
59 | 59 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | ... | ... |