Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PathMatchingResourcePatternResolver should enforce consistent alphabetical sorting of directory content [SPR-14085] #18657

Closed
spring-projects-issues opened this issue Mar 24, 2016 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Mar 24, 2016

Say.li opened SPR-14085 and commented

java jdk (new File("").listFiles()) api:
listFiles public File[] listFiles() Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory. Pathnames denoting the directory itself and the directory's parent directory are not included in the result. Each resulting abstract pathname is constructed from this abstract pathname using the File(File, String) constructor. Therefore if this pathname is absolute then each resulting pathname is absolute; if this pathname is relative then each resulting pathname will be relative to the same directory.
There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Note that the Files class defines the newDirectoryStream method to open a directory and iterate over the names of the files in the directory. This may use less resources when working with very large directories.

Returns: An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs. Throws: SecurityException - If a security manager exists and its SecurityManager.checkRead(String) method denies read access to the directory Since:

Problem location: PathMatchingResourcePatternResolver(class) doRetrieveMatchingFiles(method)(line number:687)

(bug)
File[] dirContents = dir.listFiles();

Solution: add a alphabetical order.

The same code in different environmental order is not the same (can not start the project). I have encountered such a situation.

code:
public static void main(String[] args) {
File[] dirContents = new File("/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring").listFiles();
for (File f : dirContents) {
System.out.println(f.getPath());
}
}

Console:
windows:
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/applicationContext-user.xml
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/applicationContext.xml
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/Provider.xml
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/Consumer.xml
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/rabbit.xml

i linux:
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/Provider.xml
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/Consumer.xml
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/applicationContext-user.xml
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/applicationContext.xml
/home/say/git/chop-usercenter/chop-usercenter-provider/src/main/resources/spring/rabbit.xml


Issue Links:

Referenced from: commits 55f1c98

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

We're explicitly sorting the File array after each listFiles() call now. Since this may subtly influence existing applications, I'd rather only this for 4.3 where we have a release candidate phase to go through.

@spring-projects-issues
Copy link
Collaborator Author

Say.li commented

good,I want to modify the code request pull came over

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants