Commit ee28b5f421d99446242ae4fb4f8a742f913c203a
1 parent
2ebb9a26
Exists in
master
Aprimoramento para lançar exceção no caso do arquivo de configuração
de um atributo anotado com @NotNull não existir
Showing
1 changed file
with
39 additions
and
26 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
@@ -39,13 +39,14 @@ package br.gov.frameworkdemoiselle.internal.configuration; | @@ -39,13 +39,14 @@ package br.gov.frameworkdemoiselle.internal.configuration; | ||
39 | 39 | ||
40 | import static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM; | 40 | import static br.gov.frameworkdemoiselle.configuration.ConfigType.SYSTEM; |
41 | 41 | ||
42 | -import java.io.FileNotFoundException; | ||
43 | import java.io.Serializable; | 42 | import java.io.Serializable; |
44 | import java.lang.reflect.Field; | 43 | import java.lang.reflect.Field; |
45 | import java.lang.reflect.InvocationTargetException; | 44 | import java.lang.reflect.InvocationTargetException; |
46 | import java.lang.reflect.Method; | 45 | import java.lang.reflect.Method; |
47 | import java.util.List; | 46 | import java.util.List; |
48 | 47 | ||
48 | +import javax.validation.constraints.NotNull; | ||
49 | + | ||
49 | import org.apache.commons.configuration.AbstractConfiguration; | 50 | import org.apache.commons.configuration.AbstractConfiguration; |
50 | import org.apache.commons.configuration.FileConfiguration; | 51 | import org.apache.commons.configuration.FileConfiguration; |
51 | import org.apache.commons.configuration.PropertiesConfiguration; | 52 | import org.apache.commons.configuration.PropertiesConfiguration; |
@@ -89,9 +90,12 @@ public class ConfigurationLoader implements Serializable { | @@ -89,9 +90,12 @@ public class ConfigurationLoader implements Serializable { | ||
89 | 90 | ||
90 | loadType(); | 91 | loadType(); |
91 | loadResource(); | 92 | loadResource(); |
92 | - loadPrefix(); | ||
93 | loadConfiguration(); | 93 | loadConfiguration(); |
94 | - loadFields(); | 94 | + |
95 | + if (this.configuration != null) { | ||
96 | + loadPrefix(); | ||
97 | + loadFields(); | ||
98 | + } | ||
95 | 99 | ||
96 | validateValues(); | 100 | validateValues(); |
97 | } | 101 | } |
@@ -104,37 +108,36 @@ public class ConfigurationLoader implements Serializable { | @@ -104,37 +108,36 @@ public class ConfigurationLoader implements Serializable { | ||
104 | } | 108 | } |
105 | 109 | ||
106 | private void loadConfiguration() { | 110 | private void loadConfiguration() { |
107 | - try { | ||
108 | - AbstractConfiguration conf; | 111 | + AbstractConfiguration conf; |
109 | 112 | ||
110 | - switch (this.type) { | ||
111 | - case SYSTEM: | ||
112 | - conf = new SystemConfiguration(); | ||
113 | - break; | 113 | + switch (this.type) { |
114 | + case SYSTEM: | ||
115 | + conf = new SystemConfiguration(); | ||
116 | + break; | ||
114 | 117 | ||
115 | - case XML: | ||
116 | - conf = new XMLConfiguration(); | ||
117 | - break; | 118 | + case XML: |
119 | + conf = new XMLConfiguration(); | ||
120 | + break; | ||
118 | 121 | ||
119 | - default: | ||
120 | - conf = new PropertiesConfiguration(); | ||
121 | - break; | ||
122 | - } | 122 | + default: |
123 | + conf = new PropertiesConfiguration(); | ||
124 | + break; | ||
125 | + } | ||
123 | 126 | ||
124 | - conf.setDelimiterParsingDisabled(true); | 127 | + conf.setDelimiterParsingDisabled(true); |
125 | 128 | ||
126 | - if (conf instanceof FileConfiguration) { | ||
127 | - ((FileConfiguration) conf).setURL(Reflections.getResourceAsURL(this.resource)); | ||
128 | - ((FileConfiguration) conf).load(); | ||
129 | - } | 129 | + if (conf instanceof FileConfiguration) { |
130 | + ((FileConfiguration) conf).setURL(Reflections.getResourceAsURL(this.resource)); | ||
130 | 131 | ||
131 | - this.configuration = conf; | 132 | + try { |
133 | + ((FileConfiguration) conf).load(); | ||
132 | 134 | ||
133 | - } catch (FileNotFoundException cause) { | ||
134 | - cause.printStackTrace(); | ||
135 | - } catch (org.apache.commons.configuration.ConfigurationException e) { | ||
136 | - e.printStackTrace(); | 135 | + } catch (org.apache.commons.configuration.ConfigurationException cause) { |
136 | + conf = null; | ||
137 | + } | ||
137 | } | 138 | } |
139 | + | ||
140 | + this.configuration = conf; | ||
138 | } | 141 | } |
139 | 142 | ||
140 | private void loadResource() { | 143 | private void loadResource() { |
@@ -211,5 +214,15 @@ public class ConfigurationLoader implements Serializable { | @@ -211,5 +214,15 @@ public class ConfigurationLoader implements Serializable { | ||
211 | } | 214 | } |
212 | 215 | ||
213 | private void validateValues() { | 216 | private void validateValues() { |
217 | + for (Field field : getFields()) { | ||
218 | + validateValue(field); | ||
219 | + } | ||
220 | + } | ||
221 | + | ||
222 | + private void validateValue(Field field) { | ||
223 | + if (field.isAnnotationPresent(NotNull.class) && Reflections.getFieldValue(field, this.object) == null) { | ||
224 | + throw new ConfigurationException(""); | ||
225 | + // TODO: Pegar mensagem do Bundle e verificar como as mensagens de log estão implementadas | ||
226 | + } | ||
214 | } | 227 | } |
215 | } | 228 | } |