Class 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 the LocatorUtils class. No caching is performed; each invocation of getURL() will look up the resource. Because a lookup might depend on the context class loader, multiple invocations of getURL() 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 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 the getInstance() 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 a ClassPathLocator 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 a ClassPathLocator instance which looks up the specified resource name using the given class loader. If a ClassLoader 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 in LocatorUtils.
        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 the LocatorUtils.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.
        Overrides:
        equals in class Object
        Parameters:
        obj - the object to be compared to
        Returns:
        a flag whether the objects are equal
      • hashCode

        public int hashCode()
        Returns a hash code for this object.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code
      • toString

        public String toString()
        Returns a string representation of this object. This string will at least contain the resource name used by this locator.
        Overrides:
        toString in class Object
        Returns:
        a string for this object