Skip to content

Commit

Permalink
Close mapping streams after the ValidatorFactory has been built
Browse files Browse the repository at this point in the history
Closes gh-26418
  • Loading branch information
jhoeller committed Feb 14, 2021
1 parent 81be4c2 commit b4baa86
Showing 1 changed file with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,8 +17,10 @@
package org.springframework.validation.beanvalidation;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -291,12 +293,17 @@ public void afterPropertiesSet() {
configureParameterNameProvider(this.parameterNameDiscoverer, configuration);
}

List<InputStream> mappingStreams = null;
if (this.mappingLocations != null) {
mappingStreams = new ArrayList<>(this.mappingLocations.length);
for (Resource location : this.mappingLocations) {
try {
configuration.addMapping(location.getInputStream());
InputStream stream = location.getInputStream();
mappingStreams.add(stream);
configuration.addMapping(stream);
}
catch (IOException ex) {
closeMappingStreams(mappingStreams);
throw new IllegalStateException("Cannot read mapping resource: " + location);
}
}
Expand All @@ -307,8 +314,13 @@ public void afterPropertiesSet() {
// Allow for custom post-processing before we actually build the ValidatorFactory.
postProcessConfiguration(configuration);

this.validatorFactory = configuration.buildValidatorFactory();
setTargetValidator(this.validatorFactory.getValidator());
try {
this.validatorFactory = configuration.buildValidatorFactory();
setTargetValidator(this.validatorFactory.getValidator());
}
finally {
closeMappingStreams(mappingStreams);
}
}

private void configureParameterNameProvider(ParameterNameDiscoverer discoverer, Configuration<?> configuration) {
Expand All @@ -329,6 +341,18 @@ public List<String> getParameterNames(Method method) {
});
}

private void closeMappingStreams(@Nullable List<InputStream> mappingStreams){
if (!CollectionUtils.isEmpty(mappingStreams)) {
for (InputStream stream : mappingStreams) {
try {
stream.close();
}
catch (IOException ignored) {
}
}
}
}

/**
* Post-process the given Bean Validation configuration,
* adding to or overriding any of its settings.
Expand Down Expand Up @@ -397,15 +421,15 @@ public <T> T unwrap(@Nullable Class<T> type) {
return super.unwrap(type);
}
catch (ValidationException ex) {
// ignore - we'll try ValidatorFactory unwrapping next
// Ignore - we'll try ValidatorFactory unwrapping next
}
}
if (this.validatorFactory != null) {
try {
return this.validatorFactory.unwrap(type);
}
catch (ValidationException ex) {
// ignore if just being asked for ValidatorFactory
// Ignore if just being asked for ValidatorFactory
if (ValidatorFactory.class == type) {
return (T) this.validatorFactory;
}
Expand Down

0 comments on commit b4baa86

Please sign in to comment.