edu.emory.mathcs.util.classloader
Class ResourceLoader

java.lang.Object
  extended byedu.emory.mathcs.util.classloader.ResourceLoader

public class ResourceLoader
extends java.lang.Object

This class aids in accessing remote resources referred by URLs. The URLs are resolved into resource handles which can be used to access the resources directly and uniformly, regardless of the URL type. The resource loader is particularly useful when dealing with resources fetched from JAR files. It maintains the cache of opened JAR files (so that so that subsequent requests for resources coming from the same base Jar file can be handled efficiently). It fully supports JAR class-path (references from a JAR file to other JAR files) and JAR index (JAR containing information about content of other JARs). The caching policy of downloaded JAR files can be customized via the constructor parameter jarHandler; the default policy is to use separate cache per each ResourceLoader instance.

This class is particularly useful when implementing custom class loaders. It provides bottom-level resource fetching functionality. By using one of the loader methods which accepts an array of URLs, it is straightforward to implement class-path searching similar to that of URLClassLoader, with JAR dependencies (Class-Path) properly resolved and with JAR indexes properly handled.

This class provides two set of methods: get methods that return ResourceHandles (or their enumerations) and find methods that return URLs (or their enumerations). If the resource is not found, null (or empty enumeration) is returned. Resource handles represent a connection to the resource and they should be closed when done processing, just like input streams. In contrast, find methods return URLs that can be used to open multiple connections to the resource. In typical class loader applications, when a single retrieval is sufficient, it is preferable to use get methods since they pose slightly smaller communication overhead.

Version:
1.0
Author:
Dawid Kurzyniec

Constructor Summary
ResourceLoader()
          Constructs new ResourceLoadeer with default JAR caching policy, that is, to create and use separate cache for this ResourceLoader instance.
ResourceLoader(java.net.URLStreamHandler jarHandler)
          Constructs new ResourceLoader with specified JAR file handler which can implement custom JAR caching policy.
 
Method Summary
 java.net.URL findResource(java.net.URL[] sources, java.lang.String name)
          Finds resource with given name at the given search path.
 java.net.URL findResource(java.net.URL source, java.lang.String name)
          Fined resource with given name at the given source URL.
 java.util.Enumeration findResources(java.net.URL[] sources, java.lang.String name)
          Finds all resources with given name at the given search path.
 java.util.Enumeration findResources(java.net.URL source, java.lang.String name)
          Finds all resources with given name at the given source URL.
 ResourceHandle getResource(java.net.URL[] sources, java.lang.String name)
          Gets resource with given name at the given search path.
 ResourceHandle getResource(java.net.URL source, java.lang.String name)
          Gets resource with given name at the given source URL.
 java.util.Enumeration getResources(java.net.URL[] sources, java.lang.String name)
          Gets all resources with given name at the given search path.
 java.util.Enumeration getResources(java.net.URL source, java.lang.String name)
          Gets all resources with given name at the given source URL.
protected static boolean isDir(java.net.URL url)
          Test whether given URL points to a directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceLoader

public ResourceLoader()
Constructs new ResourceLoadeer with default JAR caching policy, that is, to create and use separate cache for this ResourceLoader instance.


ResourceLoader

public ResourceLoader(java.net.URLStreamHandler jarHandler)
Constructs new ResourceLoader with specified JAR file handler which can implement custom JAR caching policy.

Parameters:
jarHandler - JAR file handler
Method Detail

getResource

public ResourceHandle getResource(java.net.URL source,
                                  java.lang.String name)
Gets resource with given name at the given source URL. If the URL points to a directory, the name is the file path relative to this directory. If the URL points to a JAR file, the name identifies an entry in that JAR file. If the URL points to a JAR file, the resource is not found in that JAR file, and the JAR file has Class-Path attribute, the JAR files identified in the Class-Path are also searched for the resource.

Parameters:
source - the source URL
name - the resource name
Returns:
handle representing the resource, or null if not found

getResource

public ResourceHandle getResource(java.net.URL[] sources,
                                  java.lang.String name)
Gets resource with given name at the given search path. The path is searched iteratively, one URL at a time. If the URL points to a directory, the name is the file path relative to this directory. If the URL points to the JAR file, the name identifies an entry in that JAR file. If the URL points to the JAR file, the resource is not found in that JAR file, and the JAR file has Class-Path attribute, the JAR files identified in the Class-Path are also searched for the resource.

Parameters:
sources - the source URL path
name - the resource name
Returns:
handle representing the resource, or null if not found

getResources

public java.util.Enumeration getResources(java.net.URL source,
                                          java.lang.String name)
Gets all resources with given name at the given source URL. If the URL points to a directory, the name is the file path relative to this directory. If the URL points to a JAR file, the name identifies an entry in that JAR file. If the URL points to a JAR file, the resource is not found in that JAR file, and the JAR file has Class-Path attribute, the JAR files identified in the Class-Path are also searched for the resource.

The search is lazy, that is, "find next resource" operation is triggered by calling Enumeration.hasMoreElements().

Parameters:
source - the source URL
name - the resource name
Returns:
enumeration of resource handles representing the resources

getResources

public java.util.Enumeration getResources(java.net.URL[] sources,
                                          java.lang.String name)
Gets all resources with given name at the given search path. If the URL points to a directory, the name is the file path relative to this directory. If the URL points to a JAR file, the name identifies an entry in that JAR file. If the URL points to a JAR file, the resource is not found in that JAR file, and the JAR file has Class-Path attribute, the JAR files identified in the Class-Path are also searched for the resource.

The search is lazy, that is, "find next resource" operation is triggered by calling Enumeration.hasMoreElements().

Parameters:
sources - the source URL path
name - the resource name
Returns:
enumeration of resource handles representing the resources

findResource

public java.net.URL findResource(java.net.URL source,
                                 java.lang.String name)
Fined resource with given name at the given source URL. If the URL points to a directory, the name is the file path relative to this directory. If the URL points to a JAR file, the name identifies an entry in that JAR file. If the URL points to a JAR file, the resource is not found in that JAR file, and the JAR file has Class-Path attribute, the JAR files identified in the Class-Path are also searched for the resource.

Parameters:
source - the source URL
name - the resource name
Returns:
URL of the resource, or null if not found

findResource

public java.net.URL findResource(java.net.URL[] sources,
                                 java.lang.String name)
Finds resource with given name at the given search path. The path is searched iteratively, one URL at a time. If the URL points to a directory, the name is the file path relative to this directory. If the URL points to the JAR file, the name identifies an entry in that JAR file. If the URL points to the JAR file, the resource is not found in that JAR file, and the JAR file has Class-Path attribute, the JAR files identified in the Class-Path are also searched for the resource.

Parameters:
sources - the source URL path
name - the resource name
Returns:
URL of the resource, or null if not found

findResources

public java.util.Enumeration findResources(java.net.URL source,
                                           java.lang.String name)
Finds all resources with given name at the given source URL. If the URL points to a directory, the name is the file path relative to this directory. If the URL points to a JAR file, the name identifies an entry in that JAR file. If the URL points to a JAR file, the resource is not found in that JAR file, and the JAR file has Class-Path attribute, the JAR files identified in the Class-Path are also searched for the resource.

The search is lazy, that is, "find next resource" operation is triggered by calling Enumeration.hasMoreElements().

Parameters:
source - the source URL
name - the resource name
Returns:
enumeration of URLs of the resources

findResources

public java.util.Enumeration findResources(java.net.URL[] sources,
                                           java.lang.String name)
Finds all resources with given name at the given search path. If the URL points to a directory, the name is the file path relative to this directory. If the URL points to a JAR file, the name identifies an entry in that JAR file. If the URL points to a JAR file, the resource is not found in that JAR file, and the JAR file has Class-Path attribute, the JAR files identified in the Class-Path are also searched for the resource.

The search is lazy, that is, "find next resource" operation is triggered by calling Enumeration.hasMoreElements().

Parameters:
sources - the source URL path
name - the resource name
Returns:
enumeration of URLs of the resources

isDir

protected static boolean isDir(java.net.URL url)
Test whether given URL points to a directory. URL is deemed to point to a directory if has non-null "file" component ending with "/".

Parameters:
url - the URL to test
Returns:
true if the URL points to a directory, false otherwise