|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.context.config; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FilenameFilter; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Comparator; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.springframework.core.io.FileSystemResource; |
| 27 | +import org.springframework.core.io.Resource; |
| 28 | +import org.springframework.core.io.ResourceLoader; |
| 29 | +import org.springframework.core.io.support.ResourcePatternResolver; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.util.ResourceUtils; |
| 32 | +import org.springframework.util.StringUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * Strategy interface for loading resources from a location. Supports single resource and |
| 36 | + * simple wildcard directory patterns. |
| 37 | + * |
| 38 | + * @author Phillip Webb |
| 39 | + * @author Madhura Bhave |
| 40 | + */ |
| 41 | +class LocationResourceLoader { |
| 42 | + |
| 43 | + private static final Resource[] EMPTY_RESOURCES = {}; |
| 44 | + |
| 45 | + private static final Comparator<File> FILE_PATH_COMPARATOR = Comparator.comparing(File::getAbsolutePath); |
| 46 | + |
| 47 | + private static final Comparator<File> FILE_NAME_COMPARATOR = Comparator.comparing(File::getName); |
| 48 | + |
| 49 | + private final ResourceLoader resourceLoader; |
| 50 | + |
| 51 | + /** |
| 52 | + * Create a new {@link LocationResourceLoader} instance. |
| 53 | + * @param resourceLoader the underlying resource loader |
| 54 | + */ |
| 55 | + LocationResourceLoader(ResourceLoader resourceLoader) { |
| 56 | + this.resourceLoader = resourceLoader; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns if the location contains a pattern. |
| 61 | + * @param location the location to check |
| 62 | + * @return if the location is a pattern |
| 63 | + */ |
| 64 | + boolean isPattern(String location) { |
| 65 | + return StringUtils.hasLength(location) && location.contains("*"); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get a single resource from a non-pattern location. |
| 70 | + * @param location the location |
| 71 | + * @return the resource |
| 72 | + * @see #isPattern(String) |
| 73 | + */ |
| 74 | + Resource getResource(String location) { |
| 75 | + validateNonPattern(location); |
| 76 | + location = StringUtils.cleanPath(location); |
| 77 | + if (!ResourceUtils.isUrl(location)) { |
| 78 | + location = ResourceUtils.FILE_URL_PREFIX + location; |
| 79 | + } |
| 80 | + return this.resourceLoader.getResource(location); |
| 81 | + } |
| 82 | + |
| 83 | + private void validateNonPattern(String location) { |
| 84 | + Assert.state(!isPattern(location), () -> String.format("Location '%s' must not be a pattern", location)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get a multiple resources from a location pattern. |
| 89 | + * @param location the location pattern |
| 90 | + * @param type the type of resource to return |
| 91 | + * @return the resources |
| 92 | + * @see #isPattern(String) |
| 93 | + */ |
| 94 | + Resource[] getResources(String location, ResourceType type) { |
| 95 | + validatePattern(location, type); |
| 96 | + String directoryPath = location.substring(0, location.indexOf("*/")); |
| 97 | + String fileName = location.substring(location.lastIndexOf("/") + 1); |
| 98 | + Resource directoryResource = getResource(directoryPath); |
| 99 | + if (!directoryResource.exists()) { |
| 100 | + return new Resource[] { directoryResource }; |
| 101 | + } |
| 102 | + File directory = getDirectory(location, directoryResource); |
| 103 | + File[] subDirectories = directory.listFiles(this::isVisibleDirectory); |
| 104 | + if (subDirectories == null) { |
| 105 | + return EMPTY_RESOURCES; |
| 106 | + } |
| 107 | + Arrays.sort(subDirectories, FILE_PATH_COMPARATOR); |
| 108 | + if (type == ResourceType.DIRECTORY) { |
| 109 | + return Arrays.stream(subDirectories).map(FileSystemResource::new).toArray(Resource[]::new); |
| 110 | + } |
| 111 | + List<Resource> resources = new ArrayList<>(); |
| 112 | + FilenameFilter filter = (dir, name) -> name.equals(fileName); |
| 113 | + for (File subDirectory : subDirectories) { |
| 114 | + File[] files = subDirectory.listFiles(filter); |
| 115 | + if (files != null) { |
| 116 | + Arrays.sort(files, FILE_NAME_COMPARATOR); |
| 117 | + Arrays.stream(files).map(FileSystemResource::new).forEach(resources::add); |
| 118 | + } |
| 119 | + } |
| 120 | + return resources.toArray(EMPTY_RESOURCES); |
| 121 | + } |
| 122 | + |
| 123 | + private void validatePattern(String location, ResourceType type) { |
| 124 | + Assert.state(isPattern(location), () -> String.format("Location '%s' must be a pattern", location)); |
| 125 | + Assert.state(!location.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX), |
| 126 | + () -> String.format("Location '%s' cannot use classpath wildcards", location)); |
| 127 | + Assert.state(StringUtils.countOccurrencesOf(location, "*") == 1, |
| 128 | + () -> String.format("Location '%s' cannot contain multiple wildcards", location)); |
| 129 | + String directoryPath = (type != ResourceType.DIRECTORY) ? location.substring(0, location.lastIndexOf("/") + 1) |
| 130 | + : location; |
| 131 | + Assert.state(directoryPath.endsWith("*/"), () -> String.format("Location '%s' must end with '*/'", location)); |
| 132 | + } |
| 133 | + |
| 134 | + private File getDirectory(String patternLocation, Resource resource) { |
| 135 | + try { |
| 136 | + File directory = resource.getFile(); |
| 137 | + Assert.state(directory.isDirectory(), () -> "'" + directory + "' is not a directory"); |
| 138 | + return directory; |
| 139 | + } |
| 140 | + catch (Exception ex) { |
| 141 | + throw new IllegalStateException( |
| 142 | + "Unable to load config data resource from pattern '" + patternLocation + "'", ex); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private boolean isVisibleDirectory(File file) { |
| 147 | + return file.isDirectory() && !file.getName().startsWith("."); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Resource types that can be returned. |
| 152 | + */ |
| 153 | + enum ResourceType { |
| 154 | + |
| 155 | + /** |
| 156 | + * Return file resources. |
| 157 | + */ |
| 158 | + FILE, |
| 159 | + |
| 160 | + /** |
| 161 | + * Return directory resources. |
| 162 | + */ |
| 163 | + DIRECTORY |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments