Class ResourceUtils
- java.lang.Object
-
- org.hotswap.agent.util.spring.util.ResourceUtils
-
public abstract class ResourceUtils extends Object
Utility methods for resolving resource locations to files in the file system. Mainly for internal use within the framework.Consider using Spring's Resource abstraction in the core package for handling all kinds of file resources in a uniform manner.
org.hotswap.agent.util.spring.io.loader.springframework.core.io.ResourceLoader'sgetResource()method can resolve any location to aorg.hotswap.agent.util.spring.io.resource.springframework.core.io.Resourceobject, which in turn allows one to obtain ajava.io.Filein the file system through itsgetFile()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'sResourceabstraction in the core package, on the other hand, already expects the logging system to be available.- Since:
- 1.1.5
- Author:
- Juergen Hoeller
-
-
Field Summary
Fields Modifier and Type Field Description static StringCLASSPATH_URL_PREFIXPseudo URL prefix for loading from the class path: "classpath:"static StringFILE_URL_PREFIXURL prefix for loading from the file system: "file:"static StringJAR_FILE_EXTENSIONFile extension for a regular jar file: ".jar"static StringJAR_URL_PREFIXURL prefix for loading from the file system: "jar:"static StringJAR_URL_SEPARATORSeparator between JAR URL and file path within the JAR: "!static StringURL_PROTOCOL_FILEURL protocol for a file in the file system: "file"static StringURL_PROTOCOL_JARURL protocol for an entry from a jar file: "jar"static StringURL_PROTOCOL_VFSURL protocol for a general JBoss VFS resource: "vfs"static StringURL_PROTOCOL_VFSFILEURL protocol for a JBoss file system resource: "vfsfile"static StringURL_PROTOCOL_VFSZIPURL protocol for an entry from a JBoss jar file: "vfszip"static StringURL_PROTOCOL_WSJARURL protocol for an entry from a WebSphere jar file: "wsjar"static StringURL_PROTOCOL_ZIPURL protocol for an entry from a zip file: "zip"
-
Constructor Summary
Constructors Constructor Description ResourceUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static URLextractJarFileURL(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 FilegetFile(String resourceLocation)Resolve the given resource location to ajava.io.File, i.e. to a file in the file system.static FilegetFile(URI resourceUri)Resolve the given resource URI to ajava.io.File, i.e. to a file in the file system.static FilegetFile(URI resourceUri, String description)Resolve the given resource URI to ajava.io.File, i.e. to a file in the file system.static FilegetFile(URL resourceUrl)Resolve the given resource URL to ajava.io.File, i.e. to a file in the file system.static FilegetFile(URL resourceUrl, String description)Resolve the given resource URL to ajava.io.File, i.e. to a file in the file system.static URLgetURL(String resourceLocation)Resolve the given resource location to ajava.net.URL.static booleanisFileURL(URL url)Determine whether the given URL points to a resource in the file system, that is, has protocol "file", "vfsfile" or "vfs".static booleanisJarFileURL(URL url)Determine whether the given URL points to a jar file itself, that is, has protocol "file" and ends with the ".jar" extension.static booleanisJarURL(URL url)Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "vfszip" or "wsjar".static booleanisUrl(String resourceLocation)Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.static URItoURI(String location)Create a URI instance for the given location String, replacing spaces with "%20" URI encoding first.static URItoURI(URL url)Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.static voiduseCachesIfNecessary(URLConnection con)Set the"useCaches"flag on the given connection, preferringfalsebut leaving the flag attruefor JNLP based resources.
-
-
-
Field Detail
-
CLASSPATH_URL_PREFIX
public static final String CLASSPATH_URL_PREFIX
Pseudo URL prefix for loading from the class path: "classpath:"- See Also:
- Constant Field Values
-
FILE_URL_PREFIX
public static final String FILE_URL_PREFIX
URL prefix for loading from the file system: "file:"- See Also:
- Constant Field Values
-
JAR_URL_PREFIX
public static final String JAR_URL_PREFIX
URL prefix for loading from the file system: "jar:"- See Also:
- Constant Field Values
-
URL_PROTOCOL_FILE
public static final String URL_PROTOCOL_FILE
URL protocol for a file in the file system: "file"- See Also:
- Constant Field Values
-
URL_PROTOCOL_JAR
public static final String URL_PROTOCOL_JAR
URL protocol for an entry from a jar file: "jar"- See Also:
- Constant Field Values
-
URL_PROTOCOL_ZIP
public static final String URL_PROTOCOL_ZIP
URL protocol for an entry from a zip file: "zip"- See Also:
- Constant Field Values
-
URL_PROTOCOL_WSJAR
public static final String URL_PROTOCOL_WSJAR
URL protocol for an entry from a WebSphere jar file: "wsjar"- See Also:
- Constant Field Values
-
URL_PROTOCOL_VFSZIP
public static final String URL_PROTOCOL_VFSZIP
URL protocol for an entry from a JBoss jar file: "vfszip"- See Also:
- Constant Field Values
-
URL_PROTOCOL_VFSFILE
public static final String URL_PROTOCOL_VFSFILE
URL protocol for a JBoss file system resource: "vfsfile"- See Also:
- Constant Field Values
-
URL_PROTOCOL_VFS
public static final String URL_PROTOCOL_VFS
URL protocol for a general JBoss VFS resource: "vfs"- See Also:
- Constant Field Values
-
JAR_FILE_EXTENSION
public static final String JAR_FILE_EXTENSION
File extension for a regular jar file: ".jar"- See Also:
- Constant Field Values
-
JAR_URL_SEPARATOR
public static final String JAR_URL_SEPARATOR
Separator between JAR URL and file path within the JAR: "!/"- See Also:
- Constant Field Values
-
-
Method Detail
-
isUrl
public static boolean isUrl(String resourceLocation)
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:
CLASSPATH_URL_PREFIX,URL
-
getURL
public static URL getURL(String resourceLocation) throws FileNotFoundException
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
public static File getFile(String resourceLocation) throws FileNotFoundException
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
public static File getFile(URL resourceUrl) throws FileNotFoundException
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
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException
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
public static File getFile(URI resourceUri) throws FileNotFoundException
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
public static File getFile(URI resourceUri, String description) throws FileNotFoundException
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
public static boolean isFileURL(URL url)
Determine whether the given URL points to a resource in the file system, that is, has protocol "file", "vfsfile" or "vfs".- Parameters:
url- the URL to check- Returns:
- whether the URL has been identified as a file system URL
-
isJarURL
public static boolean isJarURL(URL url)
Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "vfszip" or "wsjar".- Parameters:
url- the URL to check- Returns:
- whether the URL has been identified as a JAR URL
-
isJarFileURL
public static boolean isJarFileURL(URL url)
Determine whether the given URL points to a jar file itself, that is, has protocol "file" and ends with the ".jar" extension.- Parameters:
url- the URL to check- Returns:
- whether the URL has been identified as a JAR file URL
- Since:
- 4.1
-
extractJarFileURL
public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException
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
public static URI toURI(URL url) throws URISyntaxException
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:
URL.toURI()
-
toURI
public static URI toURI(String location) throws URISyntaxException
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
public static void useCachesIfNecessary(URLConnection con)
Set the"useCaches"flag on the given connection, preferringfalsebut leaving the flag attruefor JNLP based resources.- Parameters:
con- the URLConnection to set the flag on
-
-