Author: epbernard
Date: 2009-06-15 22:56:04 -0400 (Mon, 15 Jun 2009)
New Revision: 16790
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Configuration.java
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ValidationProviderResolver.java
beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/ProviderSpecificBootstrap.java
beanvalidation/trunk/validation-api/src/main/java/javax/validation/spi/ValidationProvider.java
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/java/org/hibernate/validation/engine/xml/ValidationBootstrapParameters.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationXmlParser.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/resolver/CachedTraversableResolverTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
Log:
BVAL-164 Validation.byProvider now accept the provider implementation class
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Configuration.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Configuration.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Configuration.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -43,10 +43,11 @@
* <p/>
* The provider is selected in the following way:
* <ul>
- * <li>if a specific <code>Configuration</code> subclass is requested
programmatically using
- * <code>Validation.byProvider(Class)</code>, find the first provider
matching it</li>
- * <li>if a specific <code>Configuration</code> subclass is defined in
<i>META-INF/validation.xml</i>,
- * find the first provider matching it </li>
+ * <li>if a specific provider is requested programmatically using
+ * <code>Validation.byProvider(Class)</code>, find the first provider
implementing
+ * the provider class requested and use it</li>
+ * <li>if a specific provider is requested in
<i>META-INF/validation.xml</i>,
+ * find the first provider implementing the provider class requested and use
it</li>
* <li>otherwise, use the first provider returned by the
<code>ValidationProviderResolver<code></li>
* </ul>
* <p/>
@@ -128,7 +129,7 @@
* <p/>
* It is more appropriate to use, if available, the type-safe equivalent provided
* by a specific provider via its <code>Configuration<code> subclass.
- * <code>ValidatorFactory factory =
Validation.byProvider(ACMEConfiguration.class)
+ * <code>ValidatorFactory factory = Validation.byProvider(ACMEPrivoder.class)
* .configure()
* .providerSpecificProperty(ACMEState.FAST)
* .buildValidatorFactory();
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -69,13 +69,12 @@
* <p/>
* <li>
* The third approach allows you to specify explicitly and in
- * a type safe fashion the expected provider by
- * using its specific <code>Configuration</code> sub-interface.
+ * a type safe fashion the expected provider.
*
* Optionally you can choose a custom
<code>ValidationProviderResolver</code>.
* <pre>
* ACMEConfiguration configuration = Validation
- * .byProvider(ACMEConfiguration.class)
+ * .byProvider(ACMEProvider.class)
* .providerResolver( new MyResolverStrategy() ) // optionally set the provider
resolver
* .configure();
* ValidatorFactory factory = configuration.buildValidatorFactory();
@@ -127,7 +126,7 @@
* </pre>
* The provider can be specified in the XML configuration. If the XML
* configuration does not exsist or if no provider is specified,
- * the first available provider will be returned.
+ * the first available provider will be returned.
*
* @return instance building a generic <code>Configuration</code>
* compliant with the bootstrap state provided.
@@ -144,34 +143,34 @@
* <p/>
* <pre>
* ACMEConfiguration configuration =
- * Validation.byProvider(ACMEConfiguration.class)
+ * Validation.byProvider(ACMEProvider.class)
* .providerResolver( new MyResolverStrategy() )
* .configure();
* </pre>,
* where <code>ACMEConfiguration</code> is the
* <code>Configuration</code> sub interface uniquely identifying the
- * ACME Bean Validation provider.
+ * ACME Bean Validation provider. and ACMEProvider is the ValidationProvider
+ * implementation of the ACME provider.
*
- * @param configurationType the <code>Configuration</code> sub interface
- * uniquely defining the targeted provider.
+ * @param providerType the <code>ValidationProvider</code> implementation
type
*
* @return instance building a provider specific <code>Configuration</code>
* sub interface implementation.
*/
- public static <T extends Configuration<T>>
- ProviderSpecificBootstrap<T> byProvider(Class<T> configurationType) {
- return new ProviderSpecificBootstrapImpl<T>( configurationType );
+ public static <T extends Configuration<T>, U extends
ValidationProvider<T>>
+ ProviderSpecificBootstrap<T> byProvider(Class<U> providerType) {
+ return new ProviderSpecificBootstrapImpl<T, U>( providerType );
}
//private class, not exposed
- private static class ProviderSpecificBootstrapImpl<T extends
Configuration<T>>
+ private static class ProviderSpecificBootstrapImpl<T extends Configuration<T>,
U extends ValidationProvider<T>>
implements ProviderSpecificBootstrap<T> {
- private final Class<T> configurationType;
+ private final Class<U> validationProviderClass;
private ValidationProviderResolver resolver;
- public ProviderSpecificBootstrapImpl(Class<T> configurationType) {
- this.configurationType = configurationType;
+ public ProviderSpecificBootstrapImpl(Class<U> validationProviderClass) {
+ this.validationProviderClass = validationProviderClass;
}
/**
@@ -194,11 +193,12 @@
* @return a Configuration sub interface implementation
*/
public T configure() {
- if ( configurationType == null ) {
+ if ( validationProviderClass == null ) {
throw new ValidationException(
"builder is mandatory. Use Validation.byDefaultProvider() to use the generic
provider discovery mechanism"
);
}
+ //used mostly as a BootstrapState
GenericBootstrapImpl state = new GenericBootstrapImpl();
if ( resolver == null ) {
resolver = state.getDefaultValidationProviderResolver();
@@ -208,11 +208,13 @@
state.providerResolver( resolver );
}
for ( ValidationProvider provider : resolver.getValidationProviders() ) {
- if ( provider.isSuitable( configurationType ) ) {
- return provider.createSpecializedConfiguration( state, configurationType );
+ if ( validationProviderClass.isAssignableFrom( provider.getClass() ) ) {
+ ValidationProvider<T> specificProvider = validationProviderClass.cast(
provider );
+ return specificProvider.createSpecializedConfiguration( state );
+
}
}
- throw new ValidationException( "Unable to find provider: " +
configurationType );
+ throw new ValidationException( "Unable to find provider: " +
validationProviderClass );
}
}
@@ -268,24 +270,24 @@
//cache per classloader for an appropriate discovery
//keep them in a weak hashmap to avoid memory leaks and allow proper hot redeployment
//TODO use a WeakConcurrentHashMap
- private static final Map<ClassLoader, List<ValidationProvider>>
providersPerClassloader =
- new WeakHashMap<ClassLoader, List<ValidationProvider>>();
+ private static final Map<ClassLoader, List<ValidationProvider<?>>>
providersPerClassloader =
+ new WeakHashMap<ClassLoader, List<ValidationProvider<?>>>();
private static final String SERVICES_FILE = "META-INF/services/" +
ValidationProvider.class.getName();
- public List<ValidationProvider> getValidationProviders() {
+ public List<ValidationProvider<?>> getValidationProviders() {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
if ( classloader == null ) {
classloader = DefaultValidationProviderResolver.class.getClassLoader();
}
- List<ValidationProvider> providers;
+ List<ValidationProvider<?>> providers;
synchronized ( providersPerClassloader ) {
providers = providersPerClassloader.get( classloader );
}
if ( providers == null ) {
- providers = new ArrayList<ValidationProvider>();
+ providers = new ArrayList<ValidationProvider<?>>();
String name = null;
try {
Enumeration<URL> providerDefinitions = classloader.getResources( SERVICES_FILE
);
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ValidationProviderResolver.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ValidationProviderResolver.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ValidationProviderResolver.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -21,7 +21,7 @@
import javax.validation.spi.ValidationProvider;
/**
- * Determine the list of Bean Validation providers available in the runtime environment
+ * Determines the list of Bean Validation providers available in the runtime environment
* <p/>
* Bean Validation providers are identified by the presence of
* META-INF/services/javax.validation.spi.ValidationProvider
@@ -41,5 +41,5 @@
*
* @return list of validation providers.
*/
- List<ValidationProvider> getValidationProviders();
+ List<ValidationProvider<?>> getValidationProviders();
}
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/ProviderSpecificBootstrap.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/ProviderSpecificBootstrap.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/ProviderSpecificBootstrap.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -2,15 +2,18 @@
import javax.validation.ValidationProviderResolver;
import javax.validation.Configuration;
+import javax.validation.spi.ValidationProvider;
/**
* Defines the state used to bootstrap Bean Validation and
- * creates a provider specific Configuration. The specific Configuration
- * sub interface uniquely identifies a provider.
+ * creates a provider specific Configuration <code>T</code>.
* <p/>
- * The requested provider is the first provider suitable for T (as defined in
- * {@link javax.validation.spi.ValidationProvider#isSuitable(Class)}). The
- * list of providers evaluated is returned by {@link ValidationProviderResolver}.
+ * The specific Configuration is linked to the provider via the generic
+ * parameter of the ValidationProvider implementation.
+ * <p/>
+ * The requested provider is the first provider instance assignable to
+ * the requested provider type (known when ProviderSpecificBootstrap is built).
+ * The list of providers evaluated is returned by {@link ValidationProviderResolver}.
* If no ValidationProviderResolver is defined, the
* default ValidationProviderResolver strategy is used.
*
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/spi/ValidationProvider.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/spi/ValidationProvider.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/spi/ValidationProvider.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -26,38 +26,26 @@
* Implementations must have a public no-arg constructor. The construction of a provider
* should be as "lightweight" as possible.
*
+ * <code>T</code> represents the provider specific Configuration subclass
+ * which typically host provider's additional configuration methods.
+ *
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
-public interface ValidationProvider {
- /**
- * Return true if <code>configurationClass</code> is the uniquely
identifying
- * Configuration subclass for this provider
- *
- * @param configurationClass targeted configuration class.
- *
- * @return <code>true</code> if <code>configurationClass</code>
is the Bean Validation Provider
- * sub-interface for Configuration
- */
- boolean isSuitable(Class<? extends Configuration<?>> configurationClass);
+public interface ValidationProvider<T extends Configuration<T>> {
/**
- * Returns a Configuration instance implementing the
- * <code>configurationClass</code> interface.
+ * Returns a Configuration instance implementing <code>T</code>,
+ * the <code>Configuration</code> subinterface.
* The returned Configuration instance must use the current provider
(<code>this</code>)
* to build the ValidatorFactory instance.
* <p/>
- * This method can only be called on providers returning true on
- * <code>#isSuitable(configurationClass)</code>
*
- * @param configurationClass the Configuration class type
* @param state bootstrap state
*
* @return specific Configuration implementation
*/
- <T extends Configuration<T>> T createSpecializedConfiguration(
- BootstrapState state,
- Class<T> configurationClass);
+ T createSpecializedConfiguration(BootstrapState state);
/**
* Returns a Configuration instance. This instance is not bound to
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-06-15
21:41:42 UTC (rev 16789)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/HibernateValidationProvider.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -34,20 +34,11 @@
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
-public class HibernateValidationProvider implements ValidationProvider {
+public class HibernateValidationProvider implements
ValidationProvider<HibernateValidatorConfiguration> {
- public boolean isSuitable(Class<? extends Configuration<?>> builderClass) {
- return builderClass == HibernateValidatorConfiguration.class;
- }
-
- public <T extends Configuration<T>> T
createSpecializedConfiguration(BootstrapState state, Class<T> configurationClass) {
- if ( !isSuitable( configurationClass ) ) {
- throw new ValidationException(
- "Illegal call to createSpecializedConfiguration() for a non suitable
provider"
- );
- }
+ public HibernateValidatorConfiguration createSpecializedConfiguration(BootstrapState
state) {
//cast protected by isSuitable call
- return configurationClass.cast( new ConfigurationImpl( this ) );
+ return HibernateValidatorConfiguration.class.cast( new ConfigurationImpl( this ) );
}
public Configuration<?> createGenericConfiguration(BootstrapState state) {
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-06-15
21:41:42 UTC (rev 16789)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -119,19 +119,20 @@
factory = validationBootstrapParameters.provider.buildValidatorFactory( this );
}
else {
- if ( validationBootstrapParameters.providerClass != null ) {
+ final Class<? extends ValidationProvider<?>> providerClass =
validationBootstrapParameters.providerClass;
+ if ( providerClass != null ) {
for ( ValidationProvider provider : providerResolver.getValidationProviders() ) {
- if ( provider.isSuitable( validationBootstrapParameters.providerClass ) ) {
+ if ( providerClass.isAssignableFrom( provider.getClass() ) ) {
factory = provider.buildValidatorFactory( this );
break;
}
}
if ( factory == null ) {
- throw new ValidationException( "Unable to find provider: " +
validationBootstrapParameters.providerClass );
+ throw new ValidationException( "Unable to find provider: " + providerClass
);
}
}
else {
- List<ValidationProvider> providers =
providerResolver.getValidationProviders();
+ List<ValidationProvider<?>> providers =
providerResolver.getValidationProviders();
assert providers.size() != 0; // I run therefore I am
factory = providers.get( 0 ).buildValidatorFactory( this );
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationBootstrapParameters.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationBootstrapParameters.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationBootstrapParameters.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -36,7 +36,7 @@
public MessageInterpolator messageInterpolator;
public TraversableResolver traversableResolver;
public ValidationProvider provider;
- public Class<? extends Configuration<?>> providerClass = null;
+ public Class<? extends ValidationProvider<?>> providerClass = null;
public final Map<String, String> configProperties = new HashMap<String,
String>();
public final Set<InputStream> mappings = new HashSet<InputStream>();
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationXmlParser.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationXmlParser.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationXmlParser.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -24,6 +24,7 @@
import javax.validation.MessageInterpolator;
import javax.validation.TraversableResolver;
import javax.validation.ValidationException;
+import javax.validation.spi.ValidationProvider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
@@ -192,7 +193,7 @@
String providerClassName = config.getDefaultProvider();
if ( providerClassName != null ) {
try {
- xmlParamters.providerClass = ( Class<? extends Configuration<?>> )
ReflectionHelper.classForName(
+ xmlParamters.providerClass = ( Class<? extends ValidationProvider<?>> )
ReflectionHelper.classForName(
providerClassName, this.getClass()
);
log.info( "Using {} as validation provider.", providerClassName );
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -38,6 +38,7 @@
import org.hibernate.validation.engine.HibernateValidatorConfiguration;
import org.hibernate.validation.engine.ValidatorFactoryImpl;
import org.hibernate.validation.engine.Customer;
+import org.hibernate.validation.HibernateValidationProvider;
/**
* Tests the Bean Validation bootstrapping.
@@ -49,7 +50,7 @@
@Test
public void testBootstrapAsServiceWithBuilder() {
HibernateValidatorConfiguration configuration = Validation
- .byProvider( HibernateValidatorConfiguration.class )
+ .byProvider( HibernateValidationProvider.class )
.configure();
assertDefaultBuilderAndFactory( configuration );
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/resolver/CachedTraversableResolverTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/resolver/CachedTraversableResolverTest.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/resolver/CachedTraversableResolverTest.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -30,6 +30,7 @@
import static org.testng.Assert.*;
import org.hibernate.validation.engine.HibernateValidatorConfiguration;
+import org.hibernate.validation.HibernateValidationProvider;
/**
* @author Emmanuel Bernard
@@ -39,7 +40,7 @@
@Test
public void testCache() {
TraversableResolver resolver = new AskOnceTR();
- ValidatorFactory factory = Validation.byProvider( HibernateValidatorConfiguration.class
)
+ ValidatorFactory factory = Validation.byProvider( HibernateValidationProvider.class )
.configure().traversableResolver( resolver )
.buildValidatorFactory();
Suit suit = new Suit();
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java 2009-06-15
21:41:42 UTC (rev 16789)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java 2009-06-16
02:56:04 UTC (rev 16790)
@@ -31,6 +31,7 @@
import static org.testng.Assert.assertTrue;
import org.hibernate.validation.engine.HibernateValidatorConfiguration;
+import org.hibernate.validation.HibernateValidationProvider;
/**
* Tests for the <code>ReflectionHelper</code>.
@@ -48,7 +49,7 @@
public static Validator getValidator() {
if ( hibernateValidator == null ) {
HibernateValidatorConfiguration configuration = Validation
- .byProvider( HibernateValidatorConfiguration.class )
+ .byProvider( HibernateValidationProvider.class )
.configure();
hibernateValidator = configuration.buildValidatorFactory().getValidator();
}
@@ -66,7 +67,7 @@
Thread.currentThread().setContextClassLoader( new CustomValidationXmlClassLoader( path
) );
HibernateValidatorConfiguration configuration = Validation
- .byProvider( HibernateValidatorConfiguration.class )
+ .byProvider( HibernateValidationProvider.class )
.configure();
return configuration.buildValidatorFactory().getValidator();
}
@@ -78,7 +79,7 @@
Thread.currentThread().setContextClassLoader( new IgnoringValidationXmlClassLoader()
);
HibernateValidatorConfiguration configuration = Validation
- .byProvider( HibernateValidatorConfiguration.class )
+ .byProvider( HibernateValidationProvider.class )
.configure();
return configuration.buildValidatorFactory().getValidator();
}