17
17
package org .springframework .validation .beanvalidation ;
18
18
19
19
import java .io .IOException ;
20
+ import java .io .InputStream ;
20
21
import java .lang .reflect .Constructor ;
21
22
import java .lang .reflect .Method ;
23
+ import java .util .ArrayList ;
22
24
import java .util .Arrays ;
23
25
import java .util .HashMap ;
24
26
import java .util .List ;
@@ -290,13 +292,17 @@ public void afterPropertiesSet() {
290
292
if (this .parameterNameDiscoverer != null ) {
291
293
configureParameterNameProvider (this .parameterNameDiscoverer , configuration );
292
294
}
293
-
295
+ List < InputStream > mappingStreams = null ;
294
296
if (this .mappingLocations != null ) {
297
+ mappingStreams = new ArrayList <>(mappingLocations .length );
295
298
for (Resource location : this .mappingLocations ) {
296
299
try {
297
- configuration .addMapping (location .getInputStream ());
300
+ InputStream stream = location .getInputStream ();
301
+ mappingStreams .add (stream );
302
+ configuration .addMapping (stream );
298
303
}
299
304
catch (IOException ex ) {
305
+ closeMappingStreams (mappingStreams );
300
306
throw new IllegalStateException ("Cannot read mapping resource: " + location );
301
307
}
302
308
}
@@ -307,8 +313,25 @@ public void afterPropertiesSet() {
307
313
// Allow for custom post-processing before we actually build the ValidatorFactory.
308
314
postProcessConfiguration (configuration );
309
315
310
- this .validatorFactory = configuration .buildValidatorFactory ();
311
- setTargetValidator (this .validatorFactory .getValidator ());
316
+ try {
317
+ this .validatorFactory = configuration .buildValidatorFactory ();
318
+ setTargetValidator (this .validatorFactory .getValidator ());
319
+ }
320
+ finally {
321
+ closeMappingStreams (mappingStreams );
322
+ }
323
+ }
324
+
325
+ private void closeMappingStreams (@ Nullable List <InputStream > mappingStreams ){
326
+ if (!CollectionUtils .isEmpty (mappingStreams )) {
327
+ for (InputStream stream : mappingStreams ) {
328
+ try {
329
+ stream .close ();
330
+ }
331
+ catch (IOException ignored ) {
332
+ }
333
+ }
334
+ }
312
335
}
313
336
314
337
private void configureParameterNameProvider (ParameterNameDiscoverer discoverer , Configuration <?> configuration ) {
0 commit comments