Package net.sf.jguiraffe.locators
Class ClassPathLocator
- java.lang.Object
-
- net.sf.jguiraffe.locators.AbstractLocator
-
- net.sf.jguiraffe.locators.ClassPathLocator
-
- All Implemented Interfaces:
Locator
public final class ClassPathLocator extends AbstractLocator
A specific
Locator
implementation that can obtain resources from the class path.This class is initialized with a resource name. The
getURL()
method tries to find this resource from the class path (using theLocatorUtils
class. No caching is performed; each invocation ofgetURL()
will look up the resource. Because a lookup might depend on the context class loader, multiple invocations ofgetURL()
may yield different results. In addition, it is possible to specify the class loader to be used for resource lookup.Instances of this class are created using the
getInstance()
factory method. They are immutable and thus can be shared between multiple threads.- Version:
- $Id: ClassPathLocator.java 211 2012-07-10 19:49:13Z oheger $
- Author:
- Oliver Heger
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object obj)
Compares this object with another one.ClassLoader
getClassLoader()
Returns the default class loader used by this locator when looking up the resource name.static ClassPathLocator
getInstance(String resourceName)
Returns aClassPathLocator
instance for the specified resource name.static ClassPathLocator
getInstance(String resourceName, ClassLoader cl)
Returns aClassPathLocator
instance which looks up the specified resource name using the given class loader.String
getResourceName()
Returns the name of the resource represented by this locator.URL
getURL()
Returns the URL for the represented resource.int
hashCode()
Returns a hash code for this object.String
toString()
Returns a string representation of this object.-
Methods inherited from class net.sf.jguiraffe.locators.AbstractLocator
getFile, getInputStream
-
-
-
-
Method Detail
-
getResourceName
public String getResourceName()
Returns the name of the resource represented by this locator.- Returns:
- the resource's name
-
getClassLoader
public ClassLoader getClassLoader()
Returns the default class loader used by this locator when looking up the resource name. This is the class loader passed to thegetInstance()
method. It may be null if no specific class loader was provided when constructing this object.- Returns:
- the default class loader for resource lookup
- Since:
- 1.2
-
getInstance
public static ClassPathLocator getInstance(String resourceName)
Returns aClassPathLocator
instance for the specified resource name.- Parameters:
resourceName
- the name of the resource (must not be null)- Returns:
- the instance with this resource name
- Throws:
IllegalArgumentException
- if the resource name is null
-
getInstance
public static ClassPathLocator getInstance(String resourceName, ClassLoader cl)
Returns aClassPathLocator
instance which looks up the specified resource name using the given class loader. If aClassLoader
reference is provided, this class loader is tried first when resolving the resource name. Otherwise, the default order of class loaders is used as implemented inLocatorUtils
.- Parameters:
resourceName
- the name of the resource (must not be null)cl
- an optional class loader to be used for resource lookup- Returns:
- the instance with this resource name
- Throws:
IllegalArgumentException
- if the resource name is null- Since:
- 1.2
-
getURL
public URL getURL()
Returns the URL for the represented resource. This implementation uses theLocatorUtils.locateResource(String, ClassLoader)
method to find the resource on the class path. If this fails, an exception is thrown.- Returns:
- the URL to the resource
- Throws:
LocatorException
- if the resource URL cannot be obtained
-
equals
public boolean equals(Object obj)
Compares this object with another one. Two instances of this class are equal if and only if they refer to the same resource name and use the same default class loader.
-
hashCode
public int hashCode()
Returns a hash code for this object.
-
-