Package org.grails.io.support
Class ClassPathResource
- java.lang.Object
-
- org.grails.io.support.AbstractFileResolvingResource
-
- org.grails.io.support.ClassPathResource
-
- All Implemented Interfaces:
Resource
public class ClassPathResource extends AbstractFileResolvingResource
Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources.Supports resolution as
java.io.Fileif the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.- Since:
- 28.12.2003
- See Also:
ClassLoader.getResourceAsStream(String),Class.getResourceAsStream(String)
-
-
Constructor Summary
Constructors Modifier Constructor Description ClassPathResource(java.lang.String path)Create a new ClassPathResource for ClassLoader usage.ClassPathResource(java.lang.String path, java.lang.Class<?> clazz)Create a new ClassPathResource for Class usage.ClassPathResource(java.lang.String path, java.lang.ClassLoader classLoader)Create a new ClassPathResource for ClassLoader usage.protectedClassPathResource(java.lang.String path, java.lang.ClassLoader classLoader, java.lang.Class<?> clazz)Create a new ClassPathResource with optional ClassLoader and Class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ResourcecreateRelative(java.lang.String relativePath)This implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor.booleanequals(java.lang.Object obj)This implementation compares the underlying class path locations.booleanexists()This implementation checks for the resolution of a resource URL.java.lang.ClassLoadergetClassLoader()Return the ClassLoader that this resource will be obtained from.java.lang.StringgetDescription()This implementation returns a description that includes the class path location.java.lang.StringgetFilename()This implementation returns the name of the file that this class path resource refers to.java.io.InputStreamgetInputStream()This implementation opens an InputStream for the given class path resource.java.lang.StringgetPath()Return the path for this resource (as resource path within the class path).java.net.URIgetURI()Return a URI handle for this resource.java.net.URLgetURL()This implementation returns a URL for the underlying class path resource.inthashCode()This implementation returns the hash code of the underlying class path location.-
Methods inherited from class org.grails.io.support.AbstractFileResolvingResource
contentLength, getFile, getFile, getFileForLastModifiedCheck, isReadable, lastModified
-
-
-
-
Constructor Detail
-
ClassPathResource
public ClassPathResource(java.lang.String path)
Create a new ClassPathResource for ClassLoader usage. A leading slash will be removed, as the ClassLoader resource access methods will not accept it.The thread context class loader will be used for loading the resource.
- Parameters:
path- the absolute path within the class path- See Also:
ClassLoader.getResourceAsStream(String)
-
ClassPathResource
public ClassPathResource(java.lang.String path, java.lang.ClassLoader classLoader)Create a new ClassPathResource for ClassLoader usage. A leading slash will be removed, as the ClassLoader resource access methods will not accept it.- Parameters:
path- the absolute path within the classpathclassLoader- the class loader to load the resource with, ornullfor the thread context class loader- See Also:
ClassLoader.getResourceAsStream(String)
-
ClassPathResource
public ClassPathResource(java.lang.String path, java.lang.Class<?> clazz)Create a new ClassPathResource for Class usage. The path can be relative to the given class, or absolute within the classpath via a leading slash.- Parameters:
path- relative or absolute path within the class pathclazz- the class to load resources with- See Also:
Class.getResourceAsStream(java.lang.String)
-
ClassPathResource
protected ClassPathResource(java.lang.String path, java.lang.ClassLoader classLoader, java.lang.Class<?> clazz)Create a new ClassPathResource with optional ClassLoader and Class. Only for internal usage.- Parameters:
path- relative or absolute path within the classpathclassLoader- the class loader to load the resource with, if anyclazz- the class to load resources with, if any
-
-
Method Detail
-
getPath
public final java.lang.String getPath()
Return the path for this resource (as resource path within the class path).
-
getClassLoader
public final java.lang.ClassLoader getClassLoader()
Return the ClassLoader that this resource will be obtained from.
-
exists
public boolean exists()
This implementation checks for the resolution of a resource URL.- Specified by:
existsin interfaceResource- Overrides:
existsin classAbstractFileResolvingResource- See Also:
ClassLoader.getResource(String),Class.getResource(String)
-
getInputStream
public java.io.InputStream getInputStream() throws java.io.IOExceptionThis implementation opens an InputStream for the given class path resource.- Returns:
- The input stream
- Throws:
java.io.IOException- See Also:
ClassLoader.getResourceAsStream(String),Class.getResourceAsStream(String)
-
getURL
public java.net.URL getURL() throws java.io.IOExceptionThis implementation returns a URL for the underlying class path resource.- Throws:
java.io.IOException- if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor- See Also:
ClassLoader.getResource(String),Class.getResource(String)
-
getURI
public java.net.URI getURI() throws java.io.IOExceptionDescription copied from interface:ResourceReturn a URI handle for this resource.- Throws:
java.io.IOException- if the resource cannot be resolved as URI, i.e. if the resource is not available as descriptor
-
createRelative
public Resource createRelative(java.lang.String relativePath)
This implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor.- Parameters:
relativePath- The relative path- Returns:
- The new resource
-
getFilename
public java.lang.String getFilename()
This implementation returns the name of the file that this class path resource refers to.
-
getDescription
public java.lang.String getDescription()
This implementation returns a description that includes the class path location.- See Also:
Object.toString()
-
equals
public boolean equals(java.lang.Object obj)
This implementation compares the underlying class path locations.- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
This implementation returns the hash code of the underlying class path location.- Overrides:
hashCodein classjava.lang.Object
-
-