1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
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 ;
@@ -291,12 +293,17 @@ public void afterPropertiesSet() {
291
293
configureParameterNameProvider (this .parameterNameDiscoverer , configuration );
292
294
}
293
295
296
+ List <InputStream > mappingStreams = null ;
294
297
if (this .mappingLocations != null ) {
298
+ mappingStreams = new ArrayList <>(this .mappingLocations .length );
295
299
for (Resource location : this .mappingLocations ) {
296
300
try {
297
- configuration .addMapping (location .getInputStream ());
301
+ InputStream stream = location .getInputStream ();
302
+ mappingStreams .add (stream );
303
+ configuration .addMapping (stream );
298
304
}
299
305
catch (IOException ex ) {
306
+ closeMappingStreams (mappingStreams );
300
307
throw new IllegalStateException ("Cannot read mapping resource: " + location );
301
308
}
302
309
}
@@ -307,8 +314,13 @@ public void afterPropertiesSet() {
307
314
// Allow for custom post-processing before we actually build the ValidatorFactory.
308
315
postProcessConfiguration (configuration );
309
316
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
+ }
312
324
}
313
325
314
326
private void configureParameterNameProvider (ParameterNameDiscoverer discoverer , Configuration <?> configuration ) {
@@ -329,6 +341,18 @@ public List<String> getParameterNames(Method method) {
329
341
});
330
342
}
331
343
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
+
332
356
/**
333
357
* Post-process the given Bean Validation configuration,
334
358
* adding to or overriding any of its settings.
@@ -397,15 +421,15 @@ public <T> T unwrap(@Nullable Class<T> type) {
397
421
return super .unwrap (type );
398
422
}
399
423
catch (ValidationException ex ) {
400
- // ignore - we'll try ValidatorFactory unwrapping next
424
+ // Ignore - we'll try ValidatorFactory unwrapping next
401
425
}
402
426
}
403
427
if (this .validatorFactory != null ) {
404
428
try {
405
429
return this .validatorFactory .unwrap (type );
406
430
}
407
431
catch (ValidationException ex ) {
408
- // ignore if just being asked for ValidatorFactory
432
+ // Ignore if just being asked for ValidatorFactory
409
433
if (ValidatorFactory .class == type ) {
410
434
return (T ) this .validatorFactory ;
411
435
}
0 commit comments