Class ResourceUtils
Consider using Spring's Resource abstraction in the core package
for handling all kinds of file resources in a uniform manner.
org.springframework.core.io.ResourceLoader @code getResource()
method can resolve any location to a org.springframework.core.io.Resource
object, which in turn allows one to obtain a java.io.File
in the
file system through its getFile()
method.
The main reason for these utility methods for resource location handling
is to support Log4jConfigurer, which must be able to resolve
resource locations before the logging system has been initialized.
Spring's Resource
abstraction in the core package, on the other hand,
already expects the logging system to be available.
- Since:
- 1.1.5 Code copied from Spring source. Modifications made (mostly removal of methods) by Per Wendel.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Pseudo URL prefix for loading from the class path: "classpath:"static final String
URL prefix for loading from the file system: "file:"static final String
Separator between JAR URL and file path within the JARstatic final String
URL protocol for a file in the file system: "file"static final String
URL protocol for an entry from a jar file: "jar"static final String
URL protocol for an entry from a JBoss jar file: "vfszip"static final String
URL protocol for an entry from a WebSphere jar file: "wsjar"static final String
URL protocol for an entry from a zip file: "zip" -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic URL
extractJarFileURL
(URL jarUrl) Extract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself).static File
Resolve the given resource location to ajava.io.File
, i.e.static File
Resolve the given resource URI to ajava.io.File
, i.e.static File
Resolve the given resource URI to ajava.io.File
, i.e.static File
Resolve the given resource URL to ajava.io.File
, i.e.static File
Resolve the given resource URL to ajava.io.File
, i.e.static URL
Resolve the given resource location to ajava.net.URL
.static boolean
Determine whether the given URL points to a resource in the file system, that is, has protocol "file" or "vfs".static boolean
Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "wsjar" or "code-source".static boolean
Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.static URI
Create a URI instance for the given location String, replacing spaces with "%20" URI encoding first.static URI
Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.static void
Set the"useCaches"
flag on the given connection, preferringfalse
but leaving the flag attrue
for JNLP based resources.
-
Field Details
-
CLASSPATH_URL_PREFIX
Pseudo URL prefix for loading from the class path: "classpath:"- See Also:
-
FILE_URL_PREFIX
URL prefix for loading from the file system: "file:"- See Also:
-
URL_PROTOCOL_FILE
URL protocol for a file in the file system: "file"- See Also:
-
URL_PROTOCOL_JAR
URL protocol for an entry from a jar file: "jar"- See Also:
-
URL_PROTOCOL_ZIP
URL protocol for an entry from a zip file: "zip"- See Also:
-
URL_PROTOCOL_VFSZIP
URL protocol for an entry from a JBoss jar file: "vfszip"- See Also:
-
URL_PROTOCOL_WSJAR
URL protocol for an entry from a WebSphere jar file: "wsjar"- See Also:
-
JAR_URL_SEPARATOR
Separator between JAR URL and file path within the JAR- See Also:
-
-
Constructor Details
-
ResourceUtils
public ResourceUtils()
-
-
Method Details
-
isUrl
Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.- Parameters:
resourceLocation
- the location String to check- Returns:
- whether the location qualifies as a URL
- See Also:
-
getURL
Resolve the given resource location to ajava.net.URL
.Does not check whether the URL actually exists; simply returns the URL that the given location would correspond to.
- Parameters:
resourceLocation
- the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path- Returns:
- a corresponding URL object
- Throws:
FileNotFoundException
- if the resource cannot be resolved to a URL
-
getFile
Resolve the given resource location to ajava.io.File
, i.e. to a file in the file system.Does not check whether the file actually exists; simply returns the File that the given location would correspond to.
- Parameters:
resourceLocation
- the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path- Returns:
- a corresponding File object
- Throws:
FileNotFoundException
- if the resource cannot be resolved to a file in the file system
-
getFile
Resolve the given resource URL to ajava.io.File
, i.e. to a file in the file system.- Parameters:
resourceUrl
- the resource URL to resolve- Returns:
- a corresponding File object
- Throws:
FileNotFoundException
- if the URL cannot be resolved to a file in the file system
-
getFile
Resolve the given resource URL to ajava.io.File
, i.e. to a file in the file system.- Parameters:
resourceUrl
- the resource URL to resolvedescription
- a description of the original resource that the URL was created for (for example, a class path location)- Returns:
- a corresponding File object
- Throws:
FileNotFoundException
- if the URL cannot be resolved to a file in the file system
-
getFile
Resolve the given resource URI to ajava.io.File
, i.e. to a file in the file system.- Parameters:
resourceUri
- the resource URI to resolve- Returns:
- a corresponding File object
- Throws:
FileNotFoundException
- if the URL cannot be resolved to a file in the file system
-
getFile
Resolve the given resource URI to ajava.io.File
, i.e. to a file in the file system.- Parameters:
resourceUri
- the resource URI to resolvedescription
- a description of the original resource that the URI was created for (for example, a class path location)- Returns:
- a corresponding File object
- Throws:
FileNotFoundException
- if the URL cannot be resolved to a file in the file system
-
isFileURL
Determine whether the given URL points to a resource in the file system, that is, has protocol "file" or "vfs".- Parameters:
url
- the URL to check- Returns:
- whether the URL has been identified as a file system URL
-
isJarURL
Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "wsjar" or "code-source"."zip" and "wsjar" are used by WebLogic Server and WebSphere, respectively, but can be treated like jar files.
- Parameters:
url
- the URL to check- Returns:
- whether the URL has been identified as a JAR URL
-
extractJarFileURL
Extract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself).- Parameters:
jarUrl
- the original URL- Returns:
- the URL for the actual jar file
- Throws:
MalformedURLException
- if no valid jar file URL could be extracted
-
toURI
Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.Furthermore, this method works on JDK 1.4 as well, in contrast to the
URL.toURI()
method.- Parameters:
url
- the URL to convert into a URI instance- Returns:
- the URI instance
- Throws:
URISyntaxException
- if the URL wasn't a valid URI- See Also:
-
toURI
Create a URI instance for the given location String, replacing spaces with "%20" URI encoding first.- Parameters:
location
- the location String to convert into a URI instance- Returns:
- the URI instance
- Throws:
URISyntaxException
- if the location wasn't a valid URI
-
useCachesIfNecessary
Set the"useCaches"
flag on the given connection, preferringfalse
but leaving the flag attrue
for JNLP based resources.- Parameters:
con
- the URLConnection to set the flag on
-