|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.ClassLoader
java.security.SecureClassLoader
java.net.URLClassLoader
public class URLClassLoader
A secure class loader that can load classes and resources from
multiple locations. Given an array of URL
s 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 URL
s 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
URL
s. 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:
definePackage()
and sealing work
precisely?newInstance()
but do we have to use it in more
places?URLStreamHandler
s has not been tested.
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
URL s. |
|
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<URL> |
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 URL s. |
static URLClassLoader |
newInstance(URL[] urls,
ClassLoader parent)
Creates a new instance of a URLClassLoader that gets
classes from the supplied URL s 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 |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public URLClassLoader(URL[] urls) throws SecurityException
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).
urls
- Locations that should be searched by this ClassLoader when
resolving Classes or Resources.
SecurityException
- if the SecurityManager disallows the
creation of a ClassLoader.SecureClassLoader
public URLClassLoader(URL[] urls, ClassLoader parent) throws SecurityException
URLClassLoader
that gets classes from the supplied
URL
s.
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).
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.
SecurityException
- if the SecurityManager disallows the
creation of a ClassLoader.
SecurityException
SecureClassLoader
public URLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) throws SecurityException
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.
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.
SecurityException
- if the SecurityManager disallows the
creation of a ClassLoader.
SecurityException
SecureClassLoader
Method Detail |
---|
protected void addURL(URL newUrl)
newUrl
- the location to addprotected Package definePackage(String name, Manifest manifest, URL url) throws IllegalArgumentException
name
- The name of the packagemanifest
- The manifest describing the specification,
implementation and sealing details of the packageurl
- the code source url to seal the package
IllegalArgumentException
- If this package name already exists
in this class loaderprotected Class<?> findClass(String className) throws ClassNotFoundException
findClass
in class ClassLoader
className
- the classname to find
ClassNotFoundException
- when the class could not be found or
loadedpublic String toString()
toString
in class Object
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
public URL findResource(String resourceName)
findResource
in class ClassLoader
resourceName
- the resource name to look for
public Enumeration<URL> findResources(String resourceName) throws IOException
findResources
in class ClassLoader
resourceName
- the name of the resource to lookup
IOException
- when an error occurs accessing one of the
locationsprotected PermissionCollection getPermissions(CodeSource source)
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.
getPermissions
in class SecureClassLoader
source
- The codesource that needs the permissions to be accessed
SecureClassLoader.getPermissions(CodeSource)
public URL[] getURLs()
public static URLClassLoader newInstance(URL[] urls) throws SecurityException
URLClassLoader
that gets
classes from the supplied URL
s. This class loader
will have as parent the standard system class loader.
urls
- the initial URLs used to resolve classes and
resources
SecurityException
- when the calling code does not have
permission to access the given URL
spublic static URLClassLoader newInstance(URL[] urls, ClassLoader parent) throws SecurityException
URLClassLoader
that gets
classes from the supplied URL
s and with the supplied
loader as parent class loader.
urls
- the initial URLs used to resolve classes and
resourcesparent
- the parent class loader
SecurityException
- when the calling code does not have
permission to access the given URL
s
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |