org.xwiki.classloader
Class URIClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by org.xwiki.classloader.ExtendedURLClassLoader
                  extended by org.xwiki.classloader.URIClassLoader
Direct Known Subclasses:
NamespaceURLClassLoader

public class URIClassLoader
extends ExtendedURLClassLoader

Equivalent of java.net.URLClassloader but without bugs related to ill-formed URLs and with customizable JAR caching policy. The standard URLClassLoader accepts URLs containing spaces and other characters which are forbidden in the URI syntax, according to the RFC 2396. As a workaround to this problem, Java escapes and un-escapes URLs in various arbitrary places; however, this is inconsistent and leads to numerous problems with URLs referring to local files with spaces in the path. SUN acknowledges the problem, but refuses to modify the behavior for compatibility reasons; see Java Bug Parade 4273532, 4466485.

Additionally, the JAR caching policy used by URLClassLoader is system-wide and inflexible: once downloaded JAR files are never re-downloaded, even if one creates a fresh instance of the class loader that happens to have the same URL in its search path. In fact, that policy is a security vulnerability: it is possible to crash any URL class loader, thus affecting potentially separate part of the system, by creating URL connection to one of the URLs of that class loader search path and closing the associated JAR file. See Java Bug Parade 4405789, 4388666, 4639900.

This class avoids these problems by 1) using URIs instead of URLs for the search path (thus enforcing strict syntax conformance and defining precise escaping semantics), and 2) using custom URLStreamHandler which ensures per-classloader JAR caching policy.

Originally written by Dawid Kurzyniec and released to the public domain, as explained at http://creativecommons.org/licenses/publicdomain

Source: http://dcl.mathcs.emory.edu/php/loadPage.php?content=util/features.html#classloading

Since:
2.0.1
Version:
$Id: 2688ff01039a9dfef53380d1f7efbdcb4415884b $d
See Also:
File.toURL(), File.toURI()

Constructor Summary
URIClassLoader(URI[] uris)
          Creates URIClassLoader with the specified search path.
URIClassLoader(URI[] uris, ClassLoader parent)
          Creates URIClassLoader with the specified search path and parent class loader.
URIClassLoader(URI[] uris, ClassLoader parent, URLStreamHandlerFactory handlerFactory)
          Creates URIClassLoader with the specified search path and parent class loader.
URIClassLoader(URI[] uris, URLStreamHandlerFactory handlerFactory)
          Creates URIClassLoader with the specified search path.
 
Method Summary
protected  void addURI(URI uri)
          Add specified URI at the end of the search path.
 void addURL(URL url)
          Add specified URL at the end of the search path.
 void addURLs(List<URL> urls)
          Add specified URLs at the end of the search path.
protected  Class<?> defineClass(String name, edu.emory.mathcs.util.classloader.ResourceHandle h)
           
protected  Class<?> findClass(String name)
          Finds and loads the class with the specified name.
protected  String findLibrary(String libname)
          Returns the absolute path name of a native library.
 URL findResource(String name)
          Finds the resource with the specified name.
 Enumeration<URL> findResources(String name)
          Returns an Enumeration of URLs representing all of the resources having the specified name.
protected  edu.emory.mathcs.util.classloader.ResourceHandle getClassHandle(String name)
          Finds the ResourceHandle object for the class with the specified name.
protected  edu.emory.mathcs.util.classloader.ResourceHandle getLibraryHandle(String name)
          Finds the ResourceHandle object for the native library with the specified name.
protected  edu.emory.mathcs.util.classloader.ResourceHandle getResourceHandle(String name)
          Finds the ResourceHandle object for the resource with the specified name.
protected  Enumeration<edu.emory.mathcs.util.classloader.ResourceHandle> getResourceHandles(String name)
          Returns an Enumeration of ResourceHandle objects representing all of the resources having the specified name.
 URL[] getURLs()
           
 
Methods inherited from class java.net.URLClassLoader
definePackage, getPermissions, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

URIClassLoader

public URIClassLoader(URI[] uris)
Creates URIClassLoader with the specified search path.

Parameters:
uris - the search path

URIClassLoader

public URIClassLoader(URI[] uris,
                      URLStreamHandlerFactory handlerFactory)
Creates URIClassLoader with the specified search path.

Parameters:
uris - the search path
handlerFactory - the URLStreamHandlerFactory to use when creating URLs

URIClassLoader

public URIClassLoader(URI[] uris,
                      ClassLoader parent)
Creates URIClassLoader with the specified search path and parent class loader.

Parameters:
uris - the search path
parent - the parent class loader

URIClassLoader

public URIClassLoader(URI[] uris,
                      ClassLoader parent,
                      URLStreamHandlerFactory handlerFactory)
Creates URIClassLoader with the specified search path and parent class loader.

Parameters:
uris - the search path
parent - the parent class loader.
handlerFactory - the URLStreamHandlerFactory to use when creating URLs
Method Detail

addURI

protected void addURI(URI uri)
Add specified URI at the end of the search path.

Parameters:
uri - the URI to add

addURL

public void addURL(URL url)
Add specified URL at the end of the search path.

Overrides:
addURL in class ExtendedURLClassLoader
Parameters:
url - the URL to add

addURLs

public void addURLs(List<URL> urls)
Add specified URLs at the end of the search path.

Overrides:
addURLs in class ExtendedURLClassLoader
Parameters:
urls - the URLs to add

getURLs

public URL[] getURLs()
Overrides:
getURLs in class URLClassLoader

findClass

protected Class<?> findClass(String name)
                      throws ClassNotFoundException
Finds and loads the class with the specified name.

Overrides:
findClass in class URLClassLoader
Parameters:
name - the name of the class
Returns:
the resulting class
Throws:
ClassNotFoundException - if the class could not be found

defineClass

protected Class<?> defineClass(String name,
                               edu.emory.mathcs.util.classloader.ResourceHandle h)
                        throws IOException
Throws:
IOException

findResource

public URL findResource(String name)
Finds the resource with the specified name.

Overrides:
findResource in class URLClassLoader
Parameters:
name - the name of the resource
Returns:
a URL for the resource, or null if the resource could not be found.

findResources

public Enumeration<URL> findResources(String name)
                               throws IOException
Returns an Enumeration of URLs representing all of the resources having the specified name.

Overrides:
findResources in class URLClassLoader
Parameters:
name - the resource name
Returns:
an Enumeration of URLs
Throws:
IOException - if an I/O exception occurs

findLibrary

protected String findLibrary(String libname)
Returns the absolute path name of a native library. The VM invokes this method to locate the native libraries that belong to classes loaded with this class loader. If this method returns null, the VM searches the library along the path specified as the java.library.path property. This method invoke getLibraryHandle(java.lang.String) method to find handle of this library. If the handle is found and its URL protocol is "file", the system-dependent absolute library file path is returned. Otherwise this method returns null.

Subclasses can override this method to provide specific approaches in library searching.

Overrides:
findLibrary in class ClassLoader
Parameters:
libname - the library name
Returns:
the absolute path of the native library
See Also:
System.loadLibrary(java.lang.String), System.mapLibraryName(java.lang.String)

getClassHandle

protected edu.emory.mathcs.util.classloader.ResourceHandle getClassHandle(String name)
Finds the ResourceHandle object for the class with the specified name. Unlike findClass(), this method does not load the class.

Parameters:
name - the name of the class
Returns:
the ResourceHandle of the class

getResourceHandle

protected edu.emory.mathcs.util.classloader.ResourceHandle getResourceHandle(String name)
Finds the ResourceHandle object for the resource with the specified name.

Parameters:
name - the name of the resource
Returns:
the ResourceHandle of the resource

getLibraryHandle

protected edu.emory.mathcs.util.classloader.ResourceHandle getLibraryHandle(String name)
Finds the ResourceHandle object for the native library with the specified name. The library name must be '/'-separated path. The last part of this path is substituted by its system-dependent mapping (using System.mapLibraryName(String) method). Next, the ResourceFinder is used to look for the library as it was ordinary resource.

Subclasses can override this method to provide specific approaches in library searching.

Parameters:
name - the name of the library
Returns:
the ResourceHandle of the library

getResourceHandles

protected Enumeration<edu.emory.mathcs.util.classloader.ResourceHandle> getResourceHandles(String name)
Returns an Enumeration of ResourceHandle objects representing all of the resources having the specified name.

Parameters:
name - the name of the resource
Returns:
the ResourceHandle of the resource


Copyright © 2004-2012 XWiki. All Rights Reserved.