Commit c8dc5e0c5628f6e554b43b40531ae15415ebfb40
1 parent
807b241d
Exists in
master
Adicionada na classe ConfigurationLoader a funcionalidade que permite a
utilização dos validadores de atributo da especificação Bean Validation (JSR 303). Foi adicionada ao pom a dependência para a implementação de referência dessa especificação (Hibernate Validator) para realização de testes.
Showing
2 changed files
with
27 additions
and
4 deletions
Show diff stats
impl/core/pom.xml
| ... | ... | @@ -206,6 +206,11 @@ |
| 206 | 206 | <artifactId>weld-se-core</artifactId> |
| 207 | 207 | <scope>test</scope> |
| 208 | 208 | </dependency> |
| 209 | + <dependency> | |
| 210 | + <groupId>org.hibernate</groupId> | |
| 211 | + <artifactId>hibernate-validator</artifactId> | |
| 212 | + <scope>test</scope> | |
| 213 | + </dependency> | |
| 209 | 214 | |
| 210 | 215 | <!-- |
| 211 | 216 | <dependency> | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationLoader.java
| ... | ... | @@ -43,8 +43,14 @@ import java.io.Serializable; |
| 43 | 43 | import java.lang.reflect.Field; |
| 44 | 44 | import java.util.Collection; |
| 45 | 45 | import java.util.HashSet; |
| 46 | +import java.util.Iterator; | |
| 47 | +import java.util.Set; | |
| 46 | 48 | |
| 47 | -import javax.validation.constraints.NotNull; | |
| 49 | +import javax.validation.ConstraintViolation; | |
| 50 | +import javax.validation.ConstraintViolationException; | |
| 51 | +import javax.validation.Validation; | |
| 52 | +import javax.validation.Validator; | |
| 53 | +import javax.validation.ValidatorFactory; | |
| 48 | 54 | |
| 49 | 55 | import org.apache.commons.configuration.AbstractConfiguration; |
| 50 | 56 | import org.apache.commons.configuration.FileConfiguration; |
| ... | ... | @@ -275,10 +281,22 @@ public class ConfigurationLoader implements Serializable { |
| 275 | 281 | } |
| 276 | 282 | } |
| 277 | 283 | |
| 284 | + @SuppressWarnings({ "rawtypes", "unchecked" }) | |
| 278 | 285 | private void validateValue(Field field, Object value) { |
| 279 | - if (field.isAnnotationPresent(NotNull.class) && value == null) { | |
| 280 | - throw new ConfigurationException(getBundle().getString("configuration-attribute-is-mandatory", | |
| 281 | - this.prefix + getKey(field), this.resource), new NullPointerException()); | |
| 286 | + ValidatorFactory dfv = Validation.buildDefaultValidatorFactory(); | |
| 287 | + Validator validator = dfv.getValidator(); | |
| 288 | + | |
| 289 | + Set violations = validator.validateProperty(this.object, field.getName()); | |
| 290 | + | |
| 291 | + StringBuffer message = new StringBuffer(); | |
| 292 | + | |
| 293 | + if (!violations.isEmpty()) { | |
| 294 | + for (Iterator iter = violations.iterator(); iter.hasNext();) { | |
| 295 | + ConstraintViolation violation = (ConstraintViolation)iter.next(); | |
| 296 | + message.append(field.toGenericString() + " " + violation.getMessage() + "\n"); | |
| 297 | + } | |
| 298 | + | |
| 299 | + throw new ConfigurationException(message.toString(), new ConstraintViolationException(violations)); | |
| 282 | 300 | } |
| 283 | 301 | } |
| 284 | 302 | ... | ... |