Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

edu.emory.mathcs.util.classloader
Class ResourceLoader  view ResourceLoader download ResourceLoader.java

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 java.net.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

Nested Class Summary
private static class ResourceLoader.JarInfo
           
private static class ResourceLoader.JarResourceHandle
           
private  class ResourceLoader.ResourceEnumeration
           
 
Field Summary
private static java.lang.String JAR_INDEX_ENTRY_NAME
           
(package private)  java.net.URLStreamHandler jarHandler
           
(package private)  java.util.Map url2jarInfo
           
 
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 ResourceLoadeer 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.
private  java.net.URL findResource(java.net.URL source, java.lang.String name, java.util.Set visitedJars, java.util.Set skip)
           
 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.
private  ResourceLoader.JarInfo getJarInfo(java.net.URL 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.
private  ResourceHandle getResource(java.net.URL source, java.lang.String name, java.util.Set visitedJars, java.util.Set skip)
           
 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.
private static java.net.URL[] parseClassPath(java.util.jar.JarFile jar, java.net.URL source)
           
private static java.util.Map parseDependentJarIndex(java.net.URL cxt, java.util.jar.JarFile jar)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JAR_INDEX_ENTRY_NAME

private static final java.lang.String JAR_INDEX_ENTRY_NAME
See Also:
Constant Field Values

jarHandler

final java.net.URLStreamHandler jarHandler

url2jarInfo

final java.util.Map url2jarInfo
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 ResourceLoadeer with specified JAR file handler which can implement custom JAR caching policy.

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.


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.


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, i.e. "find next resource" operation is triggered by calling Enumeration.hasMoreElements()>Enumeration.hasMoreElements() 55 .


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, i.e. "find next resource" operation is triggered by calling Enumeration.hasMoreElements()>Enumeration.hasMoreElements() 55 .


getResource

private ResourceHandle getResource(java.net.URL source,
                                   java.lang.String name,
                                   java.util.Set visitedJars,
                                   java.util.Set skip)

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.


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.


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, i.e. "find next resource" operation is triggered by calling Enumeration.hasMoreElements()>Enumeration.hasMoreElements() 55 .


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, i.e. "find next resource" operation is triggered by calling Enumeration.hasMoreElements()>Enumeration.hasMoreElements() 55 .


findResource

private java.net.URL findResource(java.net.URL source,
                                  java.lang.String name,
                                  java.util.Set visitedJars,
                                  java.util.Set skip)

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 "/".


getJarInfo

private ResourceLoader.JarInfo getJarInfo(java.net.URL url)
                                   throws java.net.MalformedURLException

parseDependentJarIndex

private static java.util.Map parseDependentJarIndex(java.net.URL cxt,
                                                    java.util.jar.JarFile jar)
                                             throws java.io.IOException

parseClassPath

private static java.net.URL[] parseClassPath(java.util.jar.JarFile jar,
                                             java.net.URL source)
                                      throws java.io.IOException