Class PreferredClassLoader
- All Implemented Interfaces:
Closeable,AutoCloseable,ClassAnnotation
A preferred class is a class that is to be loaded by a class loader without the loader delegating to its parent class loader first. Resources may also be preferred.
Like URLClassLoader, PreferredClassLoader loads classes and
resources from a search path of URLs. If a URL in the path ends with a '/', it is
assumed to refer to a directory; otherwise, the URL is assumed to refer to a JAR file.
The location of the first URL in the path can contain a preferred list for the entire
path. A preferred list declares names of certain classes and other resources throughout the path
as being preferred or not. When a PreferredClassLoader is asked to load a
class or resource that is preferred (according to the preferred list) and the class or resource
exists in the loader's path of URLs, the loader will not delegate first to its parent class
loader as it otherwise would do; instead, it will attempt to load the class or resource from its
own path of URLs only.
The preferred list for a path of URLs, if one exists, is located relative to the first URL in
the path. If the first URL refers to a JAR file, then the preferred list is the contents of the
file named "META-INF/PREFERRED.LIST" within that JAR file. If the first URL refers
to a directory, then the preferred list is the contents of the file at the location
"META-INF/PREFERRED.LIST" relative to that directory URL. If there is no preferred
list at the required location, then no classes or resources are preferred for the path of URLs. A
preferred list at any other location (such as relative to one of the other URLs in the path) is
ignored.
Note that a class or resource is only considered to be preferred if the preferred list declares the name of the class or resource as being preferred and the class or resource actually exists in the path of URLs.
Preferred List Syntax
A preferred list is a UTF-8 encoded text file, with lines separated by CR LF, LF, or CR (not followed by an LF). Multiple whitespace characters in a line are equivalent to a single whitespace character, and whitespace characters at the beginning or end of a line are ignored. If the first non-whitespace character of a line is'#', the line is a comment and is
equivalent to a blank line.
The first line of a preferred list must contain a version number in the following format:
PreferredResources-Version: 1.x
This specification defines only version 1.0, but PreferredClassLoader will parse any
version 1.x, x>=0 with the format and semantics specified here.
After the version number line, a preferred list comprises an optional default preferred entry followed by zero or more named preferred entries. A preferred list must contain either a default preferred entry or at least one named preferred entry. Blank lines are allowed before and after preferred entries, as well as between the lines of a named preferred entry.
A default preferred entry is a single line in the following format:
Preferred: preferred-setting
where preferred-setting is a non-empty sequence of characters. If
preferred-setting equals "true" (case insensitive), then resource names not
matched by any of the named preferred entries are by default preferred; otherwise, resource names
not matched by any of the named preferred entries are by default not preferred. If there is no
default preferred entry, then resource names are by default not preferred.
A named preferred entry is two lines in the following format:
Name: name-expression
Preferred: preferred-setting
where name-expression and preferred-setting are non-empty sequences of characters.
If preferred-setting equals "true" (case insensitive), then resource names
that are matched by name-expression (and not any more specific named preferred entries)
are preferred; otherwise, resource names that are matched by name-expression (and not any
more specific named preferred entries) are not preferred.
If name-expression ends with ".class", it matches a class whose binary
name is name-expression without the ".class" suffix and with each
'/' character replaced with a '.'. It also matches any class whose
binary name starts with that same value followed by a '$'; this rule is intended to
match nested classes that have an enclosing class of that name, so that the preferred settings of
a class and all of its nested classes are the same by default. It is possible, but strongly
discouraged, to override the preferred setting of a nested class with a named preferred entry
that explicitly matches the nested class's binary name.
name-expression may match arbitrary resource names as well as class names, with path
elements separated by '/' characters.
If name-expression ends with "/" or "/*", then the entry is a
directory wildcard entry that matches all resources (including classes) in the named directory.
If name-expression ends with "/-", then the entry is a namespace wildcard
entry that matches all resources (including classes) in the named directory and all of its
subdirectories.
When more than one named preferred entry matches a class or resource name, then the most specific entry takes precedence. A non-wildcard entry is more specific than a wildcard entry. A directory wildcard entry is more specific than a namespace wildcard entry. A namespace wildcard entry with more path elements is more specific than a namespace wildcard entry with fewer path elements. Given two non-wildcard entries, the entry with the longer name-expression is more specific (this rule is only significant when matching a class). The order of named preferred entries is insignificant.
Example Preferred List
Following is an example preferred list:
PreferredResources-Version: 1.0
Preferred: false
Name: com/foo/FooBar.class
Preferred: true
Name: com/foo/*
Preferred: false
Name: com/foo/-
Preferred: true
Name: image-files/*
Preferred: mumble
The class com.foo.FooBar is preferred, as well as any nested classes that have it
as an enclosing class. All other classes in the com.foo package are not preferred
because of the directory wildcard entry. Classes in subpackages of com.foo are
preferred because of the namespace wildcard entry. Resources in the directory
"com/foo/" are not preferred, and resources in subdirectories of
"com/foo/" are preferred. Resources in the directory "image-files/"
are not preferred because preferred settings other than "true" are interpreted as
false. Classes that are in a package named com.bar are not preferred because of the
default preferred entry.
- Since:
- 2.0
- Author:
- Sun Microsystems, Inc.
-
Constructor Summary
ConstructorsConstructorDescriptionPreferredClassLoader(URL[] urls, ClassLoader parent, String exportAnnotation, boolean requireDlPerm) Creates a newPreferredClassLoaderthat loads classes and resources from the specified path of URLs and delegates to the specified parent class loader.PreferredClassLoader(URL[] urls, ClassLoader parent, String exportAnnotation, boolean requireDlPerm, URLStreamHandlerFactory factory) Creates a newPreferredClassLoaderthat loads classes and resources from the specified path of URLs, delegates to the specified parent class loader, and uses the specifiedURLStreamHandlerFactorywhen creating new URL objects. -
Method Summary
Modifier and TypeMethodDescriptionprotected PackagedefinePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) Returns the class annotation string for classes defined by this class loader.protected PermissionCollectiongetPermissions(CodeSource codeSource) Returns the static permissions to be automatically granted to classes loaded from the specifiedCodeSourceand defined by this class loader.getResource(String name) Gets a resource with the specified name.protected booleanisPreferredResource(String name, boolean isClass) Returnstrueif a class or resource with the specified name is preferred for this class loader, andfalseif a class or resource with the specified name is not preferred for this loader.protected ClassLoads a class with the specified name.static PreferredClassLoadernewInstance(URL[] urls, ClassLoader parent, String exportAnnotation, boolean requireDlPerm) Creates a new instance ofPreferredClassLoaderthat loads classes and resources from the specified path of URLs and delegates to the specified parent class loader.toString()Returns a string representation of this class loader.Methods inherited from class java.net.URLClassLoader
addURL, close, definePackage, findClass, findResource, findResources, getResourceAsStream, getURLs, newInstance, newInstanceMethods inherited from class java.security.SecureClassLoader
defineClass, defineClassMethods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Constructor Details
-
PreferredClassLoader
public PreferredClassLoader(URL[] urls, ClassLoader parent, String exportAnnotation, boolean requireDlPerm) Creates a newPreferredClassLoaderthat loads classes and resources from the specified path of URLs and delegates to the specified parent class loader.If
exportAnnotationis notnull, then it will be used as the return value of the loader'sgetClassAnnotationmethod. IfexportAnnotationisnull, the loader'sgetClassAnnotationmethod will return a space-separated list of the URLs in the specified path. TheexportAnnotationparameter can be used to specify so-called "export" URLs, from which other parties should load classes defined by the loader and which are different from the "import" URLs that the classes are actually loaded from.If
requireDlPermistrue, the loader'sgetPermissionsmethod will require that theCodeSourceof any class defined by the loader is grantedDownloadPermission.- Parameters:
urls- the path of URLs to load classes and resources fromparent- the parent class loader for delegationexportAnnotation- the export class annotation string to use for classes defined by this loader, ornullrequireDlPerm- iftrue, the loader will only define classes with aCodeSourcethat is grantedDownloadPermission- Throws:
SecurityException- if there is a security manager and an invocation of itscheckCreateClassLoadermethod fails
-
PreferredClassLoader
public PreferredClassLoader(URL[] urls, ClassLoader parent, String exportAnnotation, boolean requireDlPerm, URLStreamHandlerFactory factory) Creates a newPreferredClassLoaderthat loads classes and resources from the specified path of URLs, delegates to the specified parent class loader, and uses the specifiedURLStreamHandlerFactorywhen creating new URL objects. This constructor passesfactoryto the superclass constructor that has aURLStreamHandlerFactoryparameter.If
exportAnnotationis notnull, then it will be used as the return value of the loader'sgetClassAnnotationmethod. IfexportAnnotationisnull, the loader'sgetClassAnnotationmethod will return a space-separated list of the URLs in the specified path. TheexportAnnotationparameter can be used to specify so-called "export" URLs, from which other parties should load classes defined by the loader and which are different from the "import" URLs that the classes are actually loaded from.If
requireDlPermistrue, the loader'sgetPermissionsmethod will require that theCodeSourceof any class defined by the loader is grantedDownloadPermission.- Parameters:
urls- the path of URLs to load classes and resources fromparent- the parent class loader for delegationexportAnnotation- the export class annotation string to use for classes defined by this loader, ornullrequireDlPerm- iftrue, the loader will only define classes with aCodeSourcethat is grantedDownloadPermissionfactory- theURLStreamHandlerFactoryto use when creating new URL objects, ornull- Throws:
SecurityException- if there is a security manager and an invocation of itscheckCreateClassLoadermethod fails- Since:
- 2.1
-
-
Method Details
-
newInstance
public static PreferredClassLoader newInstance(URL[] urls, ClassLoader parent, String exportAnnotation, boolean requireDlPerm) Creates a new instance ofPreferredClassLoaderthat loads classes and resources from the specified path of URLs and delegates to the specified parent class loader.The
exportAnnotationandrequireDlPermparameters have the same semantics as they do for the constructors.The
loadClassmethod of the returnedPreferredClassLoaderwill, if there is a security manager, invoke itscheckPackageAccessmethod with the package name of the class to load before attempting to load the class; this could result in aSecurityExceptionbeing thrown fromloadClass.- Parameters:
urls- the path of URLs to load classes and resources fromparent- the parent class loader for delegationexportAnnotation- the export class annotation string to use for classes defined by this loader, ornullrequireDlPerm- iftrue, the loader will only define classes with aCodeSourcethat is grantedDownloadPermission- Returns:
- the new
PreferredClassLoaderinstance - Throws:
SecurityException- if the current security context does not have the permissions necessary to connect to all of the URLs inurls
-
isPreferredResource
Returnstrueif a class or resource with the specified name is preferred for this class loader, andfalseif a class or resource with the specified name is not preferred for this loader.If
isClassistrue, thennameis interpreted as the binary name of a class; otherwise,nameis interpreted as the full path of a resource.This method only returns
trueif a class or resource with the specified name exists in the this loader's path of URLs and the name is preferred in the preferred list. This method returnsfalseif the name is not preferred in the preferred list or if the name is preferred with the default preferred entry or a wildcard preferred entry and the class or resource does not exist in the path of URLs.- Parameters:
name- the name of the class or resourceisClass-trueifnameis a binary class name, andfalseifnameis the full path of a resource- Returns:
trueif a class or resource namednameis preferred for this loader, andfalseif a class or resource namednameis not preferred for this loader- Throws:
IOException- if the preferred list cannot definitely be determined to exist or not exist, or if the preferred list contains a syntax error, or if the name is preferred with the default preferred entry or a wildcard preferred entry and the class or resource cannot definitely be determined to exist or not exist in the path of URLs, or if the name is preferred with a non-wildcard entry and the class or resource does not exist or cannot definitely be determined to exist in the path of URLs
-
loadClass
Loads a class with the specified name.PreferredClassLoaderimplements this method as follows:This method first invokes
findLoadedClasswithname; iffindLoadedClassreturns a non-nullClass, then this method returns thatClass.Otherwise, this method invokes
isPreferredResourcewithnameas the first argument andtrueas the second argument:- If
isPreferredResourcethrows anIOException, then this method throws aClassNotFoundExceptioncontaining theIOExceptionas its cause. - If
isPreferredResourcereturnstrue, then this method invokesfindClasswithname. IffindClassthrows an exception, then this method throws that exception. Otherwise, this method returns theClassreturned byfindClass, and ifresolveistrue,resolveClassis invoked with theClassbefore returning. - If
isPreferredResourcereturnsfalse, then this method invokes the superclass implementation ofloadClasswithnameandresolveand returns the result. If the superclass'sloadClassthrows an exception, then this method throws that exception.
- Overrides:
loadClassin classClassLoader- Parameters:
name- the binary name of the class to loadresolve- iftrue, thenresolveClasswill be invoked with the loaded class before returning- Returns:
- the loaded class
- Throws:
ClassNotFoundException- if the class could not be found
- If
-
getResource
Gets a resource with the specified name.PreferredClassLoaderimplements this method as follows:This method invokes
isPreferredResourcewithnameas the first argument andfalseas the second argument:- If
isPreferredResourcethrows anIOException, then this method returnsnull. - If
isPreferredResourcereturnstrue, then this method invokesfindResourcewithnameand returns the result. - If
isPreferredResourcereturnsfalse, then this method invokes the superclass implementation ofgetResourcewithnameand returns the result.
- Overrides:
getResourcein classClassLoader- Parameters:
name- the name of the resource to get- Returns:
- a
URLfor the resource, ornullif the resource could not be found
- If
-
definePackage
protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) - Overrides:
definePackagein classClassLoader
-
getClassAnnotation
Returns the class annotation string for classes defined by this class loader. The returned value may benull.PreferredClassLoaderimplements this method as follows:If this
PreferredClassLoaderwas constructed with a non-nullexport class annotation string, then this method returns that string. Otherwise, this method returns a space-separated list of this loader's path of URLs.- Specified by:
getClassAnnotationin interfaceClassAnnotation- Returns:
- the class annotation string for classes defined by this loader, or
null
-
getPermissions
Returns the static permissions to be automatically granted to classes loaded from the specifiedCodeSourceand defined by this class loader.PreferredClassLoaderimplements this method as follows:If there is a security manager and this
PreferredClassLoaderwas constructed to enforceDownloadPermission, then this method checks that the current security policy grants the specifiedCodeSourcethe permissionDownloadPermission("permit"); if that check fails, then this method throws aSecurityException.Then this method invokes the superclass implementation of
getPermissionsand returns the result.- Overrides:
getPermissionsin classURLClassLoader- Parameters:
codeSource- theCodeSourceto return the permissions to be granted to- Returns:
- the permissions to be granted to the
CodeSource - Throws:
SecurityException- if there is a security manager, thisPreferredClassLoaderwas constructed to enforceDownloadPermission, and the current security policy does not grant the specifiedCodeSourcethe permissionDownloadPermission("permit")
-
toString
Returns a string representation of this class loader.
-