java.net

Class URLClassLoader

Known Direct Subclasses:
SystemClassLoader

public class URLClassLoader
extends SecureClassLoader

A secure class loader that can load classes and resources from multiple locations. Given an array of URLs this class loader will retrieve classes and resources by fetching them from possible remote locations. Each URL is searched in order in which it was added. If the file portion of the URL ends with a '/' character then it is interpreted as a base directory, otherwise it is interpreted as a jar file from which the classes/resources are resolved.

New instances can be created by two static newInstance() methods or by three public contructors. Both ways give the option to supply an initial array of URLs and (optionally) a parent classloader (that is different from the standard system class loader).

Normally creating a URLClassLoader throws a SecurityException if a SecurityManager is installed and the checkCreateClassLoader() method does not return true. But the newInstance() methods may be used by any code as long as it has permission to acces the given URLs. URLClassLoaders created by the newInstance() methods also explicitly call the checkPackageAccess() method of SecurityManager if one is installed before trying to load a class. Note that only subclasses of URLClassLoader can add new URLs after the URLClassLoader had been created. But it is always possible to get an array of all URLs that the class loader uses to resolve classes and resources by way of the getURLs() method.

Open issues:

Since:
1.2

Constructor Summary

URLClassLoader(URL[] urls)
Creates a URLClassLoader that gets classes from the supplied URLs.
URLClassLoader(URL[] urls, ClassLoader parent)
Creates a URLClassLoader that gets classes from the supplied URLs.
URLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory)
Creates a URLClassLoader that gets classes from the supplied URLs.

Method Summary

protected void
addURL(URL newUrl)
Adds a new location to the end of the internal URL store.
protected Package
definePackage(String name, Manifest manifest, URL url)
Defines a Package based on the given name and the supplied manifest information.
protected Class
findClass(String className)
Finds (the first) class by name from one of the locations.
URL
findResource(String resourceName)
Finds the first occurrence of a resource that can be found.
Enumeration
findResources(String resourceName)
Finds all the resources with a particular name from all the locations.
protected PermissionCollection
getPermissions(CodeSource source)
Returns the permissions needed to access a particular code source.
URL[]
getURLs()
Returns all the locations that this class loader currently uses the resolve classes and resource.
static URLClassLoader
newInstance(URL[] urls)
Creates a new instance of a URLClassLoader that gets classes from the supplied URLs.
static URLClassLoader
newInstance(URL[] urls, ClassLoader parent)
Creates a new instance of a URLClassLoader that gets classes from the supplied URLs and with the supplied loader as parent class loader.
String
toString()
Returns a String representation of this URLClassLoader giving the actual Class name, the URLs that are searched and the parent ClassLoader.

Methods inherited from class java.security.SecureClassLoader

defineClass, defineClass, getPermissions

Methods inherited from class java.lang.ClassLoader

clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, 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, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

URLClassLoader

public URLClassLoader(URL[] urls)
            throws SecurityException
Creates a URLClassLoader that gets classes from the supplied URLs. To determine if this classloader may be created the constructor of the super class (SecureClassLoader) is called first, which can throw a SecurityException. Then the supplied URLs are added in the order given to the URLClassLoader which uses these URLs to load classes and resources (after using the default parent ClassLoader).
Parameters:
urls - Locations that should be searched by this ClassLoader when resolving Classes or Resources.
Throws:
SecurityException - if the SecurityManager disallows the creation of a ClassLoader.

URLClassLoader

public URLClassLoader(URL[] urls,
                      ClassLoader parent)
            throws SecurityException
Creates a URLClassLoader that gets classes from the supplied URLs. To determine if this classloader may be created the constructor of the super class (SecureClassLoader) is called first, which can throw a SecurityException. Then the supplied URLs are added in the order given to the URLClassLoader which uses these URLs to load classes and resources (after using the supplied parent ClassLoader).
Parameters:
urls - Locations that should be searched by this ClassLoader when resolving Classes or Resources.
parent - The parent class loader used before trying this class loader.
Throws:
SecurityException - if the SecurityManager disallows the creation of a ClassLoader.
SecurityException -

URLClassLoader

public URLClassLoader(URL[] urls,
                      ClassLoader parent,
                      URLStreamHandlerFactory factory)
            throws SecurityException
Creates a URLClassLoader that gets classes from the supplied URLs. To determine if this classloader may be created the constructor of the super class (SecureClassLoader) is called first, which can throw a SecurityException. Then the supplied URLs are added in the order given to the URLClassLoader which uses these URLs to load classes and resources (after using the supplied parent ClassLoader). It will use the supplied URLStreamHandlerFactory to get the protocol handlers of the supplied URLs.
Parameters:
urls - Locations that should be searched by this ClassLoader when resolving Classes or Resources.
parent - The parent class loader used before trying this class loader.
factory - Used to get the protocol handler for the URLs.
Throws:
SecurityException - if the SecurityManager disallows the creation of a ClassLoader.
SecurityException -

Method Details

addURL

protected void addURL(URL newUrl)
Adds a new location to the end of the internal URL store.
Parameters:
newUrl - the location to add

definePackage

protected Package definePackage(String name,
                                Manifest manifest,
                                URL url)
            throws IllegalArgumentException
Defines a Package based on the given name and the supplied manifest information. The manifest indicates the title, version and vendor information of the specification and implementation and whether the package is sealed. If the Manifest indicates that the package is sealed then the Package will be sealed with respect to the supplied URL.
Parameters:
name - The name of the package
manifest - The manifest describing the specification, implementation and sealing details of the package
url - the code source url to seal the package
Returns:
the defined Package
Throws:
IllegalArgumentException - If this package name already exists in this class loader

findClass

protected Class findClass(String className)
            throws ClassNotFoundException
Finds (the first) class by name from one of the locations. The locations are searched in the order they were added to the URLClassLoader.
Overrides:
findClass in interface ClassLoader
Parameters:
className - the classname to find
Returns:
a Class object representing the found class
Throws:
ClassNotFoundException - when the class could not be found or loaded

findResource

public URL findResource(String resourceName)
Finds the first occurrence of a resource that can be found.
Overrides:
findResource in interface ClassLoader
Parameters:
resourceName - the resource name to look for
Returns:
the URL if found, null otherwise

findResources

public Enumeration findResources(String resourceName)
            throws IOException
Finds all the resources with a particular name from all the locations.
Overrides:
findResources in interface ClassLoader
Parameters:
resourceName - the name of the resource to lookup
Returns:
a (possible empty) enumeration of URLs where the resource can be found
Throws:
IOException - when an error occurs accessing one of the locations

getPermissions

protected PermissionCollection getPermissions(CodeSource source)
Returns the permissions needed to access a particular code source. These permissions includes those returned by SecureClassLoader.getPermissions() and the actual permissions to access the objects referenced by the URL of the code source. The extra permissions added depend on the protocol and file portion of the URL in the code source. If the URL has the "file" protocol ends with a '/' character then it must be a directory and a file Permission to read everything in that directory and all subdirectories is added. If the URL had the "file" protocol and doesn't end with a '/' character then it must be a normal file and a file permission to read that file is added. If the URL has any other protocol then a socket permission to connect and accept connections from the host portion of the URL is added.
Overrides:
getPermissions in interface SecureClassLoader
Parameters:
source - The codesource that needs the permissions to be accessed
Returns:
the collection of permissions needed to access the code resource

getURLs

public URL[] getURLs()
Returns all the locations that this class loader currently uses the resolve classes and resource. This includes both the initially supplied URLs as any URLs added later by the loader.
Returns:
All the currently used URLs

newInstance

public static URLClassLoader newInstance(URL[] urls)
            throws SecurityException
Creates a new instance of a URLClassLoader that gets classes from the supplied URLs. This class loader will have as parent the standard system class loader.
Parameters:
urls - the initial URLs used to resolve classes and resources
Returns:
the class loader
Throws:
SecurityException - when the calling code does not have permission to access the given URLs

newInstance

public static URLClassLoader newInstance(URL[] urls,
                                         ClassLoader parent)
            throws SecurityException
Creates a new instance of a URLClassLoader that gets classes from the supplied URLs and with the supplied loader as parent class loader.
Parameters:
urls - the initial URLs used to resolve classes and resources
parent - the parent class loader
Returns:
the class loader
Throws:
SecurityException - when the calling code does not have permission to access the given URLs

toString

public String toString()
Returns a String representation of this URLClassLoader giving the actual Class name, the URLs that are searched and the parent ClassLoader.
Overrides:
toString in interface Object

URLClassLoader.java -- ClassLoader that loads classes from one or more URLs Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.