Skip to content

Commit

Permalink
Skip searching of nonexistent directory in PathMatchingResourcePatter…
Browse files Browse the repository at this point in the history
…nResolver

Prior to this commit, when PathMatchingResourcePatternResolver
processed a `file:` pattern (for example, `file:/app-config/**`) for a
`rootPath` that did not exist in the filesystem, the resolver attempted
to search the directory and logged a WARNING message similar to the
following when it failed to do so.

Failed to search in directory [/app-config/] for files matching pattern
[**]: java.nio.file.NoSuchFileException: /app-config/

To avoid unnecessary attempts to search a nonexistent directory,
PathMatchingResourcePatternResolver now skips searching of a nonexistent
directory and preemptively logs an INFO message similar to the
following.

Skipping search for files matching pattern [**]: directory [/app-config]
does not exist

Closes gh-31111
  • Loading branch information
sbrannen committed Aug 25, 2023
1 parent 1957033 commit 89b7a6b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Expand Up @@ -792,6 +792,14 @@ protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource
rootPath = Path.of(rootDirResource.getFile().getAbsolutePath());
}

if (!Files.exists(rootPath)) {
if (logger.isInfoEnabled()) {
logger.info("Skipping search for files matching pattern [%s]: directory [%s] does not exist"
.formatted(subPattern, rootPath.toAbsolutePath()));
}
return result;
}

String rootDir = StringUtils.cleanPath(rootPath.toString());
if (!rootDir.endsWith("/")) {
rootDir += "/";
Expand Down
Expand Up @@ -90,6 +90,19 @@ void classpathStarWithPatternOnFileSystem() {
assertFilenames(pattern, expectedFilenames);
}

@Test // gh-31111
void usingFileProtocolWithWildcardInPatternAndNonexistentRootPath() throws IOException {
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
String pattern = String.format("file:%s/example/bogus/**", testResourcesDir);
assertThat(resolver.getResources(pattern)).isEmpty();
// When the log level for the resolver is set to at least INFO, we should see
// a log entry similar to the following.
//
// [main] INFO o.s.c.i.s.PathMatchingResourcePatternResolver -
// Skipping search for files matching pattern [**]: directory
// [/<...>/spring-core/src/test/resources/example/bogus] does not exist
}

@Test
void encodedHashtagInPath() throws IOException {
Path rootDir = Paths.get("src/test/resources/custom%23root").toAbsolutePath();
Expand Down

0 comments on commit 89b7a6b

Please sign in to comment.