implementation for class path resources.
Uses either a given ClassLoader or a given Class for loading resources.
| Constructor: |
public ClassPathResource(String path) {
this(path, (ClassLoader) null);
}
Parameters:
path - the absolute path within the class path
Also see:
- java.lang.ClassLoader#getResourceAsStream(String)
- org.springframework.util.ClassUtils#getDefaultClassLoader()
|
public ClassPathResource(String path,
ClassLoader classLoader) {
Assert.notNull(path, "Path must not be null");
if (path.startsWith("/")) {
path = path.substring(1);
}
this.path = StringUtils.cleanPath(path);
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}
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 classpath
classLoader - the class loader to load the resource with,
or null for the thread context class loader
Also see:
- java.lang.ClassLoader#getResourceAsStream(String)
|
public ClassPathResource(String path,
Class clazz) {
Assert.notNull(path, "Path must not be null");
this.path = StringUtils.cleanPath(path);
this.clazz = 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 path
clazz - the class to load resources with
Also see:
- java.lang.Class#getResourceAsStream
|
protected ClassPathResource(String path,
ClassLoader classLoader,
Class clazz) {
this.path = path;
this.classLoader = classLoader;
this.clazz = clazz;
}
Create a new ClassPathResource with optional ClassLoader and Class.
Only for internal usage. Parameters:
path - relative or absolute path within the classpath
classLoader - the class loader to load the resource with, if any
clazz - the class to load resources with, if any
|
| Method from org.springframework.core.io.ClassPathResource Detail: |
public Resource createRelative(String relativePath) {
String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
return new ClassPathResource(pathToUse, this.classLoader, this.clazz);
}
This implementation creates a ClassPathResource, applying the given path
relative to the path of the underlying resource of this descriptor. |
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof ClassPathResource) {
ClassPathResource otherRes = (ClassPathResource) obj;
return (this.path.equals(otherRes.path) &&
ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) &&
ObjectUtils.nullSafeEquals(this.clazz, otherRes.clazz));
}
return false;
}
This implementation compares the underlying class path locations. |
public final ClassLoader getClassLoader() {
return (this.classLoader != null ? this.classLoader : this.clazz.getClassLoader());
}
Return the ClassLoader that this resource will be obtained from. |
public String getDescription() {
return "class path resource [" + this.path + "]";
}
This implementation returns a description that includes the class path location. |
public File getFile() throws IOException {
return ResourceUtils.getFile(getURL(), getDescription());
}
This implementation returns a File reference for the underlying class path
resource, provided that it refers to a file in the file system. |
protected File getFileForLastModifiedCheck() throws IOException {
URL url = getURL();
if (ResourceUtils.isJarURL(url)) {
URL actualUrl = ResourceUtils.extractJarFileURL(url);
return ResourceUtils.getFile(actualUrl);
}
else {
return ResourceUtils.getFile(url, getDescription());
}
}
This implementation determines the underlying File
(or jar file, in case of a resource in a jar/zip). |
public String getFilename() {
return StringUtils.getFilename(this.path);
}
This implementation returns the name of the file that this class path
resource refers to. |
public InputStream getInputStream() throws IOException {
InputStream is = null;
if (this.clazz != null) {
is = this.clazz.getResourceAsStream(this.path);
}
else {
is = this.classLoader.getResourceAsStream(this.path);
}
if (is == null) {
throw new FileNotFoundException(
getDescription() + " cannot be opened because it does not exist");
}
return is;
}
This implementation opens an InputStream for the given class path resource. |
public final String getPath() {
return this.path;
}
Return the path for this resource (as resource path within the class path). |
public URL getURL() throws IOException {
URL url = null;
if (this.clazz != null) {
url = this.clazz.getResource(this.path);
}
else {
url = this.classLoader.getResource(this.path);
}
if (url == null) {
throw new FileNotFoundException(
getDescription() + " cannot be resolved to URL because it does not exist");
}
return url;
}
This implementation returns a URL for the underlying class path resource. |
public int hashCode() {
return this.path.hashCode();
}
This implementation returns the hash code of the underlying
class path location. |