Skip to content

Commit b4baa86

Browse files
committed
Close mapping streams after the ValidatorFactory has been built
Closes gh-26418
1 parent 81be4c2 commit b4baa86

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,8 +17,10 @@
1717
package org.springframework.validation.beanvalidation;
1818

1919
import java.io.IOException;
20+
import java.io.InputStream;
2021
import java.lang.reflect.Constructor;
2122
import java.lang.reflect.Method;
23+
import java.util.ArrayList;
2224
import java.util.Arrays;
2325
import java.util.HashMap;
2426
import java.util.List;
@@ -291,12 +293,17 @@ public void afterPropertiesSet() {
291293
configureParameterNameProvider(this.parameterNameDiscoverer, configuration);
292294
}
293295

296+
List<InputStream> mappingStreams = null;
294297
if (this.mappingLocations != null) {
298+
mappingStreams = new ArrayList<>(this.mappingLocations.length);
295299
for (Resource location : this.mappingLocations) {
296300
try {
297-
configuration.addMapping(location.getInputStream());
301+
InputStream stream = location.getInputStream();
302+
mappingStreams.add(stream);
303+
configuration.addMapping(stream);
298304
}
299305
catch (IOException ex) {
306+
closeMappingStreams(mappingStreams);
300307
throw new IllegalStateException("Cannot read mapping resource: " + location);
301308
}
302309
}
@@ -307,8 +314,13 @@ public void afterPropertiesSet() {
307314
// Allow for custom post-processing before we actually build the ValidatorFactory.
308315
postProcessConfiguration(configuration);
309316

310-
this.validatorFactory = configuration.buildValidatorFactory();
311-
setTargetValidator(this.validatorFactory.getValidator());
317+
try {
318+
this.validatorFactory = configuration.buildValidatorFactory();
319+
setTargetValidator(this.validatorFactory.getValidator());
320+
}
321+
finally {
322+
closeMappingStreams(mappingStreams);
323+
}
312324
}
313325

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

344+
private void closeMappingStreams(@Nullable List<InputStream> mappingStreams){
345+
if (!CollectionUtils.isEmpty(mappingStreams)) {
346+
for (InputStream stream : mappingStreams) {
347+
try {
348+
stream.close();
349+
}
350+
catch (IOException ignored) {
351+
}
352+
}
353+
}
354+
}
355+
332356
/**
333357
* Post-process the given Bean Validation configuration,
334358
* adding to or overriding any of its settings.
@@ -397,15 +421,15 @@ public <T> T unwrap(@Nullable Class<T> type) {
397421
return super.unwrap(type);
398422
}
399423
catch (ValidationException ex) {
400-
// ignore - we'll try ValidatorFactory unwrapping next
424+
// Ignore - we'll try ValidatorFactory unwrapping next
401425
}
402426
}
403427
if (this.validatorFactory != null) {
404428
try {
405429
return this.validatorFactory.unwrap(type);
406430
}
407431
catch (ValidationException ex) {
408-
// ignore if just being asked for ValidatorFactory
432+
// Ignore if just being asked for ValidatorFactory
409433
if (ValidatorFactory.class == type) {
410434
return (T) this.validatorFactory;
411435
}

0 commit comments

Comments
 (0)