Search in sources :

Example 1 with ProviderSpecificBootstrap

use of jakarta.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) {
        configureParameterNameProvider(this.parameterNameDiscoverer, configuration);
    }
    List<InputStream> mappingStreams = null;
    if (this.mappingLocations != null) {
        mappingStreams = new ArrayList<>(this.mappingLocations.length);
        for (Resource location : this.mappingLocations) {
            try {
                InputStream stream = location.getInputStream();
                mappingStreams.add(stream);
                configuration.addMapping(stream);
            } catch (IOException ex) {
                closeMappingStreams(mappingStreams);
                throw new IllegalStateException("Cannot read mapping resource: " + location);
            }
        }
    }
    this.validationPropertyMap.forEach(configuration::addProperty);
    // Allow for custom post-processing before we actually build the ValidatorFactory.
    postProcessConfiguration(configuration);
    try {
        this.validatorFactory = configuration.buildValidatorFactory();
        setTargetValidator(this.validatorFactory.getValidator());
    } finally {
        closeMappingStreams(mappingStreams);
    }
}
Also used : ConstraintValidatorFactory(jakarta.validation.ConstraintValidatorFactory) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) Method(java.lang.reflect.Method) IOException(java.io.IOException) ProviderSpecificBootstrap(jakarta.validation.bootstrap.ProviderSpecificBootstrap) GenericBootstrap(jakarta.validation.bootstrap.GenericBootstrap) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator)

Example 2 with ProviderSpecificBootstrap

use of jakarta.validation.bootstrap.ProviderSpecificBootstrap in project today-framework by TAKETODAY.

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 ContextConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
    }
    if (targetConstraintValidatorFactory != null) {
        configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
    }
    if (this.parameterNameDiscoverer != null) {
        configureParameterNameProvider(this.parameterNameDiscoverer, configuration);
    }
    List<InputStream> mappingStreams = null;
    if (this.mappingLocations != null) {
        mappingStreams = new ArrayList<>(this.mappingLocations.length);
        for (Resource location : this.mappingLocations) {
            try {
                InputStream stream = location.getInputStream();
                mappingStreams.add(stream);
                configuration.addMapping(stream);
            } catch (IOException ex) {
                closeMappingStreams(mappingStreams);
                throw new IllegalStateException("Cannot read mapping resource: " + location);
            }
        }
    }
    this.validationPropertyMap.forEach(configuration::addProperty);
    // Allow for custom post-processing before we actually build the ValidatorFactory.
    postProcessConfiguration(configuration);
    try {
        this.validatorFactory = configuration.buildValidatorFactory();
        setTargetValidator(this.validatorFactory.getValidator());
    } finally {
        closeMappingStreams(mappingStreams);
    }
}
Also used : ConstraintValidatorFactory(jakarta.validation.ConstraintValidatorFactory) InputStream(java.io.InputStream) Resource(cn.taketoday.core.io.Resource) Method(java.lang.reflect.Method) IOException(java.io.IOException) ProviderSpecificBootstrap(jakarta.validation.bootstrap.ProviderSpecificBootstrap) GenericBootstrap(jakarta.validation.bootstrap.GenericBootstrap) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator)

Example 3 with ProviderSpecificBootstrap

use of jakarta.validation.bootstrap.ProviderSpecificBootstrap in project today-infrastructure by TAKETODAY.

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 ContextConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
    }
    if (targetConstraintValidatorFactory != null) {
        configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
    }
    if (this.parameterNameDiscoverer != null) {
        configureParameterNameProvider(this.parameterNameDiscoverer, configuration);
    }
    List<InputStream> mappingStreams = null;
    if (this.mappingLocations != null) {
        mappingStreams = new ArrayList<>(this.mappingLocations.length);
        for (Resource location : this.mappingLocations) {
            try {
                InputStream stream = location.getInputStream();
                mappingStreams.add(stream);
                configuration.addMapping(stream);
            } catch (IOException ex) {
                closeMappingStreams(mappingStreams);
                throw new IllegalStateException("Cannot read mapping resource: " + location);
            }
        }
    }
    this.validationPropertyMap.forEach(configuration::addProperty);
    // Allow for custom post-processing before we actually build the ValidatorFactory.
    if (this.configurationInitializer != null) {
        this.configurationInitializer.accept(configuration);
    }
    postProcessConfiguration(configuration);
    try {
        this.validatorFactory = configuration.buildValidatorFactory();
        setTargetValidator(this.validatorFactory.getValidator());
    } finally {
        closeMappingStreams(mappingStreams);
    }
}
Also used : ConstraintValidatorFactory(jakarta.validation.ConstraintValidatorFactory) InputStream(java.io.InputStream) Resource(cn.taketoday.core.io.Resource) Method(java.lang.reflect.Method) IOException(java.io.IOException) ProviderSpecificBootstrap(jakarta.validation.bootstrap.ProviderSpecificBootstrap) GenericBootstrap(jakarta.validation.bootstrap.GenericBootstrap) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator)

Aggregations

ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)3 MessageInterpolator (jakarta.validation.MessageInterpolator)3 GenericBootstrap (jakarta.validation.bootstrap.GenericBootstrap)3 ProviderSpecificBootstrap (jakarta.validation.bootstrap.ProviderSpecificBootstrap)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Method (java.lang.reflect.Method)3 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)3 Resource (cn.taketoday.core.io.Resource)2 Resource (org.springframework.core.io.Resource)1