OS-independent alphabetical sorting of directory content

Issue: SPR-16838
This commit is contained in:
Juergen Hoeller 2018-06-15 22:09:29 +02:00
parent 51091f2242
commit bb6ab5dc91
1 changed files with 21 additions and 9 deletions

View File

@ -28,6 +28,7 @@ import java.net.URLClassLoader;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -783,15 +784,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
logger.debug("Searching directory [" + dir.getAbsolutePath() + logger.debug("Searching directory [" + dir.getAbsolutePath() +
"] for files matching pattern [" + fullPattern + "]"); "] for files matching pattern [" + fullPattern + "]");
} }
File[] dirContents = dir.listFiles(); for (File content : listDirectory(dir)) {
if (dirContents == null) {
if (logger.isWarnEnabled()) {
logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
}
return;
}
Arrays.sort(dirContents);
for (File content : dirContents) {
String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/"); String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) { if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
if (!content.canRead()) { if (!content.canRead()) {
@ -810,6 +803,25 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
} }
/**
* Determine a sorted list of files in the given directory.
* @param dir the directory to introspect
* @return the sorted list of files (by default in alphabetical order)
* @since 5.1
* @see File#listFiles()
*/
protected File[] listDirectory(File dir) {
File[] files = dir.listFiles();
if (files == null) {
if (logger.isWarnEnabled()) {
logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
}
return new File[0];
}
Arrays.sort(files, Comparator.comparing(File::getName));
return files;
}
/** /**
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime. * Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.