Package com.aspectran.utils
Class ResourceUtils
java.lang.Object
com.aspectran.utils.ResourceUtils
A class to simplify access to ResourceUtils through the classloader.
-
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
File extension for a regular jar file: ".jar".static final String
URL prefix for loading from a jar file: "jar:".static final String
Separator between JAR URL and file path within the JAR: "!/".static 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 a general JBoss VFS resource: "vfs".static final String
URL protocol for a JBoss file system resource: "vfsfile".static final String
URL protocol for an entry from a JBoss jar file: "vfszip".static final String
URL protocol for an entry from a war file: "war".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".static final String
URL prefix for loading from a war file on Tomcat: "war:".static final String
Special separator between WAR URL and jar part on Tomcat. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic URL
extractArchiveURL
(URL jarUrl) Extract the URL for the outermost archive from the given jar/war URL (which may point to a resource in a jar file or to a jar file itself).static 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
getFile
(String resourceLocation, ClassLoader classLoader) 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 Reader
Returns a Reader for reading the specified file.static Reader
Returns a Reader for reading the specified url.static URL
getResource
(String resource) Returns the URL of the resource on the classpath.static URL
getResource
(String resource, ClassLoader classLoader) Returns the URL of the resource on the classpath.static File
getResourceAsFile
(String resource) static File
getResourceAsFile
(String resource, ClassLoader classLoader) static Reader
getResourceAsReader
(String resource) Returns a resource on the classpath as a Stream object.static Reader
getResourceAsReader
(String resource, ClassLoader classLoader) Returns a resource on the classpath as a Stream object.static InputStream
getResourceAsStream
(String resource) Returns a resource on the classpath as a Stream object.static InputStream
getResourceAsStream
(String resource, ClassLoader classLoader) Returns a resource on the classpath as a Stream object.static URL
Resolve the given resource location to ajava.net.URL
.static URL
getURL
(String resourceLocation, ClassLoader classLoader) Resolve the given resource location to ajava.net.URL
.static boolean
Determine whether the given URL points to a resource in the file system, i.e.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.static boolean
Determine whether the given URL points to a resource in a jar file.static boolean
Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.static String
Returns a string from the specified file.static String
Returns a string from the specified Reader object.static String
Returns a string from the specified 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 URL
Create a URL instance for the given location String, going through URI construction and then URL conversion.
-
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:
-
JAR_URL_PREFIX
URL prefix for loading from a jar file: "jar:".- See Also:
-
WAR_URL_PREFIX
URL prefix for loading from a war file on Tomcat: "war:".- 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_WAR
URL protocol for an entry from a war file: "war".- See Also:
-
URL_PROTOCOL_ZIP
URL protocol for an entry from a zip file: "zip".- See Also:
-
URL_PROTOCOL_WSJAR
URL protocol for an entry from a WebSphere jar file: "wsjar".- See Also:
-
URL_PROTOCOL_VFSZIP
URL protocol for an entry from a JBoss jar file: "vfszip".- See Also:
-
URL_PROTOCOL_VFSFILE
URL protocol for a JBoss file system resource: "vfsfile".- See Also:
-
URL_PROTOCOL_VFS
URL protocol for a general JBoss VFS resource: "vfs".- See Also:
-
JAR_FILE_EXTENSION
File extension for a regular jar file: ".jar".- See Also:
-
JAR_URL_SEPARATOR
Separator between JAR URL and file path within the JAR: "!/".- See Also:
-
WAR_URL_SEPARATOR
Special separator between WAR URL and jar part on Tomcat.- 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
-
getURL
@NonNull public static URL getURL(String resourceLocation, ClassLoader classLoader) 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 pathclassLoader
- the class loader- 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
@NonNull public static File getFile(String resourceLocation, ClassLoader classLoader) 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 pathclassLoader
- the class loader- 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
@NonNull 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
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
@NonNull 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
Determine whether the given URL points to a resource in the file system, i.e. 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
Determine whether the given URL points to a resource in a jar file. i.e. has protocol "jar", "war, ""zip", "vfszip" or "wsjar".- Parameters:
url
- the URL to check- Returns:
- whether the URL has been identified as a JAR URL
-
isJarFileURL
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
-
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
-
extractArchiveURL
Extract the URL for the outermost archive from the given jar/war URL (which may point to a resource in a jar file or to a jar file itself).In the case of a jar file nested within a war file, this will return a URL to the war file since that is the one resolvable in the file system.
- Parameters:
jarUrl
- the original URL- Returns:
- the URL for the actual jar file
- Throws:
MalformedURLException
- if no valid jar file URL could be extracted- See Also:
-
toURI
Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.- 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
-
toURL
Create a URL instance for the given location String, going through URI construction and then URL conversion.- Parameters:
location
- the location String to convert into a URL instance- Returns:
- the URL instance
- Throws:
MalformedURLException
- if the location wasn't a valid URL- Since:
- 6.0
-
getResource
Returns the URL of the resource on the classpath.- Parameters:
resource
- the resource to find- Returns:
URL
object for reading the resource- Throws:
IOException
- if the resource cannot be found or read
-
getResource
Returns the URL of the resource on the classpath.- Parameters:
resource
- the resource to findclassLoader
- the class loader used to load the resource- Returns:
URL
object for reading the resource- Throws:
IOException
- if the resource cannot be found or read
-
getResourceAsFile
- Throws:
IOException
-
getResourceAsFile
@NonNull public static File getResourceAsFile(String resource, ClassLoader classLoader) throws IOException - Throws:
IOException
-
getResourceAsStream
Returns a resource on the classpath as a Stream object.- Parameters:
resource
- the resource to find- Returns:
- an input stream for reading the resource;
null
if the resource could not be found - Throws:
IOException
- if the resource cannot be found or read
-
getResourceAsStream
@NonNull public static InputStream getResourceAsStream(String resource, ClassLoader classLoader) throws IOException Returns a resource on the classpath as a Stream object.- Parameters:
resource
- the resource to findclassLoader
- the class loader used to load the resource- Returns:
- an input stream for reading the resource
- Throws:
IOException
- if the resource cannot be found or read
-
getResourceAsReader
Returns a resource on the classpath as a Stream object.- Parameters:
resource
- the resource to find- Returns:
- a stream reader for reading the resource
- Throws:
IOException
- if the resource cannot be found or read
-
getResourceAsReader
@NonNull public static Reader getResourceAsReader(String resource, ClassLoader classLoader) throws IOException Returns a resource on the classpath as a Stream object.- Parameters:
resource
- the resource to findclassLoader
- the class loader used to load the resource- Returns:
- a stream reader for reading the resource
- Throws:
IOException
- if the resource cannot be found or read
-
getReader
Returns a Reader for reading the specified file.- Parameters:
file
- the resource file to resolveencoding
- the encoding- Returns:
- the reader instance
- Throws:
IOException
- if an error occurred when reading resources using any I/O operations
-
getReader
Returns a Reader for reading the specified url.- Parameters:
url
- the resource URL to resolveencoding
- the encoding- Returns:
- the reader instance
- Throws:
IOException
- if an error occurred when reading resources using any I/O operations
-
read
Returns a string from the specified file.- Parameters:
file
- the fileencoding
- the encoding- Returns:
- the reader instance
- Throws:
IOException
- if an error occurred when reading resources using any I/O operations
-
read
Returns a string from the specified url.- Parameters:
url
- the urlencoding
- the encoding- Returns:
- the string
- Throws:
IOException
- if an error occurred when reading resources using any I/O operations
-
read
Returns a string from the specified Reader object.- Parameters:
reader
- the reader- Returns:
- the string
- Throws:
IOException
- if an error occurred when reading resources using any I/O operations
-