| CONTENTS | PREV | NEXT | Java Remote Method Invocation |
RMIClassLoader Class
Thejava.rmi.server.RMIClassLoaderclass provides a set of public static utility methods for supporting network-based class loading in RMI. These methods are called by RMI's internal marshal streams to implement the dynamic class loading of types for RMI parameters and return values, but they also may be called directly by applications in order to mimic RMI's class loading behavior. TheRMIClassLoaderclass has no publicly-accessible constructors and thus cannot be instantiated.
package java.rmi.server;
public class RMIClassLoader {
public static String getClassAnnotation(Class cl);
public static ClassLoader getClassLoader(String codebase)
throws java.net.MalformedURLException, SecurityException;
public static Object getSecurityContext(ClassLoader loader);
public static Class loadClass(String name)
throws java.net.MalformedURLException,
ClassNotFoundException;
public static Class loadClass(String codebase, String name)
throws java.net.MalformedURLException,
ClassNotFoundException;
public static Class loadClass(URL codebase, String name)
throws java.net.MalformedURLException,
ClassNotFoundException;
}
ThegetClassAnnotationmethod returns aStringrepresenting the network codebase path that a remote endpoint should use for downloading the definition of the indicated class. The RMI runtime usesStringobjects returned by this method as the annotations for class descriptors in its marshal streams. The format of this codebase string is a path of codebase URL strings delimited by spaces.The codebase string returned depends on the class loader of the supplied class:
| - | the "system class loader" (the class loader used to load classes in the application's "class path" and returned by the method ClassLoader.getSystemClassLoader), |
| - | a parent of the "system class loader" such as the class loader used for installed extensions, |
| - | or null (the "boot class loader" used to load JVM classes), |
java.rmi.server.codebase property is returned, or null is returned if that property is not set.
- Otherwise, if the class loader is an instance of the class
java.net.URLClassLoader, then the codebase string returned is a space-separated list of the external forms of the URLs returned by invoking thegetURLsmethods on the class loader. If theURLClassLoaderwas created by the RMI runtime to service an invocation of one of theRMIClassLoader.loadClassmethods, then no permissions are necessary to get the associated codebase string. If it is an arbitraryURLClassLoaderinstance, the caller must have permission to connect to all of the URLs in the codebase path, as determined by callingopenConnection().getPermission()on eachURLinstance returned by thegetURLsmethod.- Finally, if the class loader is not an instance of
URLClassLoader, then the value of thejava.rmi.server.codebaseproperty is returned, ornullis returned if that property is not set.
ThegetClassLoadermethod returns a class loader that loads classes from the given codebase URL path, a list of space-separated URLs. The class loader returned is the class loader that theloadClass(String,String)method would use to load classes from the given codebase. If a class loader with the same codebase URL path already exists for the RMI runtime, it will be returned; otherwise a new class loader will be created. If the given codebase isnull, it returns the class loader used to load classes via theloadClass(String)method. The method throwsMalformedURLExceptionif the codebase parameter contains an invalid non-nullURL and throwsSecurityExceptionif the caller does not have permission to connect to all of the URLs in the codebase URL path.The
getSecurityContextmethod is deprecated because it is no longer applicable to the Java 2 platform security model; it was used internally in JDK1.1 to implement class loader-based security checks. If the indicated class loader was created by the RMI runtime to service an invocation of one of theRMIClassLoader.loadClassmethods, the first URL in the class loader's codebase path is returned; otherwise,nullis returned.The three
loadClassmethods all attempt to load the class with the specified name using the current thread's context class loader and, if there is a security manager set, an internalURLClassLoaderfor a particular codebase path (depending on the method):
- The
loadClassmethod that only takes one parameter (the class name) implicitly uses the value of thejava.rmi.server.codebaseproperty as the codebase path to use. This version of theloadClassmethod has been deprecated because this use of thejava.rmi.server.codebaseproperty is discouraged; use the following, more general version instead.- The
loadClassmethod with theStringcodebase parameter uses it as the codebase path; the codebase string must be a space-separated list of URLs, as would be returned by thegetClassAnnotationmethod.- The
loadClassmethod with thejava.net.URLcodebase parameter uses that singleURLas the codebase.
For all of theloadClassmethods, the codebase path is used in conjunction with the current thread's context class loader (determined by invokinggetContextClassLoaderon the current thread) to determine the internal class loader instance to attempt to load the class from. The RMI runtime maintains a table of internal class loader instances, keyed by the pair consisting of the parent class loader and the loader's codebase path (an ordered list of URLs). AloadClassmethod looks in the table for aURLClassLoaderinstance with the desired codebase path and the current thread's context class loader as its parent. If no such loader exists, then one is created and added to the table. Finally, theloadClassmethod is called on the chosen class loader with the specified class name.If there is a security manager set (
System.getSecurityManagerdoes not returnnull), the caller ofloadClassmust have permission to connect to all of the URLs in the codebase path, or aClassNotFoundExceptionwill be thrown. In order to prevent arbitrary untrusted code from being loaded into a JVM with no security manager, if there is no security manager set, all of theloadClassmethods will ignore the particular codebase path and only attempt to load the class with the specified name from the current thread's context class loader.