|
31 | 31 | import java.net.URL;
|
32 | 32 | import java.net.URLClassLoader;
|
33 | 33 | import java.net.URLConnection;
|
34 |
| -import java.nio.file.FileSystem; |
35 | 34 | import java.nio.file.FileSystemNotFoundException;
|
36 | 35 | import java.nio.file.FileSystems;
|
37 | 36 | import java.nio.file.Files;
|
@@ -748,76 +747,68 @@ protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource
|
748 | 747 | return Collections.emptySet();
|
749 | 748 | }
|
750 | 749 |
|
751 |
| - FileSystem fileSystem = null; |
752 |
| - try { |
753 |
| - Path rootPath = null; |
754 |
| - if (rootDirUri.isAbsolute() && !rootDirUri.isOpaque()) { |
755 |
| - // Prefer Path resolution from URI if possible |
| 750 | + Path rootPath = null; |
| 751 | + if (rootDirUri.isAbsolute() && !rootDirUri.isOpaque()) { |
| 752 | + // Prefer Path resolution from URI if possible |
| 753 | + try { |
756 | 754 | try {
|
757 |
| - try { |
758 |
| - rootPath = Path.of(rootDirUri); |
759 |
| - } |
760 |
| - catch (FileSystemNotFoundException ex) { |
761 |
| - // If the file system was not found, assume it's a custom file system that needs to be installed. |
762 |
| - fileSystem = FileSystems.newFileSystem(rootDirUri, Map.of(), ClassUtils.getDefaultClassLoader()); |
763 |
| - rootPath = Path.of(rootDirUri); |
764 |
| - } |
| 755 | + rootPath = Path.of(rootDirUri); |
765 | 756 | }
|
766 |
| - catch (Exception ex) { |
767 |
| - if (logger.isDebugEnabled()) { |
768 |
| - logger.debug("Failed to resolve %s in file system: %s".formatted(rootDirUri, ex)); |
769 |
| - } |
770 |
| - // Fallback via Resource.getFile() below |
| 757 | + catch (FileSystemNotFoundException ex) { |
| 758 | + // If the file system was not found, assume it's a custom file system that needs to be installed. |
| 759 | + FileSystems.newFileSystem(rootDirUri, Map.of(), ClassUtils.getDefaultClassLoader()); |
| 760 | + rootPath = Path.of(rootDirUri); |
771 | 761 | }
|
772 | 762 | }
|
773 |
| - if (rootPath == null) { |
774 |
| - // Resource.getFile() resolution as a fallback - |
775 |
| - // for custom URI formats and custom Resource implementations |
776 |
| - rootPath = Path.of(rootDirResource.getFile().getAbsolutePath()); |
| 763 | + catch (Exception ex) { |
| 764 | + if (logger.isDebugEnabled()) { |
| 765 | + logger.debug("Failed to resolve %s in file system: %s".formatted(rootDirUri, ex)); |
| 766 | + } |
| 767 | + // Fallback via Resource.getFile() below |
777 | 768 | }
|
| 769 | + } |
| 770 | + if (rootPath == null) { |
| 771 | + // Resource.getFile() resolution as a fallback - |
| 772 | + // for custom URI formats and custom Resource implementations |
| 773 | + rootPath = Path.of(rootDirResource.getFile().getAbsolutePath()); |
| 774 | + } |
778 | 775 |
|
779 |
| - String rootDir = StringUtils.cleanPath(rootPath.toString()); |
780 |
| - if (!rootDir.endsWith("/")) { |
781 |
| - rootDir += "/"; |
782 |
| - } |
| 776 | + String rootDir = StringUtils.cleanPath(rootPath.toString()); |
| 777 | + if (!rootDir.endsWith("/")) { |
| 778 | + rootDir += "/"; |
| 779 | + } |
783 | 780 |
|
784 |
| - Path rootPathForPattern = rootPath; |
785 |
| - String resourcePattern = rootDir + StringUtils.cleanPath(subPattern); |
786 |
| - Predicate<Path> isMatchingFile = path -> (!path.equals(rootPathForPattern) && |
787 |
| - getPathMatcher().match(resourcePattern, StringUtils.cleanPath(path.toString()))); |
| 781 | + Path rootPathForPattern = rootPath; |
| 782 | + String resourcePattern = rootDir + StringUtils.cleanPath(subPattern); |
| 783 | + Predicate<Path> isMatchingFile = path -> (!path.equals(rootPathForPattern) && |
| 784 | + getPathMatcher().match(resourcePattern, StringUtils.cleanPath(path.toString()))); |
788 | 785 |
|
789 |
| - if (logger.isTraceEnabled()) { |
790 |
| - logger.trace("Searching directory [%s] for files matching pattern [%s]" |
791 |
| - .formatted(rootPath.toAbsolutePath(), subPattern)); |
792 |
| - } |
| 786 | + if (logger.isTraceEnabled()) { |
| 787 | + logger.trace("Searching directory [%s] for files matching pattern [%s]" |
| 788 | + .formatted(rootPath.toAbsolutePath(), subPattern)); |
| 789 | + } |
793 | 790 |
|
794 |
| - Set<Resource> result = new LinkedHashSet<>(); |
795 |
| - try (Stream<Path> files = Files.walk(rootPath)) { |
796 |
| - files.filter(isMatchingFile).sorted().forEach(file -> { |
797 |
| - try { |
798 |
| - result.add(new FileSystemResource(file)); |
799 |
| - } |
800 |
| - catch (Exception ex) { |
801 |
| - if (logger.isDebugEnabled()) { |
802 |
| - logger.debug("Failed to convert file %s to an org.springframework.core.io.Resource: %s" |
803 |
| - .formatted(file, ex)); |
804 |
| - } |
| 791 | + Set<Resource> result = new LinkedHashSet<>(); |
| 792 | + try (Stream<Path> files = Files.walk(rootPath)) { |
| 793 | + files.filter(isMatchingFile).sorted().forEach(file -> { |
| 794 | + try { |
| 795 | + result.add(new FileSystemResource(file)); |
| 796 | + } |
| 797 | + catch (Exception ex) { |
| 798 | + if (logger.isDebugEnabled()) { |
| 799 | + logger.debug("Failed to convert file %s to an org.springframework.core.io.Resource: %s" |
| 800 | + .formatted(file, ex)); |
805 | 801 | }
|
806 |
| - }); |
807 |
| - } |
808 |
| - catch (Exception ex) { |
809 |
| - if (logger.isDebugEnabled()) { |
810 |
| - logger.debug("Failed to complete search in directory [%s] for files matching pattern [%s]: %s" |
811 |
| - .formatted(rootPath.toAbsolutePath(), subPattern, ex)); |
812 | 802 | }
|
813 |
| - } |
814 |
| - return result; |
| 803 | + }); |
815 | 804 | }
|
816 |
| - finally { |
817 |
| - if (fileSystem != null) { |
818 |
| - fileSystem.close(); |
| 805 | + catch (Exception ex) { |
| 806 | + if (logger.isDebugEnabled()) { |
| 807 | + logger.debug("Failed to complete search in directory [%s] for files matching pattern [%s]: %s" |
| 808 | + .formatted(rootPath.toAbsolutePath(), subPattern, ex)); |
819 | 809 | }
|
820 | 810 | }
|
| 811 | + return result; |
821 | 812 | }
|
822 | 813 |
|
823 | 814 | /**
|
|
0 commit comments