Author: hardy.ferentschik
Date: 2009-04-03 10:36:39 -0400 (Fri, 03 Apr 2009)
New Revision: 16256
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/order-constraints.xml
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
Log:
HV-112
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-04-03
09:09:58 UTC (rev 16255)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-04-03
14:36:39 UTC (rev 16256)
@@ -129,24 +129,32 @@
public ValidatorFactory buildValidatorFactory() {
parseValidationXml();
+ ValidatorFactory factory = null;
if ( isSpecificProvider() ) {
- return parameterHolder.provider.buildValidatorFactory( this );
+ factory = parameterHolder.provider.buildValidatorFactory( this );
}
else {
if ( parameterHolder.providerClass != null ) {
for ( ValidationProvider provider : providerResolver.getValidationProviders() ) {
if ( provider.isSuitable( parameterHolder.providerClass ) ) {
- return provider.buildValidatorFactory( this );
+ factory = provider.buildValidatorFactory( this );
+ break;
}
}
- throw new ValidationException( "Unable to find provider: " +
parameterHolder.providerClass );
+ if ( factory == null ) {
+ throw new ValidationException( "Unable to find provider: " +
parameterHolder.providerClass );
+ }
}
else {
List<ValidationProvider> providers =
providerResolver.getValidationProviders();
assert providers.size() != 0; // I run therefore I am
- return providers.get( 0 ).buildValidatorFactory( this );
+ factory = providers.get( 0 ).buildValidatorFactory( this );
}
}
+
+ // reset the param holder
+ parameterHolder = new ParameterHolder();
+ return factory;
}
public boolean isIgnoreXmlConfiguration() {
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-03
09:09:58 UTC (rev 16255)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-03
14:36:39 UTC (rev 16256)
@@ -17,12 +17,16 @@
*/
package org.hibernate.validation.engine;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
+import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
import javax.validation.TraversableResolver;
+import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorContext;
import javax.validation.ValidatorFactory;
@@ -39,7 +43,12 @@
import org.xml.sax.SAXException;
import org.hibernate.validation.util.LoggerFactory;
+import org.hibernate.validation.util.ReflectionHelper;
+import org.hibernate.validation.xml.BeanType;
+import org.hibernate.validation.xml.ClassType;
import org.hibernate.validation.xml.ConstraintMappingsType;
+import org.hibernate.validation.xml.FieldType;
+import org.hibernate.validation.xml.GetterType;
/**
* @author Emmanuel Bernard
@@ -55,6 +64,7 @@
private final TraversableResolver traversableResolver;
private final ConstraintValidatorFactory constraintValidatorFactory;
private final ConstraintHelper constraintHelper = new ConstraintHelper();
+ private static final String PACKAGE_SEPERATOR = ".";
public ValidatorFactoryImpl(ConfigurationState configurationState) {
this.messageInterpolator = configurationState.getMessageInterpolator();
@@ -89,15 +99,84 @@
);
}
+ public Schema getMappingSchema() {
+ URL schemaUrl = this.getClass().getClassLoader().getResource( VALIDATION_MAPPING_XSD
);
+ SchemaFactory sf = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI );
+ Schema schema = null;
+ try {
+ schema = sf.newSchema( schemaUrl );
+ }
+ catch ( SAXException e ) {
+ log.warn( "Unable to create schema for {}: {}", VALIDATION_MAPPING_XSD,
e.getMessage() );
+ }
+ return schema;
+ }
+
private void parseMappingFiles(Set<InputStream> mappingStreams) {
+ Set<Class<?>> processedClasses = new HashSet<Class<?>>();
for ( InputStream in : mappingStreams ) {
- ConstraintMappingsType mappings = getValidationConfig( in );
+ try {
+ ConstraintMappingsType mappings = getValidationConfig( in );
+ String defaultPackage = mappings.getDefaultPackage();
+ for ( BeanType bean : mappings.getBean() ) {
+ Class<?> beanClass = getBeanClass( bean.getClazz(), defaultPackage );
+ if ( processedClasses.contains( beanClass ) ) {
+ throw new ValidationException( beanClass.getName() + " has already be
configured in xml." );
+ }
+ boolean ignoreAnnotations = bean.isIgnoreAnnotations();
+ @SuppressWarnings("unchecked")
+ BeanMetaDataImpl<?> metaData = new BeanMetaDataImpl( beanClass,
constraintHelper );
+ parseClassLevelOverrides( metaData, bean.getClassType() );
+ parseFieldLevelOverrides( metaData, bean.getField() );
+ parsePropertyLevelOverrides( metaData, bean.getGetter() );
+ processedClasses.add( beanClass );
+ }
+ }
+ finally {
+ try {
+ in.close();
+ }
+ catch ( IOException e ) {
+ log.warn( "Error closing input stream: {}", e.getMessage() );
+ }
+ }
}
}
+ private void parseFieldLevelOverrides(BeanMetaDataImpl<?> metaData,
List<FieldType> field) {
+ //To change body of created methods use File | Settings | File Templates.
+ }
+
+ private void parsePropertyLevelOverrides(BeanMetaDataImpl<?> metaData,
List<GetterType> getter) {
+ //To change body of created methods use File | Settings | File Templates.
+ }
+
+ private void parseClassLevelOverrides(BeanMetaDataImpl<?> metaData, ClassType
classType) {
+ //To change body of created methods use File | Settings | File Templates.
+ }
+
+ private Class getBeanClass(String clazz, String defaultPackage) {
+ String fullyQualifiedClass;
+ if ( isQualifiedClass( clazz ) ) {
+ fullyQualifiedClass = clazz;
+ }
+ else {
+ fullyQualifiedClass = defaultPackage + PACKAGE_SEPERATOR + clazz;
+ }
+ try {
+ return ReflectionHelper.classForName( fullyQualifiedClass, this.getClass() );
+ }
+ catch ( Exception e ) {
+ throw new ValidationException( "Unable to instantiate class " +
fullyQualifiedClass );
+ }
+ }
+
+ private boolean isQualifiedClass(String clazz) {
+ return clazz.contains( PACKAGE_SEPERATOR );
+ }
+
private ConstraintMappingsType getValidationConfig(InputStream in) {
-
- ConstraintMappingsType constraintMappings = null;
+ ConstraintMappingsType constraintMappings;
Schema schema = getMappingSchema();
try {
JAXBContext jc = JAXBContext.newInstance( ConstraintMappingsType.class );
@@ -108,21 +187,10 @@
constraintMappings = root.getValue();
}
catch ( JAXBException e ) {
- log.error( "Error parsing mapping file: {}", e.getMessage() );
+ String msg = "Error parsing mapping file.";
+ log.error( msg );
+ throw new ValidationException( msg, e );
}
return constraintMappings;
}
-
- public Schema getMappingSchema() {
- URL schemaUrl = this.getClass().getClassLoader().getResource( VALIDATION_MAPPING_XSD
);
- SchemaFactory sf = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI );
- Schema schema = null;
- try {
- schema = sf.newSchema( schemaUrl );
- }
- catch ( SAXException e ) {
- log.warn( "Unable to create schema for {}: {}", VALIDATION_MAPPING_XSD,
e.getMessage() );
- }
- return schema;
- }
}
Modified:
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/order-constraints.xml
===================================================================
---
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/order-constraints.xml 2009-04-03
09:09:58 UTC (rev 16255)
+++
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/order-constraints.xml 2009-04-03
14:36:39 UTC (rev 16256)
@@ -0,0 +1,4 @@
+<constraint-mappings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping
validation-mapping-1.0.xsd"
+
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
+</constraint-mappings>
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
===================================================================
---
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-03
09:09:58 UTC (rev 16255)
+++
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-03
14:36:39 UTC (rev 16256)
@@ -1,8 +1,8 @@
<constraint-mappings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping
validation-mapping-1.0.xsd"
-
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
- <default-package>com.acme.app.domain</default-package>
- <bean class="Customer" ignore-annotations="false">
+
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping
validation-mapping-1.0.xsd"
+
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
+
<default-package>org.hibernate.validation.engine.groups</default-package>
+ <bean class="User" ignore-annotations="false">
<field name="firstName">
<constraint annotation="com.acme.app.constraint.LooksLike">
<element name="patterns">