From c5e49fc6dc7a6f065a62eadff3e2c2163f45a850 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 7 Sep 2009 12:44:59 +0000 Subject: [PATCH] refined PathMatchingResourcePatternResolver's treatment of non-readable directories (SPR-6086) --- .../PathMatchingResourcePatternResolver.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/org.springframework.core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 24eaa7f469..9c8b001e96 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -521,8 +521,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol rootDir = rootDirResource.getFile().getAbsoluteFile(); } catch (IOException ex) { - if (logger.isDebugEnabled()) { - logger.debug("Cannot search for matching files underneath " + rootDirResource + + if (logger.isWarnEnabled()) { + logger.warn("Cannot search for matching files underneath " + rootDirResource + " because it does not correspond to a directory in the file system", ex); } return Collections.emptySet(); @@ -563,7 +563,15 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol */ protected Set retrieveMatchingFiles(File rootDir, String pattern) throws IOException { if (!rootDir.isDirectory()) { - throw new IllegalArgumentException("Resource path [" + rootDir + "] does not denote a directory"); + throw new IllegalStateException( + "Resource path [" + rootDir.getAbsolutePath() + "] does not denote a directory"); + } + if (!rootDir.canRead()) { + if (logger.isWarnEnabled()) { + logger.warn("Cannot search for matching files underneath directory [" + rootDir.getAbsolutePath() + + "] because the application is not allowed to read the directory"); + } + return Collections.emptySet(); } String fullPattern = StringUtils.replace(rootDir.getAbsolutePath(), File.separator, "/"); if (!pattern.startsWith("/")) { @@ -591,12 +599,23 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol } File[] dirContents = dir.listFiles(); if (dirContents == null) { - throw new IOException("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]"); + if (logger.isWarnEnabled()) { + logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]"); + } + return; } for (File content : dirContents) { String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/"); if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) { - doRetrieveMatchingFiles(fullPattern, content, result); + if (!content.canRead()) { + if (logger.isDebugEnabled()) { + logger.debug("Skipping subdirectory [" + dir.getAbsolutePath() + + "] because the application is not allowed to read the directory"); + } + } + else { + doRetrieveMatchingFiles(fullPattern, content, result); + } } if (getPathMatcher().match(fullPattern, currPath)) { result.add(content);