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

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

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

Loading…
Cancel
Save