Commit 86a2e752fe2dc94c03953897d954eca24597f992
1 parent
ee28b5f4
Exists in
master
Adicionando carregamento para tipos complexos
Showing
1 changed file
with
77 additions
and
8 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
... | ... | @@ -48,6 +48,7 @@ import java.util.List; |
48 | 48 | import javax.validation.constraints.NotNull; |
49 | 49 | |
50 | 50 | import org.apache.commons.configuration.AbstractConfiguration; |
51 | +import org.apache.commons.configuration.DataConfiguration; | |
51 | 52 | import org.apache.commons.configuration.FileConfiguration; |
52 | 53 | import org.apache.commons.configuration.PropertiesConfiguration; |
53 | 54 | import org.apache.commons.configuration.SystemConfiguration; |
... | ... | @@ -79,7 +80,7 @@ public class ConfigurationLoader implements Serializable { |
79 | 80 | |
80 | 81 | private String prefix; |
81 | 82 | |
82 | - private org.apache.commons.configuration.Configuration configuration; | |
83 | + private DataConfiguration configuration; | |
83 | 84 | |
84 | 85 | private List<Field> fields; |
85 | 86 | |
... | ... | @@ -137,7 +138,7 @@ public class ConfigurationLoader implements Serializable { |
137 | 138 | } |
138 | 139 | } |
139 | 140 | |
140 | - this.configuration = conf; | |
141 | + this.configuration = new DataConfiguration(conf); | |
141 | 142 | } |
142 | 143 | |
143 | 144 | private void loadResource() { |
... | ... | @@ -172,16 +173,84 @@ public class ConfigurationLoader implements Serializable { |
172 | 173 | return; |
173 | 174 | } |
174 | 175 | |
176 | + String key = getKey(field); | |
177 | + | |
178 | + // if (field.getType() == String.class) { | |
179 | + // loadStringField(field, key); | |
180 | + // } else { | |
181 | + // loadBasicField(field, key); | |
182 | + // } | |
183 | + | |
184 | + // if (field.getType().isArray()) { | |
185 | + // loadArrayField(field, key); | |
186 | + // } else { | |
187 | + loadBasicField(field, key); | |
188 | + // } | |
189 | + } | |
190 | + | |
191 | + // private void loadStringField(Field field, String key) { | |
192 | + // try { | |
193 | + // String value = (String) Reflections.getFieldValue(field, this.object); | |
194 | + // | |
195 | + // for (String string : this.configuration.getStringArray(key)) { | |
196 | + // if (value == null) { | |
197 | + // value = ""; | |
198 | + // this.configuration.get(String.class, key); | |
199 | + // } else { | |
200 | + // value += ","; | |
201 | + // } | |
202 | + // | |
203 | + // value += string; | |
204 | + // } | |
205 | + // // this.configuration.setDelimiterParsingDisabled(true); | |
206 | + // | |
207 | + // Reflections.setFieldValue(field, this.object, value); | |
208 | + // | |
209 | + // } catch (SecurityException cause) { | |
210 | + // cause.printStackTrace(); | |
211 | + // } catch (IllegalArgumentException cause) { | |
212 | + // cause.printStackTrace(); | |
213 | + // } | |
214 | + // } | |
215 | + | |
216 | + private void loadArrayField(Field field, String key) { | |
175 | 217 | try { |
176 | - String key = getKey(field); | |
177 | - Class<?> fieldType = field.getType(); | |
178 | - String methodName = "get" + Strings.firstToUpper(fieldType.getSimpleName()); | |
218 | + @SuppressWarnings("unused") | |
219 | + Object array = this.configuration.getArray(Integer.class, key); | |
220 | + | |
221 | + String methodName = "get" + Strings.firstToUpper(field.getType().getSimpleName()); | |
222 | + | |
223 | + if (field.getType().isArray()) { | |
224 | + methodName = Strings.removeChars(methodName, '[', ']') + "Array"; | |
225 | + } | |
179 | 226 | |
180 | - Method method = this.configuration.getClass().getMethod(methodName, String.class, fieldType); | |
227 | + Method method = this.configuration.getClass().getMethod(methodName, String.class, field.getType()); | |
181 | 228 | Object value = method.invoke(this.configuration, key, Reflections.getFieldValue(field, this.object)); |
182 | - // TODO Se não achar no arquivo de configuração vai dar a falsa sensação que o valor padrão foi carregado de | |
183 | - // lá. Corrigir isso! | |
229 | + Reflections.setFieldValue(field, this.object, value); | |
230 | + | |
231 | + } catch (SecurityException cause) { | |
232 | + cause.printStackTrace(); | |
233 | + } catch (NoSuchMethodException cause) { | |
234 | + cause.printStackTrace(); | |
235 | + } catch (IllegalArgumentException cause) { | |
236 | + cause.printStackTrace(); | |
237 | + } catch (IllegalAccessException cause) { | |
238 | + cause.printStackTrace(); | |
239 | + } catch (InvocationTargetException cause) { | |
240 | + cause.printStackTrace(); | |
241 | + } | |
242 | + } | |
243 | + | |
244 | + private void loadBasicField(Field field, String key) { | |
245 | + try { | |
246 | + String methodName = "get" + Strings.firstToUpper(field.getType().getSimpleName()); | |
184 | 247 | |
248 | + if (field.getType().isArray()) { | |
249 | + methodName = Strings.removeChars(methodName, '[', ']') + "Array"; | |
250 | + } | |
251 | + | |
252 | + Method method = this.configuration.getClass().getMethod(methodName, String.class, field.getType()); | |
253 | + Object value = method.invoke(this.configuration, key, Reflections.getFieldValue(field, this.object)); | |
185 | 254 | Reflections.setFieldValue(field, this.object, value); |
186 | 255 | |
187 | 256 | } catch (SecurityException cause) { | ... | ... |