Commit 2f3cf8fa008dcff3437390ee9ad2dda97ac71a1a
1 parent
86a2e752
Exists in
master
Ajuste para carregamento de variáveis do tipo primitivo, wrapped e array
Showing
1 changed file
with
29 additions
and
84 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
| ... | ... | @@ -41,13 +41,12 @@ import static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM; |
| 41 | 41 | |
| 42 | 42 | import java.io.Serializable; |
| 43 | 43 | import java.lang.reflect.Field; |
| 44 | -import java.lang.reflect.InvocationTargetException; | |
| 45 | -import java.lang.reflect.Method; | |
| 46 | 44 | import java.util.List; |
| 47 | 45 | |
| 48 | 46 | import javax.validation.constraints.NotNull; |
| 49 | 47 | |
| 50 | 48 | import org.apache.commons.configuration.AbstractConfiguration; |
| 49 | +import org.apache.commons.configuration.ConversionException; | |
| 51 | 50 | import org.apache.commons.configuration.DataConfiguration; |
| 52 | 51 | import org.apache.commons.configuration.FileConfiguration; |
| 53 | 52 | import org.apache.commons.configuration.PropertiesConfiguration; |
| ... | ... | @@ -60,7 +59,6 @@ import br.gov.frameworkdemoiselle.configuration.ConfigType; |
| 60 | 59 | import br.gov.frameworkdemoiselle.configuration.Configuration; |
| 61 | 60 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; |
| 62 | 61 | import br.gov.frameworkdemoiselle.util.Reflections; |
| 63 | -import br.gov.frameworkdemoiselle.util.Strings; | |
| 64 | 62 | |
| 65 | 63 | /** |
| 66 | 64 | * This component loads a config class annotated with {@link Configuration} by filling its attributes with {@link Param} |
| ... | ... | @@ -102,6 +100,12 @@ public class ConfigurationLoader implements Serializable { |
| 102 | 100 | } |
| 103 | 101 | |
| 104 | 102 | private void validateFields() { |
| 103 | + for (Field field : getFields()) { | |
| 104 | + validateField(field); | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | + private void validateField(Field field) { | |
| 105 | 109 | } |
| 106 | 110 | |
| 107 | 111 | private void loadType() { |
| ... | ... | @@ -138,7 +142,7 @@ public class ConfigurationLoader implements Serializable { |
| 138 | 142 | } |
| 139 | 143 | } |
| 140 | 144 | |
| 141 | - this.configuration = new DataConfiguration(conf); | |
| 145 | + this.configuration = (conf == null ? null : new DataConfiguration(conf)); | |
| 142 | 146 | } |
| 143 | 147 | |
| 144 | 148 | private void loadResource() { |
| ... | ... | @@ -175,95 +179,36 @@ public class ConfigurationLoader implements Serializable { |
| 175 | 179 | |
| 176 | 180 | String key = getKey(field); |
| 177 | 181 | |
| 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 | - // } | |
| 182 | + if (field.getType().isArray()) { | |
| 183 | + loadArrayField(field, key); | |
| 184 | + } else if (field.getType() == String.class) { | |
| 185 | + loadStringField(field, key); | |
| 186 | + } else { | |
| 187 | + loadBasicField(field, key); | |
| 188 | + } | |
| 189 | 189 | } |
| 190 | 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 | 191 | private void loadArrayField(Field field, String key) { |
| 217 | - try { | |
| 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 | - } | |
| 192 | + Object value = this.configuration.getArray(field.getType().getComponentType(), key, | |
| 193 | + Reflections.getFieldValue(field, this.object)); | |
| 194 | + Reflections.setFieldValue(field, this.object, value); | |
| 195 | + } | |
| 226 | 196 | |
| 227 | - Method method = this.configuration.getClass().getMethod(methodName, String.class, field.getType()); | |
| 228 | - Object value = method.invoke(this.configuration, key, Reflections.getFieldValue(field, this.object)); | |
| 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 | - } | |
| 197 | + private void loadStringField(Field field, String key) { | |
| 198 | + Object value = this.configuration.getString(key, (String) Reflections.getFieldValue(field, this.object)); | |
| 199 | + Reflections.setFieldValue(field, this.object, value); | |
| 242 | 200 | } |
| 243 | 201 | |
| 244 | 202 | private void loadBasicField(Field field, String key) { |
| 245 | - try { | |
| 246 | - String methodName = "get" + Strings.firstToUpper(field.getType().getSimpleName()); | |
| 203 | + Object value; | |
| 247 | 204 | |
| 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)); | |
| 254 | - Reflections.setFieldValue(field, this.object, value); | |
| 255 | - | |
| 256 | - } catch (SecurityException cause) { | |
| 257 | - cause.printStackTrace(); | |
| 258 | - } catch (NoSuchMethodException cause) { | |
| 259 | - cause.printStackTrace(); | |
| 260 | - } catch (IllegalArgumentException cause) { | |
| 261 | - cause.printStackTrace(); | |
| 262 | - } catch (IllegalAccessException cause) { | |
| 263 | - cause.printStackTrace(); | |
| 264 | - } catch (InvocationTargetException cause) { | |
| 265 | - cause.printStackTrace(); | |
| 205 | + try { | |
| 206 | + value = this.configuration.get(field.getType(), key, Reflections.getFieldValue(field, this.object)); | |
| 207 | + } catch (ConversionException cause) { | |
| 208 | + value = null; | |
| 266 | 209 | } |
| 210 | + | |
| 211 | + Reflections.setFieldValue(field, this.object, value); | |
| 267 | 212 | } |
| 268 | 213 | |
| 269 | 214 | private String getKey(Field field) { | ... | ... |