Commit a7f2b75f71b715c8fa1f760e4b1af87ddc2ed31e
1 parent
a7fec2fe
Exists in
master
Organização do código fonte
Showing
1 changed file
with
42 additions
and
17 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
| ... | ... | @@ -44,6 +44,7 @@ import java.io.Serializable; |
| 44 | 44 | import java.lang.reflect.Field; |
| 45 | 45 | import java.lang.reflect.InvocationTargetException; |
| 46 | 46 | import java.lang.reflect.Method; |
| 47 | +import java.util.List; | |
| 47 | 48 | |
| 48 | 49 | import org.apache.commons.configuration.AbstractConfiguration; |
| 49 | 50 | import org.apache.commons.configuration.FileConfiguration; |
| ... | ... | @@ -69,6 +70,8 @@ public class ConfigurationLoader implements Serializable { |
| 69 | 70 | |
| 70 | 71 | private static final long serialVersionUID = 1L; |
| 71 | 72 | |
| 73 | + private Object object; | |
| 74 | + | |
| 72 | 75 | private ConfigType type; |
| 73 | 76 | |
| 74 | 77 | private String resource; |
| ... | ... | @@ -77,15 +80,26 @@ public class ConfigurationLoader implements Serializable { |
| 77 | 80 | |
| 78 | 81 | private org.apache.commons.configuration.Configuration configuration; |
| 79 | 82 | |
| 83 | + private List<Field> fields; | |
| 84 | + | |
| 80 | 85 | public void load(Object object) throws ConfigurationException { |
| 81 | - loadType(object); | |
| 82 | - loadResource(object); | |
| 83 | - loadPrefix(object); | |
| 86 | + this.object = object; | |
| 87 | + | |
| 88 | + validateFields(); | |
| 89 | + | |
| 90 | + loadType(); | |
| 91 | + loadResource(); | |
| 92 | + loadPrefix(); | |
| 84 | 93 | loadConfiguration(); |
| 85 | - loadFields(object); | |
| 94 | + loadFields(); | |
| 95 | + | |
| 96 | + validateValues(); | |
| 97 | + } | |
| 98 | + | |
| 99 | + private void validateFields() { | |
| 86 | 100 | } |
| 87 | 101 | |
| 88 | - private void loadType(Object object) { | |
| 102 | + private void loadType() { | |
| 89 | 103 | this.type = object.getClass().getAnnotation(Configuration.class).type(); |
| 90 | 104 | } |
| 91 | 105 | |
| ... | ... | @@ -123,26 +137,34 @@ public class ConfigurationLoader implements Serializable { |
| 123 | 137 | } |
| 124 | 138 | } |
| 125 | 139 | |
| 126 | - private void loadResource(Object object) { | |
| 140 | + private void loadResource() { | |
| 127 | 141 | if (this.type != SYSTEM) { |
| 128 | - String name = object.getClass().getAnnotation(Configuration.class).resource(); | |
| 142 | + String name = this.object.getClass().getAnnotation(Configuration.class).resource(); | |
| 129 | 143 | String extension = this.type.toString().toLowerCase(); |
| 130 | 144 | |
| 131 | 145 | this.resource = name + "." + extension; |
| 132 | 146 | } |
| 133 | 147 | } |
| 134 | 148 | |
| 135 | - private void loadPrefix(Object object) { | |
| 136 | - this.prefix = object.getClass().getAnnotation(Configuration.class).prefix(); | |
| 149 | + private void loadPrefix() { | |
| 150 | + this.prefix = this.object.getClass().getAnnotation(Configuration.class).prefix(); | |
| 137 | 151 | } |
| 138 | 152 | |
| 139 | - private void loadFields(Object object) { | |
| 140 | - for (Field field : Reflections.getNonStaticFields(object.getClass())) { | |
| 141 | - loadField(field, object); | |
| 153 | + private void loadFields() { | |
| 154 | + for (Field field : getFields()) { | |
| 155 | + loadField(field); | |
| 142 | 156 | } |
| 143 | 157 | } |
| 144 | 158 | |
| 145 | - private void loadField(Field field, Object object) { | |
| 159 | + private List<Field> getFields() { | |
| 160 | + if (this.fields == null) { | |
| 161 | + this.fields = Reflections.getNonStaticFields(this.object.getClass()); | |
| 162 | + } | |
| 163 | + | |
| 164 | + return this.fields; | |
| 165 | + } | |
| 166 | + | |
| 167 | + private void loadField(Field field) { | |
| 146 | 168 | if (hasIgnore(field)) { |
| 147 | 169 | return; |
| 148 | 170 | } |
| ... | ... | @@ -152,12 +174,12 @@ public class ConfigurationLoader implements Serializable { |
| 152 | 174 | Class<?> fieldType = field.getType(); |
| 153 | 175 | String methodName = "get" + Strings.firstToUpper(fieldType.getSimpleName()); |
| 154 | 176 | |
| 155 | - Method method = configuration.getClass().getMethod(methodName, String.class, fieldType); | |
| 156 | - Object value = method.invoke(configuration, key, Reflections.getFieldValue(field, object)); | |
| 177 | + Method method = this.configuration.getClass().getMethod(methodName, String.class, fieldType); | |
| 178 | + Object value = method.invoke(this.configuration, key, Reflections.getFieldValue(field, this.object)); | |
| 157 | 179 | // TODO Se não achar no arquivo de configuração vai dar a falsa sensação que o valor padrão foi carregado de |
| 158 | 180 | // lá. Corrigir isso! |
| 159 | 181 | |
| 160 | - Reflections.setFieldValue(field, object, value); | |
| 182 | + Reflections.setFieldValue(field, this.object, value); | |
| 161 | 183 | |
| 162 | 184 | } catch (SecurityException cause) { |
| 163 | 185 | cause.printStackTrace(); |
| ... | ... | @@ -177,7 +199,7 @@ public class ConfigurationLoader implements Serializable { |
| 177 | 199 | |
| 178 | 200 | if (field.isAnnotationPresent(Name.class)) { |
| 179 | 201 | key += field.getAnnotation(Name.class).value(); |
| 180 | - }else{ | |
| 202 | + } else { | |
| 181 | 203 | key += field.getName(); |
| 182 | 204 | } |
| 183 | 205 | |
| ... | ... | @@ -187,4 +209,7 @@ public class ConfigurationLoader implements Serializable { |
| 187 | 209 | private boolean hasIgnore(Field field) { |
| 188 | 210 | return field.isAnnotationPresent(Ignore.class); |
| 189 | 211 | } |
| 212 | + | |
| 213 | + private void validateValues() { | |
| 214 | + } | |
| 190 | 215 | } | ... | ... |