org.springframework.core.io
Class ClassPathResource

java.lang.Object
  extended by org.springframework.core.io.AbstractResource
      extended by org.springframework.core.io.ClassPathResource
All Implemented Interfaces:
InputStreamSource, Resource

public class ClassPathResource
extends AbstractResource

Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources.

Supports resolution as java.io.File if 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
Author:
Juergen Hoeller
See Also:
ClassLoader.getResourceAsStream(String), Class.getResourceAsStream(String)

Constructor Summary
  ClassPathResource(String path)
          Create a new ClassPathResource for ClassLoader usage.
  ClassPathResource(String path, Class clazz)
          Create a new ClassPathResource for Class usage.
  ClassPathResource(String path, ClassLoader classLoader)
          Create a new ClassPathResource for ClassLoader usage.
protected ClassPathResource(String path, ClassLoader classLoader, Class clazz)
          Create a new ClassPathResource with optional ClassLoader and Class.
 
Method Summary
 Resource createRelative(String relativePath)
          This implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor.
 boolean equals(Object obj)
          This implementation compares the underlying class path locations.
 ClassLoader getClassLoader()
          Return the ClassLoader that this resource will be obtained from.
 String getDescription()
          This implementation returns a description that includes the class path location.
 File getFile()
          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()
          This implementation determines the underlying File (or jar file, in case of a resource in a jar/zip).
 String getFilename()
          This implementation returns the name of the file that this class path resource refers to.
 InputStream getInputStream()
          This implementation opens an InputStream for the given class path resource.
 String getPath()
          Return the path for this resource (as resource path within the class path).
 URL getURL()
          This implementation returns a URL for the underlying class path resource.
 int hashCode()
          This implementation returns the hash code of the underlying class path location.
 
Methods inherited from class org.springframework.core.io.AbstractResource
exists, getURI, isOpen, isReadable, lastModified, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClassPathResource

public ClassPathResource(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), ClassUtils.getDefaultClassLoader()

ClassPathResource

public ClassPathResource(String path,
                         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 classpath
classLoader - the class loader to load the resource with, or null for the thread context class loader
See Also:
ClassLoader.getResourceAsStream(String)

ClassPathResource

public ClassPathResource(String path,
                         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 path
clazz - the class to load resources with
See Also:
Class.getResourceAsStream(java.lang.String)

ClassPathResource

protected ClassPathResource(String path,
                            ClassLoader classLoader,
                            Class 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 Detail

getPath

public final String getPath()
Return the path for this resource (as resource path within the class path).


getClassLoader

public final ClassLoader getClassLoader()
Return the ClassLoader that this resource will be obtained from.


getInputStream

public InputStream getInputStream()
                           throws IOException
This implementation opens an InputStream for the given class path resource.

Throws:
IOException - if the stream could not be opened
See Also:
ClassLoader.getResourceAsStream(String), Class.getResourceAsStream(String)

getURL

public URL getURL()
           throws IOException
This implementation returns a URL for the underlying class path resource.

Specified by:
getURL in interface Resource
Overrides:
getURL in class AbstractResource
Throws:
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)

getFile

public File getFile()
             throws IOException
This implementation returns a File reference for the underlying class path resource, provided that it refers to a file in the file system.

Specified by:
getFile in interface Resource
Overrides:
getFile in class AbstractResource
Throws:
IOException - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
See Also:
ResourceUtils.getFile(java.net.URL, String)

getFileForLastModifiedCheck

protected File getFileForLastModifiedCheck()
                                    throws IOException
This implementation determines the underlying File (or jar file, in case of a resource in a jar/zip).

Overrides:
getFileForLastModifiedCheck in class AbstractResource
Returns:
the File to use for timestamp checking (never null)
Throws:
IOException - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system

createRelative

public Resource createRelative(String relativePath)
This implementation creates a ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor.

Specified by:
createRelative in interface Resource
Overrides:
createRelative in class AbstractResource
Parameters:
relativePath - the relative path (relative to this resource)
Returns:
the resource handle for the relative resource
See Also:
StringUtils.applyRelativePath(String, String)

getFilename

public String getFilename()
This implementation returns the name of the file that this class path resource refers to.

Specified by:
getFilename in interface Resource
Overrides:
getFilename in class AbstractResource
See Also:
StringUtils.getFilename(String)

getDescription

public String getDescription()
This implementation returns a description that includes the class path location.

See Also:
Object.toString()

equals

public boolean equals(Object obj)
This implementation compares the underlying class path locations.

Overrides:
equals in class AbstractResource
See Also:
Resource.getDescription()

hashCode

public int hashCode()
This implementation returns the hash code of the underlying class path location.

Overrides:
hashCode in class AbstractResource
See Also:
Resource.getDescription()


Copyright © 2002-2008 The Spring Framework.