Author: hardy.ferentschik
Date: 2009-03-27 13:12:01 -0400 (Fri, 27 Mar 2009)
New Revision: 16226
Added:
validator/trunk/hibernate-validator/src/test/resources/META-INF/
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation.xml
Modified:
validator/trunk/hibernate-validator/pom.xml
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/HibernateValidationProvider.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
validator/trunk/hibernate-validator/src/main/xsd/validation-configuration-1.0.xsd
Log:
HV-111 work in progress
Modified: validator/trunk/hibernate-validator/pom.xml
===================================================================
--- validator/trunk/hibernate-validator/pom.xml 2009-03-27 15:40:12 UTC (rev 16225)
+++ validator/trunk/hibernate-validator/pom.xml 2009-03-27 17:12:01 UTC (rev 16226)
@@ -65,6 +65,9 @@
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
+ <resource>
+ <directory>src/main/xsd</directory>
+ </resource>
</resources>
<plugins>
<plugin>
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/HibernateValidationProvider.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/HibernateValidationProvider.java 2009-03-27
15:40:12 UTC (rev 16225)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/HibernateValidationProvider.java 2009-03-27
17:12:01 UTC (rev 16226)
@@ -49,7 +49,7 @@
"Illegal call to createSpecializedConfiguration() for a non suitable
provider"
);
}
- //cast protected by isSuitable call
+ //cast protected by isSuitable call
return configurationClass.cast( new ConfigurationImpl( this ) );
}
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-03-27
15:40:12 UTC (rev 16225)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-03-27
17:12:01 UTC (rev 16226)
@@ -18,9 +18,12 @@
package org.hibernate.validation.engine;
import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
-import java.util.Map;
import javax.validation.Configuration;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
@@ -31,12 +34,28 @@
import javax.validation.spi.BootstrapState;
import javax.validation.spi.ConfigurationState;
import javax.validation.spi.ValidationProvider;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import org.slf4j.Logger;
+import org.xml.sax.SAXException;
+
+import org.hibernate.validation.engine.resolver.DefaultTraversableResolver;
+import org.hibernate.validation.util.LoggerFactory;
+import org.hibernate.validation.util.ReflectionHelper;
import org.hibernate.validation.util.Version;
-import org.hibernate.validation.engine.resolver.DefaultTraversableResolver;
+import org.hibernate.validation.xml.ValidationConfigType;
/**
+ * Hibernate specific <code>Configuration</code> implementation.
+ *
* @author Emmanuel Bernard
+ * @author Hardy Ferentschik
*/
public class ConfigurationImpl implements HibernateValidatorConfiguration,
ConfigurationState {
@@ -44,16 +63,22 @@
Version.touch();
}
+ private static final Logger log = LoggerFactory.make();
+ private static final String VALIDATION_XML_FILE = "/META-INF/validation.xml";
+ private static final String VALIDATION_CONFIGURATION_XSD =
"validation-configuration-1.0.xsd";
private static final MessageInterpolator defaultMessageInterpolator = new
ResourceBundleMessageInterpolator();
private static final TraversableResolver defaultTraversableResolver = new
DefaultTraversableResolver();
+ private final ValidationProvider provider;
+ private final ValidationProviderResolver providerResolver;
+ private final Map<String, String> configProperties = new HashMap<String,
String>();
+ private final Set<InputStream> mappings = new HashSet<InputStream>();
+
private MessageInterpolator messageInterpolator;
private ConstraintValidatorFactory constraintValidatorFactory = new
ConstraintValidatorFactoryImpl();
- private String configurationFile = "META-INF/validation.xml";
- private final ValidationProvider provider;
- private final ValidationProviderResolver providerResolver;
private TraversableResolver traversableResolver;
- private boolean ignoreXmlConfiguration;
+ private boolean ignoreXmlConfiguration = true; // false;
+ private Class<? extends Configuration<?>> providerClass = null;
public ConfigurationImpl(BootstrapState state) {
if ( state.getValidationProviderResolver() == null ) {
@@ -78,7 +103,8 @@
}
public HibernateValidatorConfiguration ignoreXmlConfiguration() {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
+ ignoreXmlConfiguration = true;
+ return this;
}
public ConfigurationImpl messageInterpolator(MessageInterpolator interpolator) {
@@ -97,21 +123,21 @@
}
public HibernateValidatorConfiguration addMapping(InputStream stream) {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
+ mappings.add( stream );
+ return this;
}
public HibernateValidatorConfiguration addProperty(String name, String value) {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
+ configProperties.put( name, value );
+ return this;
}
public ValidatorFactory buildValidatorFactory() {
+ parseValidationXml();
if ( isSpecificProvider() ) {
return provider.buildValidatorFactory( this );
}
else {
- //read provider name from configuration
- Class<? extends Configuration<?>> providerClass = null;
-
if ( providerClass != null ) {
for ( ValidationProvider provider : providerResolver.getValidationProviders() ) {
if ( provider.isSuitable( providerClass ) ) {
@@ -122,16 +148,12 @@
}
else {
List<ValidationProvider> providers =
providerResolver.getValidationProviders();
- assert providers.size() != 0; //I run therefore I am
+ assert providers.size() != 0; // I run therefore I am
return providers.get( 0 ).buildValidatorFactory( this );
}
}
}
- private boolean isSpecificProvider() {
- return provider != null;
- }
-
public boolean isIgnoreXmlConfiguration() {
return ignoreXmlConfiguration;
}
@@ -141,7 +163,7 @@
}
public Set<InputStream> getMappingStreams() {
- throw new UnsupportedOperationException( "TODO" );
+ return mappings;
}
public ConstraintValidatorFactory getConstraintValidatorFactory() {
@@ -153,11 +175,119 @@
}
public Map<String, String> getProperties() {
- throw new UnsupportedOperationException( "TODO" );
+ return configProperties;
}
public MessageInterpolator getDefaultMessageInterpolator() {
return defaultMessageInterpolator;
}
+ private boolean isSpecificProvider() {
+ return provider != null;
+ }
+
+ /**
+ * Tries to check whether a validation.xml file exists and parses it using JAXB
+ */
+ private void parseValidationXml() {
+ if ( ignoreXmlConfiguration ) {
+ log.info( "Ignoring XML configuration." );
+ return;
+ }
+
+ ValidationConfigType config = getValidationConfig();
+ if ( config == null ) {
+ return;
+ }
+
+ setMessageInterpolatorFromXml( config );
+ setTraversableResolverFromXml( config );
+ setProviderClassFromXml( config );
+ }
+
+ private void setMessageInterpolatorFromXml(ValidationConfigType config) {
+ String messageInterpolatorClass = config.getMessageInterpolator();
+ if ( messageInterpolatorClass != null ) {
+ try {
+ @SuppressWarnings("unchecked")
+ Class<MessageInterpolator> clazz = ( Class<MessageInterpolator> )
ReflectionHelper.classForName(
+ messageInterpolatorClass, this.getClass()
+ );
+ messageInterpolator = clazz.newInstance();
+ log.info( "Using {} as message interpolator.", messageInterpolatorClass );
+ }
+ catch ( Exception e ) {
+ throw new ValidationException( "Unable to instantiate message interpolator class
" + messageInterpolatorClass + "." );
+ }
+ }
+ }
+
+ private void setTraversableResolverFromXml(ValidationConfigType config) {
+ String traversableResolverClass = config.getTraversableResolver();
+ if ( traversableResolverClass != null ) {
+ try {
+ @SuppressWarnings("unchecked")
+ Class<TraversableResolver> clazz = ( Class<TraversableResolver> )
ReflectionHelper.classForName(
+ traversableResolverClass, this.getClass()
+ );
+ traversableResolver = clazz.newInstance();
+ log.info( "Using {} as traversable resolver", traversableResolverClass );
+ }
+ catch ( Exception e ) {
+ throw new ValidationException( "Unable to instantiate message interpolator class
" + traversableResolverClass + "." );
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private void setProviderClassFromXml(ValidationConfigType config) {
+ String providerClassName = config.getDefaultProvider();
+ if ( providerClassName != null ) {
+ try {
+ providerClass = ( Class<? extends Configuration<?>> )
ReflectionHelper.classForName(
+ providerClassName, this.getClass()
+ );
+ log.info( "Using {} as validation provider.", providerClassName );
+ }
+ catch ( Exception e ) {
+ throw new ValidationException( "Unable to instantiate validation provider class
" + providerClassName + "." );
+ }
+ }
+ }
+
+ private ValidationConfigType getValidationConfig() {
+ InputStream inputStream = this.getClass().getResourceAsStream( VALIDATION_XML_FILE );
+ if ( inputStream == null ) {
+ log.info( "No {} found. Using defaults.", VALIDATION_XML_FILE );
+ return null;
+ }
+
+ ValidationConfigType validationConfig = null;
+ Schema schema = getValidationConfigurationSchema();
+ try {
+ JAXBContext jc = JAXBContext.newInstance( ValidationConfigType.class );
+ Unmarshaller unmarshaller = jc.createUnmarshaller();
+ unmarshaller.setSchema( schema );
+ StreamSource stream = new StreamSource( inputStream );
+ JAXBElement<ValidationConfigType> root = unmarshaller.unmarshal( stream,
ValidationConfigType.class );
+ validationConfig = root.getValue();
+ }
+ catch ( JAXBException e ) {
+ log.error( "Error parsing validation.xml: {}", e.getMessage() );
+ }
+ return validationConfig;
+ }
+
+ private Schema getValidationConfigurationSchema() {
+ URL schemaUrl = this.getClass().getClassLoader().getResource(
VALIDATION_CONFIGURATION_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_XML_FILE,
e.getMessage() );
+ }
+ return schema;
+ }
}
Modified:
validator/trunk/hibernate-validator/src/main/xsd/validation-configuration-1.0.xsd
===================================================================
---
validator/trunk/hibernate-validator/src/main/xsd/validation-configuration-1.0.xsd 2009-03-27
15:40:12 UTC (rev 16225)
+++
validator/trunk/hibernate-validator/src/main/xsd/validation-configuration-1.0.xsd 2009-03-27
17:12:01 UTC (rev 16226)
@@ -6,7 +6,7 @@
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
version="1.0">
<xs:element name="validation-config"
type="validation-configType"/>
- <xs:complexType name="validation-configType">
+ <xs:complexType name="validation-configType" >
<xs:sequence>
<xs:element type="xs:string" name="default-provider"
minOccurs="0"/>
<xs:element type="xs:string"
name="message-interpolator" minOccurs="0"/>
Added: validator/trunk/hibernate-validator/src/test/resources/META-INF/validation.xml
===================================================================
--- validator/trunk/hibernate-validator/src/test/resources/META-INF/validation.xml
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation.xml 2009-03-27
17:12:01 UTC (rev 16226)
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
+
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configu...
validation-configuration-1.0.xsd"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+
<default-provider>org.hibernate.validation.engine.HibernateValidatorConfiguration</default-provider>
+
<message-interpolator>org.hibernate.validation.engine.ResourceBundleMessageInterpolator</message-interpolator>
+
<constraint-mapping>META-INF/validation/order-constraints.xml</constraint-mapping>
+</validation-config>
\ No newline at end of file