use of jakarta.validation.ConstraintValidatorFactory 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);
}
}
use of jakarta.validation.ConstraintValidatorFactory 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);
}
}
use of jakarta.validation.ConstraintValidatorFactory in project hibernate-validator by hibernate.
the class ConstraintValidatorInjectionUnitIT method testSuccessfulInjectionIntoConstraintValidator.
@Test
public void testSuccessfulInjectionIntoConstraintValidator() {
ConstraintValidatorFactory constraintValidatorFactory = validatorFactory.getConstraintValidatorFactory();
PingableValidator validator = constraintValidatorFactory.getInstance(PingableValidator.class);
assertThat(validator).as("Constraint Validator could not be created").isNotNull();
assertThat(validator.getPingService()).as("The ping service did not get injected").isNotNull();
}
use of jakarta.validation.ConstraintValidatorFactory in project hibernate-validator by hibernate.
the class ConstraintValidatorCachingTest method testConstraintValidatorInstancesAreCachedPerConstraintValidatorFactory.
@Test
@TestForIssue(jiraKey = "HV-564")
public void testConstraintValidatorInstancesAreCachedPerConstraintValidatorFactory() {
Configuration<?> config = getConfiguration();
OnceInstanceOnlyConstraintValidatorFactory constraintValidatorFactory1 = new OnceInstanceOnlyConstraintValidatorFactory();
OnceInstanceOnlyConstraintValidatorFactory constraintValidatorFactory2 = new OnceInstanceOnlyConstraintValidatorFactory();
config.constraintValidatorFactory(constraintValidatorFactory1);
ValidatorFactory factory = config.buildValidatorFactory();
Validator validator = factory.getValidator();
Person john = new Person("John Doe");
john.setAge(36);
john.addAddress(new Address("Mysterious Lane", "Mysterious"));
constraintValidatorFactory1.assertSize(0);
Set<ConstraintViolation<Person>> violations = validator.validate(john);
assertNoViolations(violations);
constraintValidatorFactory1.assertSize(3);
// getting a new validator with a new constraint factory
validator = factory.usingContext().constraintValidatorFactory(constraintValidatorFactory2).getValidator();
constraintValidatorFactory2.assertSize(0);
violations = validator.validate(john);
assertNoViolations(violations);
constraintValidatorFactory2.assertSize(3);
constraintValidatorFactory1.assertConstraintValidatorInstancesAreNotShared(constraintValidatorFactory2);
factory.close();
constraintValidatorFactory1.assertAllConstraintValidatorInstancesReleased();
constraintValidatorFactory2.assertAllConstraintValidatorInstancesReleased();
}
use of jakarta.validation.ConstraintValidatorFactory in project hibernate-validator by hibernate.
the class ConstraintValidatorCachingTest method testConstraintValidatorInstancesAreCached.
@Test
@TestForIssue(jiraKey = "HV-564")
public void testConstraintValidatorInstancesAreCached() {
Configuration<?> config = getConfiguration();
OnceInstanceOnlyConstraintValidatorFactory constraintValidatorFactory = new OnceInstanceOnlyConstraintValidatorFactory();
config.constraintValidatorFactory(constraintValidatorFactory);
ValidatorFactory factory = config.buildValidatorFactory();
Validator validator = factory.getValidator();
Person john = new Person("John Doe");
john.setAge(36);
john.addAddress(new Address("Mysterious Lane", "Mysterious"));
constraintValidatorFactory.assertSize(0);
Set<ConstraintViolation<Person>> violations = validator.validate(john);
assertNoViolations(violations);
constraintValidatorFactory.assertSize(3);
// need to call validate twice to let the cache kick in
violations = validator.validate(john);
assertNoViolations(violations);
constraintValidatorFactory.assertSize(3);
constraintValidatorFactory.assertKeyExists(SizeValidatorForCollection.class);
constraintValidatorFactory.assertKeyExists(MinValidatorForInteger.class);
constraintValidatorFactory.assertKeyExists(NotNullValidator.class);
// getting a new validator from the same factory should have the same instances cached
validator = factory.getValidator();
constraintValidatorFactory.assertSize(3);
violations = validator.validate(john);
assertNoViolations(violations);
constraintValidatorFactory.assertSize(3);
factory.close();
constraintValidatorFactory.assertAllConstraintValidatorInstancesReleased();
}
Aggregations