Browse Source

Polishing (aligned with 6.1.x)

pull/33048/head
Juergen Hoeller 2 years ago
parent
commit
c8c95e360f
  1. 226
      spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java
  2. 25
      spring-core/src/main/java/org/springframework/util/ResourceUtils.java

226
spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

@ -71,18 +71,18 @@ import org.springframework.util.StringUtils;
/** /**
* A {@link ResourcePatternResolver} implementation that is able to resolve a * A {@link ResourcePatternResolver} implementation that is able to resolve a
* specified resource location path into one or more matching Resources. * specified resource location path into one or more matching Resources.
* The source path may be a simple path which has a one-to-one mapping to a
* target {@link org.springframework.core.io.Resource}, or alternatively
* may contain the special "{@code classpath*:}" prefix and/or
* internal Ant-style regular expressions (matched using Spring's
* {@link org.springframework.util.AntPathMatcher} utility).
* Both of the latter are effectively wildcards.
* *
* <p><b>No Wildcards:</b> * <p>The source path may be a simple path which has a one-to-one mapping to a
* target {@link org.springframework.core.io.Resource}, or alternatively may
* contain the special "{@code classpath*:}" prefix and/or internal Ant-style
* path patterns (matched using Spring's {@link AntPathMatcher} utility). Both
* of the latter are effectively wildcards.
*
* <h3>No Wildcards</h3>
* *
* <p>In the simple case, if the specified location path does not start with the * <p>In the simple case, if the specified location path does not start with the
* {@code "classpath*:}" prefix, and does not contain a PathMatcher pattern, * {@code "classpath*:}" prefix and does not contain a {@link PathMatcher}
* this resolver will simply return a single resource via a * pattern, this resolver will simply return a single resource via a
* {@code getResource()} call on the underlying {@code ResourceLoader}. * {@code getResource()} call on the underlying {@code ResourceLoader}.
* Examples are real URLs such as "{@code file:C:/context.xml}", pseudo-URLs * Examples are real URLs such as "{@code file:C:/context.xml}", pseudo-URLs
* such as "{@code classpath:/context.xml}", and simple unprefixed paths * such as "{@code classpath:/context.xml}", and simple unprefixed paths
@ -90,14 +90,14 @@ import org.springframework.util.StringUtils;
* fashion specific to the underlying {@code ResourceLoader} (e.g. * fashion specific to the underlying {@code ResourceLoader} (e.g.
* {@code ServletContextResource} for a {@code WebApplicationContext}). * {@code ServletContextResource} for a {@code WebApplicationContext}).
* *
* <p><b>Ant-style Patterns:</b> * <h3>Ant-style Patterns</h3>
* *
* <p>When the path location contains an Ant-style pattern, e.g.: * <p>When the path location contains an Ant-style pattern, for example:
* <pre class="code"> * <pre class="code">
* /WEB-INF/*-context.xml * /WEB-INF/*-context.xml
* com/mycompany/**&#47;applicationContext.xml * com/example/**&#47;applicationContext.xml
* file:C:/some/path/*-context.xml * file:C:/some/path/*-context.xml
* classpath:com/mycompany/**&#47;applicationContext.xml</pre> * classpath:com/example/**&#47;applicationContext.xml</pre>
* the resolver follows a more complex but defined procedure to try to resolve * the resolver follows a more complex but defined procedure to try to resolve
* the wildcard. It produces a {@code Resource} for the path up to the last * the wildcard. It produces a {@code Resource} for the path up to the last
* non-wildcard segment and obtains a {@code URL} from it. If this URL is not a * non-wildcard segment and obtains a {@code URL} from it. If this URL is not a
@ -108,31 +108,31 @@ import org.springframework.util.StringUtils;
* {@code java.net.JarURLConnection} from it, or manually parses the jar URL, and * {@code java.net.JarURLConnection} from it, or manually parses the jar URL, and
* then traverses the contents of the jar file, to resolve the wildcards. * then traverses the contents of the jar file, to resolve the wildcards.
* *
* <p><b>Implications on portability:</b> * <h3>Implications on Portability</h3>
* *
* <p>If the specified path is already a file URL (either explicitly, or * <p>If the specified path is already a file URL (either explicitly, or
* implicitly because the base {@code ResourceLoader} is a filesystem one), * implicitly because the base {@code ResourceLoader} is a filesystem one),
* then wildcarding is guaranteed to work in a completely portable fashion. * then wildcarding is guaranteed to work in a completely portable fashion.
* *
* <p>If the specified path is a classpath location, then the resolver must * <p>If the specified path is a class path location, then the resolver must
* obtain the last non-wildcard path segment URL via a * obtain the last non-wildcard path segment URL via a
* {@code Classloader.getResource()} call. Since this is just a * {@code Classloader.getResource()} call. Since this is just a
* node of the path (not the file at the end) it is actually undefined * node of the path (not the file at the end) it is actually undefined
* (in the ClassLoader Javadocs) exactly what sort of URL is returned in * (in the ClassLoader Javadocs) exactly what sort of URL is returned in
* this case. In practice, it is usually a {@code java.io.File} representing * this case. In practice, it is usually a {@code java.io.File} representing
* the directory, where the classpath resource resolves to a filesystem * the directory, where the class path resource resolves to a filesystem
* location, or a jar URL of some sort, where the classpath resource resolves * location, or a jar URL of some sort, where the class path resource resolves
* to a jar location. Still, there is a portability concern on this operation. * to a jar location. Still, there is a portability concern on this operation.
* *
* <p>If a jar URL is obtained for the last non-wildcard segment, the resolver * <p>If a jar URL is obtained for the last non-wildcard segment, the resolver
* must be able to get a {@code java.net.JarURLConnection} from it, or * must be able to get a {@code java.net.JarURLConnection} from it, or
* manually parse the jar URL, to be able to walk the contents of the jar, * manually parse the jar URL, to be able to walk the contents of the jar
* and resolve the wildcard. This will work in most environments, but will * and resolve the wildcard. This will work in most environments but will
* fail in others, and it is strongly recommended that the wildcard * fail in others, and it is strongly recommended that the wildcard
* resolution of resources coming from jars be thoroughly tested in your * resolution of resources coming from jars be thoroughly tested in your
* specific environment before you rely on it. * specific environment before you rely on it.
* *
* <p><b>{@code classpath*:} Prefix:</b> * <h3>{@code classpath*:} Prefix</h3>
* *
* <p>There is special support for retrieving multiple class path resources with * <p>There is special support for retrieving multiple class path resources with
* the same name, via the "{@code classpath*:}" prefix. For example, * the same name, via the "{@code classpath*:}" prefix. For example,
@ -142,22 +142,22 @@ import org.springframework.util.StringUtils;
* at the same location within each jar file. Internally, this happens via a * at the same location within each jar file. Internally, this happens via a
* {@code ClassLoader.getResources()} call, and is completely portable. * {@code ClassLoader.getResources()} call, and is completely portable.
* *
* <p>The "classpath*:" prefix can also be combined with a PathMatcher pattern in * <p>The "{@code classpath*:}" prefix can also be combined with a {@code PathMatcher}
* the rest of the location path, for example "classpath*:META-INF/*-beans.xml". * pattern in the rest of the location path &mdash; for example,
* In this case, the resolution strategy is fairly simple: a * "{@code classpath*:META-INF/*-beans.xml"}. In this case, the resolution strategy
* {@code ClassLoader.getResources()} call is used on the last non-wildcard * is fairly simple: a {@code ClassLoader.getResources()} call is used on the last
* path segment to get all the matching resources in the class loader hierarchy, * non-wildcard path segment to get all the matching resources in the class loader
* and then off each resource the same PathMatcher resolution strategy described * hierarchy, and then off each resource the same {@code PathMatcher} resolution
* above is used for the wildcard sub pattern. * strategy described above is used for the wildcard sub pattern.
* *
* <p><b>Other notes:</b> * <h3>Other Notes</h3>
* *
* <p>As of Spring Framework 6.0, if {@link #getResources(String)} is invoked * <p>As of Spring Framework 6.0, if {@link #getResources(String)} is invoked with
* with a location pattern using the "classpath*:" prefix it will first search * a location pattern using the "{@code classpath*:}" prefix it will first search
* all modules in the {@linkplain ModuleLayer#boot() boot layer}, excluding * all modules in the {@linkplain ModuleLayer#boot() boot layer}, excluding
* {@linkplain ModuleFinder#ofSystem() system modules}. It will then search the * {@linkplain ModuleFinder#ofSystem() system modules}. It will then search the
* classpath using {@link ClassLoader} APIs as described previously and return the * class path using {@link ClassLoader} APIs as described previously and return the
* combined results. Consequently, some of the limitations of classpath searches * combined results. Consequently, some of the limitations of class path searches
* may not apply when applications are deployed as modules. * may not apply when applications are deployed as modules.
* *
* <p><b>WARNING:</b> Note that "{@code classpath*:}" when combined with * <p><b>WARNING:</b> Note that "{@code classpath*:}" when combined with
@ -168,26 +168,26 @@ import org.springframework.util.StringUtils;
* root of expanded directories. This originates from a limitation in the JDK's * root of expanded directories. This originates from a limitation in the JDK's
* {@code ClassLoader.getResources()} method which only returns file system * {@code ClassLoader.getResources()} method which only returns file system
* locations for a passed-in empty String (indicating potential roots to search). * locations for a passed-in empty String (indicating potential roots to search).
* This {@code ResourcePatternResolver} implementation is trying to mitigate the * This {@code ResourcePatternResolver} implementation tries to mitigate the
* jar root lookup limitation through {@link URLClassLoader} introspection and * jar root lookup limitation through {@link URLClassLoader} introspection and
* "java.class.path" manifest evaluation; however, without portability guarantees. * "{@code java.class.path}" manifest evaluation; however, without portability
* guarantees.
* *
* <p><b>WARNING:</b> Ant-style patterns with "classpath:" resources are not * <p><b>WARNING:</b> Ant-style patterns with "{@code classpath:}" resources are not
* guaranteed to find matching resources if the root package to search is available * guaranteed to find matching resources if the base package to search is available
* in multiple class path locations. This is because a resource such as * in multiple class path locations. This is because a resource such as
* <pre class="code"> * <pre class="code">
* com/mycompany/package1/service-context.xml * com/example/package1/service-context.xml</pre>
* </pre> * may exist in only one class path location, but when a location pattern such as
* may be in only one location, but when a path such as
* <pre class="code"> * <pre class="code">
* classpath:com/mycompany/**&#47;service-context.xml * classpath:com/example/**&#47;service-context.xml</pre>
* </pre>
* is used to try to resolve it, the resolver will work off the (first) URL * is used to try to resolve it, the resolver will work off the (first) URL
* returned by {@code getResource("com/mycompany");}. If this base package node * returned by {@code getResource("com/example")}. If the {@code com/example} base
* exists in multiple classloader locations, the actual end resource may not be * package node exists in multiple class path locations, the actual desired resource
* underneath. Therefore, preferably, use "{@code classpath*:}" with the same * may not be present under the {@code com/example} base package in the first URL.
* Ant-style pattern in such a case, which will search <i>all</i> class path * Therefore, preferably, use "{@code classpath*:}" with the same Ant-style pattern
* locations that contain the root package. * in such a case, which will search <i>all</i> class path locations that contain
* the base package.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Colin Sampaleanu * @author Colin Sampaleanu
@ -249,19 +249,21 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
/** /**
* Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader. * Create a {@code PathMatchingResourcePatternResolver} with a
* {@link DefaultResourceLoader}.
* <p>ClassLoader access will happen via the thread context class loader. * <p>ClassLoader access will happen via the thread context class loader.
* @see org.springframework.core.io.DefaultResourceLoader * @see DefaultResourceLoader
*/ */
public PathMatchingResourcePatternResolver() { public PathMatchingResourcePatternResolver() {
this.resourceLoader = new DefaultResourceLoader(); this.resourceLoader = new DefaultResourceLoader();
} }
/** /**
* Create a new PathMatchingResourcePatternResolver. * Create a {@code PathMatchingResourcePatternResolver} with the supplied
* {@link ResourceLoader}.
* <p>ClassLoader access will happen via the thread context class loader. * <p>ClassLoader access will happen via the thread context class loader.
* @param resourceLoader the ResourceLoader to load root directories and * @param resourceLoader the {@code ResourceLoader} to load root directories
* actual resources with * and actual resources with
*/ */
public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) { public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null"); Assert.notNull(resourceLoader, "ResourceLoader must not be null");
@ -269,8 +271,9 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
/** /**
* Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader. * Create a {@code PathMatchingResourcePatternResolver} with a
* @param classLoader the ClassLoader to load classpath resources with, * {@link DefaultResourceLoader} and the supplied {@link ClassLoader}.
* @param classLoader the ClassLoader to load class path resources with,
* or {@code null} for using the thread context class loader * or {@code null} for using the thread context class loader
* at the time of actual resource access * at the time of actual resource access
* @see org.springframework.core.io.DefaultResourceLoader * @see org.springframework.core.io.DefaultResourceLoader
@ -281,7 +284,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
/** /**
* Return the ResourceLoader that this pattern resolver works with. * Return the {@link ResourceLoader} that this pattern resolver works with.
*/ */
public ResourceLoader getResourceLoader() { public ResourceLoader getResourceLoader() {
return this.resourceLoader; return this.resourceLoader;
@ -294,9 +297,10 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
/** /**
* Set the PathMatcher implementation to use for this * Set the {@link PathMatcher} implementation to use for this
* resource pattern resolver. Default is AntPathMatcher. * resource pattern resolver.
* @see org.springframework.util.AntPathMatcher * <p>Default is {@link AntPathMatcher}.
* @see AntPathMatcher
*/ */
public void setPathMatcher(PathMatcher pathMatcher) { public void setPathMatcher(PathMatcher pathMatcher) {
Assert.notNull(pathMatcher, "PathMatcher must not be null"); Assert.notNull(pathMatcher, "PathMatcher must not be null");
@ -304,7 +308,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
/** /**
* Return the PathMatcher that this resource pattern resolver uses. * Return the {@link PathMatcher} that this resource pattern resolver uses.
*/ */
public PathMatcher getPathMatcher() { public PathMatcher getPathMatcher() {
return this.pathMatcher; return this.pathMatcher;
@ -353,8 +357,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
/** /**
* Find all class location resources with the given location via the ClassLoader. * Find all class location resources with the given location via the ClassLoader.
* Delegates to {@link #doFindAllClassPathResources(String)}. * <p>Delegates to {@link #doFindAllClassPathResources(String)}.
* @param location the absolute path within the classpath * @param location the absolute path within the class path
* @return the result as Resource array * @return the result as Resource array
* @throws IOException in case of I/O errors * @throws IOException in case of I/O errors
* @see java.lang.ClassLoader#getResources * @see java.lang.ClassLoader#getResources
@ -364,15 +368,16 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
String path = stripLeadingSlash(location); String path = stripLeadingSlash(location);
Set<Resource> result = doFindAllClassPathResources(path); Set<Resource> result = doFindAllClassPathResources(path);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Resolved classpath location [" + path + "] to resources " + result); logger.trace("Resolved class path location [" + path + "] to resources " + result);
} }
return result.toArray(new Resource[0]); return result.toArray(new Resource[0]);
} }
/** /**
* Find all class location resources with the given path via the ClassLoader. * Find all class path resources with the given path via the configured
* Called by {@link #findAllClassPathResources(String)}. * {@link #getClassLoader() ClassLoader}.
* @param path the absolute path within the classpath (never a leading slash) * <p>Called by {@link #findAllClassPathResources(String)}.
* @param path the absolute path within the class path (never a leading slash)
* @return a mutable Set of matching Resource instances * @return a mutable Set of matching Resource instances
* @since 4.1.1 * @since 4.1.1
*/ */
@ -386,20 +391,21 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
if (!StringUtils.hasLength(path)) { if (!StringUtils.hasLength(path)) {
// The above result is likely to be incomplete, i.e. only containing file system references. // The above result is likely to be incomplete, i.e. only containing file system references.
// We need to have pointers to each of the jar files on the classpath as well... // We need to have pointers to each of the jar files on the class path as well...
addAllClassLoaderJarRoots(cl, result); addAllClassLoaderJarRoots(cl, result);
} }
return result; return result;
} }
/** /**
* Convert the given URL as returned from the ClassLoader into a {@link Resource}, * Convert the given URL as returned from the configured
* applying to path lookups without a pattern ({@link #findAllClassPathResources}). * {@link #getClassLoader() ClassLoader} into a {@link Resource}, applying
* to path lookups without a pattern (see {@link #findAllClassPathResources}).
* <p>As of 6.0.5, the default implementation creates a {@link FileSystemResource} * <p>As of 6.0.5, the default implementation creates a {@link FileSystemResource}
* in case of the "file" protocol or a {@link UrlResource} otherwise, matching * in case of the "file" protocol or a {@link UrlResource} otherwise, matching
* the outcome of pattern-based classpath traversal in the same resource layout, * the outcome of pattern-based class path traversal in the same resource layout,
* as well as matching the outcome of module path searches. * as well as matching the outcome of module path searches.
* @param url a URL as returned from the ClassLoader * @param url a URL as returned from the configured ClassLoader
* @return the corresponding Resource object * @return the corresponding Resource object
* @see java.lang.ClassLoader#getResources * @see java.lang.ClassLoader#getResources
* @see #doFindAllClassPathResources * @see #doFindAllClassPathResources
@ -422,8 +428,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
/** /**
* Search all {@link URLClassLoader} URLs for jar file references and add them to the * Search all {@link URLClassLoader} URLs for jar file references and add each to the
* given set of resources in the form of pointers to the root of the jar file content. * given set of resources in the form of a pointer to the root of the jar file content.
* @param classLoader the ClassLoader to search (including its ancestors) * @param classLoader the ClassLoader to search (including its ancestors)
* @param result the set of resources to add jar roots to * @param result the set of resources to add jar roots to
* @since 4.1.1 * @since 4.1.1
@ -457,7 +463,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
if (classLoader == ClassLoader.getSystemClassLoader()) { if (classLoader == ClassLoader.getSystemClassLoader()) {
// "java.class.path" manifest evaluation... // JAR "Class-Path" manifest header evaluation...
addClassPathManifestEntries(result); addClassPathManifestEntries(result);
} }
@ -476,16 +482,17 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
/** /**
* Determine jar file references from the "java.class.path." manifest property and add them * Determine jar file references from {@code Class-Path} manifest entries (which
* to the given set of resources in the form of pointers to the root of the jar file content. * are added to the {@code java.class.path} JVM system property by the system
* class loader) and add each to the given set of resources in the form of
* a pointer to the root of the jar file content.
* @param result the set of resources to add jar roots to * @param result the set of resources to add jar roots to
* @since 4.3 * @since 4.3
*/ */
protected void addClassPathManifestEntries(Set<Resource> result) { protected void addClassPathManifestEntries(Set<Resource> result) {
try { try {
String javaClassPathProperty = System.getProperty("java.class.path"); String javaClassPathProperty = System.getProperty("java.class.path");
for (String path : StringUtils.delimitedListToStringArray( for (String path : StringUtils.delimitedListToStringArray(javaClassPathProperty, File.pathSeparator)) {
javaClassPathProperty, System.getProperty("path.separator"))) {
try { try {
String filePath = new File(path).getAbsolutePath(); String filePath = new File(path).getAbsolutePath();
int prefixIndex = filePath.indexOf(':'); int prefixIndex = filePath.indexOf(':');
@ -499,7 +506,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
// Build URL that points to the root of the jar file // Build URL that points to the root of the jar file
UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX + UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX +
ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR); ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR);
// Potentially overlapping with URLClassLoader.getURLs() result above! // Potentially overlapping with URLClassLoader.getURLs() result in addAllClassLoaderJarRoots().
if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) { if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) {
result.add(jarResource); result.add(jarResource);
} }
@ -543,14 +550,18 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
/** /**
* Find all resources that match the given location pattern via the * Find all resources that match the given location pattern via the Ant-style
* Ant-style PathMatcher. Supports resources in OSGi bundles, JBoss VFS, * {@link #getPathMatcher() PathMatcher}.
* jar files, zip files, and file systems. * <p>Supports resources in OSGi bundles, JBoss VFS, jar files, zip files,
* and file systems.
* @param locationPattern the location pattern to match * @param locationPattern the location pattern to match
* @return the result as Resource array * @return the result as Resource array
* @throws IOException in case of I/O errors * @throws IOException in case of I/O errors
* @see #doFindPathMatchingJarResources * @see #determineRootDir(String)
* @see #doFindPathMatchingFileResources * @see #resolveRootDirResource(Resource)
* @see #isJarResource(Resource)
* @see #doFindPathMatchingJarResources(Resource, URL, String)
* @see #doFindPathMatchingFileResources(Resource, String)
* @see org.springframework.util.PathMatcher * @see org.springframework.util.PathMatcher
*/ */
protected Resource[] findPathMatchingResources(String locationPattern) throws IOException { protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
@ -607,29 +618,39 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
/** /**
* Resolve the specified resource for path matching. * Resolve the supplied root directory resource for path matching.
* <p>By default, Equinox OSGi "bundleresource:" / "bundleentry:" URL will be * <p>By default, {@link #findPathMatchingResources(String)} resolves Equinox
* resolved into a standard jar file URL that be traversed using Spring's * OSGi "bundleresource:" and "bundleentry:" URLs into standard jar file URLs
* standard jar file traversal algorithm. For any preceding custom resolution, * that will be traversed using Spring's standard jar file traversal algorithm.
* override this method and replace the resource handle accordingly. * <p>For any custom resolution, override this template method and replace the
* supplied resource handle accordingly.
* <p>The default implementation of this method returns the supplied resource
* unmodified.
* @param original the resource to resolve * @param original the resource to resolve
* @return the resolved resource (may be identical to the passed-in resource) * @return the resolved resource (may be identical to the supplied resource)
* @throws IOException in case of resolution failure * @throws IOException in case of resolution failure
* @see #findPathMatchingResources(String)
*/ */
protected Resource resolveRootDirResource(Resource original) throws IOException { protected Resource resolveRootDirResource(Resource original) throws IOException {
return original; return original;
} }
/** /**
* Return whether the given resource handle indicates a jar resource * Determine if the given resource handle indicates a jar resource that the
* that the {@link #doFindPathMatchingJarResources} method can handle. * {@link #doFindPathMatchingJarResources} method can handle.
* <p>By default, the URL protocols "jar", "zip", "vfszip, and "wsjar" * <p>{@link #findPathMatchingResources(String)} delegates to
* will be treated as jar resources. This template method allows for * {@link ResourceUtils#isJarURL(URL)} to determine whether the given URL
* detecting further kinds of jar-like resources, e.g. through * points to a resource in a jar file, and only invokes this method as a fallback.
* {@code instanceof} checks on the resource handle type. * <p>This template method therefore allows for detecting further kinds of
* @param resource the resource handle to check * jar-like resources &mdash; for example, via {@code instanceof} checks on
* (usually the root directory to start path matching from) * the resource handle type.
* @see #doFindPathMatchingJarResources * <p>The default implementation of this method returns {@code false}.
* @param resource the resource handle to check (usually the root directory
* to start path matching from)
* @return {@code true} if the given resource handle indicates a jar resource
* @throws IOException in case of I/O errors
* @see #findPathMatchingResources(String)
* @see #doFindPathMatchingJarResources(Resource, URL, String)
* @see org.springframework.util.ResourceUtils#isJarURL * @see org.springframework.util.ResourceUtils#isJarURL
*/ */
protected boolean isJarResource(Resource resource) throws IOException { protected boolean isJarResource(Resource resource) throws IOException {
@ -638,7 +659,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
/** /**
* Find all resources in jar files that match the given location pattern * Find all resources in jar files that match the given location pattern
* via the Ant-style PathMatcher. * via the Ant-style {@link #getPathMatcher() PathMatcher}.
* @param rootDirResource the root directory as Resource * @param rootDirResource the root directory as Resource
* @param rootDirUrl the pre-resolved root directory URL * @param rootDirUrl the pre-resolved root directory URL
* @param subPattern the sub pattern to match (below the root directory) * @param subPattern the sub pattern to match (below the root directory)
@ -691,7 +712,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
catch (ZipException ex) { catch (ZipException ex) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Skipping invalid jar classpath entry [" + urlFile + "]"); logger.debug("Skipping invalid jar class path entry [" + urlFile + "]");
} }
return Collections.emptySet(); return Collections.emptySet();
} }
@ -746,7 +767,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
/** /**
* Find all resources in the file system of the supplied root directory that * Find all resources in the file system of the supplied root directory that
* match the given location sub pattern via the Ant-style PathMatcher. * match the given location sub pattern via the Ant-style {@link #getPathMatcher()
* PathMatcher}.
* @param rootDirResource the root directory as a Resource * @param rootDirResource the root directory as a Resource
* @param subPattern the sub pattern to match (below the root directory) * @param subPattern the sub pattern to match (below the root directory)
* @return a mutable Set of matching Resource instances * @return a mutable Set of matching Resource instances
@ -924,8 +946,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
} }
/** /**
* If it's a "file:" URI, use FileSystemResource to avoid duplicates * If it's a "file:" URI, use {@link FileSystemResource} to avoid duplicates
* for the same path discovered via class-path scanning. * for the same path discovered via class path scanning.
*/ */
private Resource convertModuleSystemURI(URI uri) { private Resource convertModuleSystemURI(URI uri) {
return (ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme()) ? return (ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme()) ?

25
spring-core/src/main/java/org/springframework/util/ResourceUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -100,6 +100,7 @@ public abstract class ResourceUtils {
* @return whether the location qualifies as a URL * @return whether the location qualifies as a URL
* @see #CLASSPATH_URL_PREFIX * @see #CLASSPATH_URL_PREFIX
* @see java.net.URL * @see java.net.URL
* @see #toURL(String)
*/ */
public static boolean isUrl(@Nullable String resourceLocation) { public static boolean isUrl(@Nullable String resourceLocation) {
if (resourceLocation == null) { if (resourceLocation == null) {
@ -125,6 +126,7 @@ public abstract class ResourceUtils {
* "classpath:" pseudo URL, a "file:" URL, or a plain file path * "classpath:" pseudo URL, a "file:" URL, or a plain file path
* @return a corresponding URL object * @return a corresponding URL object
* @throws FileNotFoundException if the resource cannot be resolved to a URL * @throws FileNotFoundException if the resource cannot be resolved to a URL
* @see #toURL(String)
*/ */
public static URL getURL(String resourceLocation) throws FileNotFoundException { public static URL getURL(String resourceLocation) throws FileNotFoundException {
Assert.notNull(resourceLocation, "Resource location must not be null"); Assert.notNull(resourceLocation, "Resource location must not be null");
@ -165,6 +167,7 @@ public abstract class ResourceUtils {
* @return a corresponding File object * @return a corresponding File object
* @throws FileNotFoundException if the resource cannot be resolved to * @throws FileNotFoundException if the resource cannot be resolved to
* a file in the file system * a file in the file system
* @see #getFile(URL)
*/ */
public static File getFile(String resourceLocation) throws FileNotFoundException { public static File getFile(String resourceLocation) throws FileNotFoundException {
Assert.notNull(resourceLocation, "Resource location must not be null"); Assert.notNull(resourceLocation, "Resource location must not be null");
@ -196,6 +199,7 @@ public abstract class ResourceUtils {
* @return a corresponding File object * @return a corresponding File object
* @throws FileNotFoundException if the URL cannot be resolved to * @throws FileNotFoundException if the URL cannot be resolved to
* a file in the file system * a file in the file system
* @see #getFile(URL, String)
*/ */
public static File getFile(URL resourceUrl) throws FileNotFoundException { public static File getFile(URL resourceUrl) throws FileNotFoundException {
return getFile(resourceUrl, "URL"); return getFile(resourceUrl, "URL");
@ -236,6 +240,7 @@ public abstract class ResourceUtils {
* @throws FileNotFoundException if the URL cannot be resolved to * @throws FileNotFoundException if the URL cannot be resolved to
* a file in the file system * a file in the file system
* @since 2.5 * @since 2.5
* @see #getFile(URI, String)
*/ */
public static File getFile(URI resourceUri) throws FileNotFoundException { public static File getFile(URI resourceUri) throws FileNotFoundException {
return getFile(resourceUri, "URI"); return getFile(resourceUri, "URI");
@ -267,6 +272,7 @@ public abstract class ResourceUtils {
* i.e. has protocol "file", "vfsfile" or "vfs". * i.e. has protocol "file", "vfsfile" or "vfs".
* @param url the URL to check * @param url the URL to check
* @return whether the URL has been identified as a file system URL * @return whether the URL has been identified as a file system URL
* @see #isJarURL(URL)
*/ */
public static boolean isFileURL(URL url) { public static boolean isFileURL(URL url) {
String protocol = url.getProtocol(); String protocol = url.getProtocol();
@ -275,10 +281,12 @@ public abstract class ResourceUtils {
} }
/** /**
* Determine whether the given URL points to a resource in a jar file. * Determine whether the given URL points to a resource in a jar file
* i.e. has protocol "jar", "war, ""zip", "vfszip" or "wsjar". * &mdash; for example, whether the URL has protocol "jar", "war, "zip",
* "vfszip", or "wsjar".
* @param url the URL to check * @param url the URL to check
* @return whether the URL has been identified as a JAR URL * @return whether the URL has been identified as a JAR URL
* @see #isJarFileURL(URL)
*/ */
public static boolean isJarURL(URL url) { public static boolean isJarURL(URL url) {
String protocol = url.getProtocol(); String protocol = url.getProtocol();
@ -293,6 +301,7 @@ public abstract class ResourceUtils {
* @param url the URL to check * @param url the URL to check
* @return whether the URL has been identified as a JAR file URL * @return whether the URL has been identified as a JAR file URL
* @since 4.1 * @since 4.1
* @see #extractJarFileURL(URL)
*/ */
public static boolean isJarFileURL(URL url) { public static boolean isJarFileURL(URL url) {
return (URL_PROTOCOL_FILE.equals(url.getProtocol()) && return (URL_PROTOCOL_FILE.equals(url.getProtocol()) &&
@ -305,6 +314,7 @@ public abstract class ResourceUtils {
* @param jarUrl the original URL * @param jarUrl the original URL
* @return the URL for the actual jar file * @return the URL for the actual jar file
* @throws MalformedURLException if no valid jar file URL could be extracted * @throws MalformedURLException if no valid jar file URL could be extracted
* @see #extractArchiveURL(URL)
*/ */
public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException { public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException {
String urlFile = jarUrl.getFile(); String urlFile = jarUrl.getFile();
@ -366,6 +376,7 @@ public abstract class ResourceUtils {
* @return the URI instance * @return the URI instance
* @throws URISyntaxException if the URL wasn't a valid URI * @throws URISyntaxException if the URL wasn't a valid URI
* @see java.net.URL#toURI() * @see java.net.URL#toURI()
* @see #toURI(String)
*/ */
public static URI toURI(URL url) throws URISyntaxException { public static URI toURI(URL url) throws URISyntaxException {
return toURI(url.toString()); return toURI(url.toString());
@ -377,6 +388,7 @@ public abstract class ResourceUtils {
* @param location the location String to convert into a URI instance * @param location the location String to convert into a URI instance
* @return the URI instance * @return the URI instance
* @throws URISyntaxException if the location wasn't a valid URI * @throws URISyntaxException if the location wasn't a valid URI
* @see #toURI(URL)
*/ */
public static URI toURI(String location) throws URISyntaxException { public static URI toURI(String location) throws URISyntaxException {
return new URI(StringUtils.replace(location, " ", "%20")); return new URI(StringUtils.replace(location, " ", "%20"));
@ -389,6 +401,7 @@ public abstract class ResourceUtils {
* @return the URL instance * @return the URL instance
* @throws MalformedURLException if the location wasn't a valid URL * @throws MalformedURLException if the location wasn't a valid URL
* @since 6.0 * @since 6.0
* @see java.net.URL#URL(String)
*/ */
public static URL toURL(String location) throws MalformedURLException { public static URL toURL(String location) throws MalformedURLException {
// Equivalent without java.net.URL constructor - for building on JDK 20+ // Equivalent without java.net.URL constructor - for building on JDK 20+
@ -414,6 +427,7 @@ public abstract class ResourceUtils {
* @return the relative URL instance * @return the relative URL instance
* @throws MalformedURLException if the end result is not a valid URL * @throws MalformedURLException if the end result is not a valid URL
* @since 6.0 * @since 6.0
* @see java.net.URL#URL(URL, String)
*/ */
public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException { public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException {
// # can appear in filenames, java.net.URL should not treat it as a fragment // # can appear in filenames, java.net.URL should not treat it as a fragment
@ -429,9 +443,10 @@ public abstract class ResourceUtils {
/** /**
* Set the {@link URLConnection#setUseCaches "useCaches"} flag on the * Set the {@link URLConnection#setUseCaches "useCaches"} flag on the
* given connection, preferring {@code false} but leaving the * given connection, preferring {@code false} but leaving the flag at
* flag at {@code true} for JNLP based resources. * {@code true} for JNLP based resources.
* @param con the URLConnection to set the flag on * @param con the URLConnection to set the flag on
* @see URLConnection#setUseCaches
*/ */
public static void useCachesIfNecessary(URLConnection con) { public static void useCachesIfNecessary(URLConnection con) {
con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP")); con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));

Loading…
Cancel
Save