|
18 | 18 |
|
19 | 19 | import java.util.ArrayDeque; |
20 | 20 | import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
21 | 22 | import java.util.Collection; |
22 | 23 | import java.util.Collections; |
23 | 24 | import java.util.Deque; |
|
26 | 27 | import java.util.Set; |
27 | 28 | import java.util.regex.Matcher; |
28 | 29 | import java.util.regex.Pattern; |
| 30 | +import java.util.stream.Collectors; |
29 | 31 |
|
30 | 32 | import org.apache.commons.logging.Log; |
31 | 33 |
|
@@ -243,19 +245,33 @@ private Collection<StandardConfigDataResource> resolveEmptyDirectories( |
243 | 245 | Set<StandardConfigDataReference> references) { |
244 | 246 | Set<StandardConfigDataResource> empty = new LinkedHashSet<>(); |
245 | 247 | for (StandardConfigDataReference reference : references) { |
246 | | - if (reference.isMandatoryDirectory()) { |
247 | | - Resource resource = this.resourceLoader.getResource(reference.getDirectory()); |
248 | | - if (resource instanceof ClassPathResource) { |
249 | | - continue; |
250 | | - } |
251 | | - StandardConfigDataResource configDataResource = new StandardConfigDataResource(reference, resource); |
252 | | - ConfigDataResourceNotFoundException.throwIfDoesNotExist(configDataResource, resource); |
253 | | - empty.add(new StandardConfigDataResource(reference, resource, true)); |
254 | | - } |
| 248 | + empty.addAll(resolveEmptyDirectories(reference)); |
255 | 249 | } |
256 | 250 | return empty; |
257 | 251 | } |
258 | 252 |
|
| 253 | + private Set<StandardConfigDataResource> resolveEmptyDirectories(StandardConfigDataReference reference) { |
| 254 | + if (!this.resourceLoader.isPattern(reference.getResourceLocation())) { |
| 255 | + return resolveNonPatternEmptyDirectories(reference); |
| 256 | + } |
| 257 | + return resolvePatternEmptyDirectories(reference); |
| 258 | + } |
| 259 | + |
| 260 | + private Set<StandardConfigDataResource> resolveNonPatternEmptyDirectories(StandardConfigDataReference reference) { |
| 261 | + Resource resource = this.resourceLoader.getResource(reference.getDirectory()); |
| 262 | + return (resource instanceof ClassPathResource || !resource.exists()) ? Collections.emptySet() |
| 263 | + : Collections.singleton(new StandardConfigDataResource(reference, resource, true)); |
| 264 | + } |
| 265 | + |
| 266 | + private Set<StandardConfigDataResource> resolvePatternEmptyDirectories(StandardConfigDataReference reference) { |
| 267 | + Resource[] resources = this.resourceLoader.getResources(reference.getDirectory(), ResourceType.DIRECTORY); |
| 268 | + Assert.state(resources.length > 0, |
| 269 | + "No subdirectories found for mandatory directory location '" + reference.getDirectory() + "'."); |
| 270 | + return Arrays.stream(resources).filter(Resource::exists) |
| 271 | + .map((resource) -> new StandardConfigDataResource(reference, resource, true)) |
| 272 | + .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 273 | + } |
| 274 | + |
259 | 275 | private List<StandardConfigDataResource> resolve(StandardConfigDataReference reference) { |
260 | 276 | if (!this.resourceLoader.isPattern(reference.getResourceLocation())) { |
261 | 277 | return resolveNonPattern(reference); |
|
0 commit comments