Search in sources :

Example 1 with ProviderSpecificBootstrap

use of javax.validation.bootstrap.ProviderSpecificBootstrap in project spring-framework by spring-projects.

the class LocalValidatorFactoryBean method afterPropertiesSet.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void afterPropertiesSet() {
    Configuration<?> configuration;
    if (this.providerClass != null) {
        ProviderSpecificBootstrap bootstrap = Validation.byProvider(this.providerClass);
        if (this.validationProviderResolver != null) {
            bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
        }
        configuration = bootstrap.configure();
    } else {
        GenericBootstrap bootstrap = Validation.byDefaultProvider();
        if (this.validationProviderResolver != null) {
            bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
        }
        configuration = bootstrap.configure();
    }
    // Try Hibernate Validator 5.2's externalClassLoader(ClassLoader) method
    if (this.applicationContext != null) {
        try {
            Method eclMethod = configuration.getClass().getMethod("externalClassLoader", ClassLoader.class);
            ReflectionUtils.invokeMethod(eclMethod, configuration, this.applicationContext.getClassLoader());
        } catch (NoSuchMethodException ex) {
        // Ignore - no Hibernate Validator 5.2+ or similar provider
        }
    }
    MessageInterpolator targetInterpolator = this.messageInterpolator;
    if (targetInterpolator == null) {
        targetInterpolator = configuration.getDefaultMessageInterpolator();
    }
    configuration.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));
    if (this.traversableResolver != null) {
        configuration.traversableResolver(this.traversableResolver);
    }
    ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory;
    if (targetConstraintValidatorFactory == null && this.applicationContext != null) {
        targetConstraintValidatorFactory = new SpringConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
    }
    if (targetConstraintValidatorFactory != null) {
        configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
    }
    if (this.parameterNameDiscoverer != null) {
        configureParameterNameProviderIfPossible(configuration);
    }
    if (this.mappingLocations != null) {
        for (Resource location : this.mappingLocations) {
            try {
                configuration.addMapping(location.getInputStream());
            } catch (IOException ex) {
                throw new IllegalStateException("Cannot read mapping resource: " + location);
            }
        }
    }
    for (Map.Entry<String, String> entry : this.validationPropertyMap.entrySet()) {
        configuration.addProperty(entry.getKey(), entry.getValue());
    }
    // Allow for custom post-processing before we actually build the ValidatorFactory.
    postProcessConfiguration(configuration);
    this.validatorFactory = configuration.buildValidatorFactory();
    setTargetValidator(this.validatorFactory.getValidator());
}
Also used : ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) Resource(org.springframework.core.io.Resource) Method(java.lang.reflect.Method) IOException(java.io.IOException) ProviderSpecificBootstrap(javax.validation.bootstrap.ProviderSpecificBootstrap) GenericBootstrap(javax.validation.bootstrap.GenericBootstrap) HashMap(java.util.HashMap) Map(java.util.Map) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(javax.validation.MessageInterpolator)

Aggregations

IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConstraintValidatorFactory (javax.validation.ConstraintValidatorFactory)1 MessageInterpolator (javax.validation.MessageInterpolator)1 GenericBootstrap (javax.validation.bootstrap.GenericBootstrap)1 ProviderSpecificBootstrap (javax.validation.bootstrap.ProviderSpecificBootstrap)1 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)1 Resource (org.springframework.core.io.Resource)1