APIdia Launch Partner
Top Description Fields Constructors Methods
java.rmi.server

public Class RMIClassLoader

extends Object
Class Inheritance
Imports
java.net.MalformedURLException, .URL, java.util.Iterator, .ServiceLoader

RMIClassLoader comprises static methods to support dynamic class loading with RMI. Included are methods for loading classes from a network location (one or more URLs) and obtaining the location from which an existing class should be loaded by remote parties. These methods are used by the RMI runtime when marshalling and unmarshalling classes contained in the arguments and return values of remote method calls.

The implementation of the following static methods

is provided by an instance of RMIClassLoaderSpi, the service provider interface for those methods. When one of the methods is invoked, its behavior is to delegate to a corresponding method on the service provider instance. The details of how each method delegates to the provider instance is described in the documentation for each particular method.

The service provider instance is chosen as follows:

Authors
Ann Wollrath, Peter Jones, Laird Dornin
Since
1.1
See Also
RMIClassLoaderSpi

Field Summary

Modifier and TypeField and Description
private static final RMIClassLoaderSpi
defaultProvider

"default" provider instance

private static final RMIClassLoaderSpi
provider

provider instance

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

a string to be used to annotate the given class when it gets marshalled, or null
getClassAnnotation
(Class<?>
the class to obtain the annotation for
cl
)

Returns the annotation string (representing a location for the class definition) that RMI will use to annotate the class descriptor when marshalling objects of the given class.

public static ClassLoader

Returns:

a class loader that loads classes from the given codebase URL path
getClassLoader
(String
the list of URLs (space-separated) from which the returned class loader will load classes from, or null
codebase
)

Returns a class loader that loads classes from the given codebase URL path.

public static RMIClassLoaderSpi

Returns:

the canonical instance of the default service provider
getDefaultProviderInstance
()

Returns the canonical instance of the default provider for the service provider interface RMIClassLoaderSpi.

public static Object

Returns:

null
getSecurityContext
(ClassLoader
a class loader from which to get the security context
loader
)
Deprecated no replacement. This method has no purpose in the absence of a Security Manager.

Always returns null.

private static RMIClassLoaderSpi
initializeProvider()

Chooses provider instance, following above documentation.

public static Class<?>

Returns:

the Class object representing the loaded class
loadClass
(String
the name of the class to load
name
)
Deprecated replaced by loadClass(String,String) method

Loads the class with the specified name.

public static Class<?>

Returns:

the Class object representing the loaded class
loadClass
(URL
the URL to load the class from, or null
codebase
,
String
the name of the class to load
name
)

Loads a class from a codebase URL.

public static Class<?>

Returns:

the Class object representing the loaded class
loadClass
(String
the list of URLs (separated by spaces) to load the class from, or null
codebase
,
String
the name of the class to load
name
)

Loads a class from a codebase URL path.

public static Class<?>

Returns:

the Class object representing the loaded class
loadClass
(String
the list of URLs (separated by spaces) to load the class from, or null
codebase
,
String
the name of the class to load
name
,
ClassLoader
additional contextual class loader to use, or null
defaultLoader
)

Loads a class from a codebase URL path, optionally using the supplied loader.

public static Class<?>

Returns:

a dynamic proxy class that implements the named interfaces
loadProxyClass
(String
the list of URLs (space-separated) to load classes from, or null
codebase
,
String[]
the names of the interfaces for the proxy class to implement
interfaces
,
ClassLoader
additional contextual class loader to use, or null
defaultLoader
)

Loads a dynamic proxy class (see java.lang.reflect.Proxy) that implements a set of interfaces with the given names from a codebase URL path.

private static RMIClassLoaderSpi
newDefaultProviderInstance()

Creates an instance of the default provider class.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultProviderback to summary
private static final RMIClassLoaderSpi defaultProvider

"default" provider instance

providerback to summary
private static final RMIClassLoaderSpi provider

provider instance

Constructor Detail

RMIClassLoaderback to summary
private RMIClassLoader()

Method Detail

getClassAnnotationback to summary
public static String getClassAnnotation(Class<?> cl)

Returns the annotation string (representing a location for the class definition) that RMI will use to annotate the class descriptor when marshalling objects of the given class.

This method delegates to the RMIClassLoaderSpi#getClassAnnotation(Class) method of the provider instance, passing cl as the argument.

Parameters
cl:Class<?>

the class to obtain the annotation for

Returns:String

a string to be used to annotate the given class when it gets marshalled, or null

Exceptions
NullPointerException:
if cl is null
Since
1.2
getClassLoaderback to summary
public static ClassLoader getClassLoader(String codebase) throws MalformedURLException

Returns a class loader that loads classes from the given codebase URL path.

The class loader returned is the class loader that the loadClass(String, String) method would use to load classes for the same codebase argument.

This method delegates to the RMIClassLoaderSpi#getClassLoader(String) method of the provider instance, passing codebase as the argument.

Parameters
codebase:String

the list of URLs (space-separated) from which the returned class loader will load classes from, or null

Returns:ClassLoader

a class loader that loads classes from the given codebase URL path

Exceptions
MalformedURLException:
if codebase is non-null and contains an invalid URL, or if codebase is null and a provider-specific URL used to identify the class loader is invalid
Since
1.3
getDefaultProviderInstanceback to summary
public static RMIClassLoaderSpi getDefaultProviderInstance()

Returns the canonical instance of the default provider for the service provider interface RMIClassLoaderSpi. If the system property java.rmi.server.RMIClassLoaderSpi is not defined, then the RMIClassLoader static methods

will use the canonical instance of the default provider as the service provider instance.

The default service provider instance implements RMIClassLoaderSpi as follows:

The getClassAnnotation method returns a String representing the codebase URL path that a remote party should use to download the definition for the specified class. The format of the returned string is a path of URLs separated by spaces. The codebase string returned depends on the defining class loader of the specified class:

  • If the class loader is the system class loader (see ClassLoader#getSystemClassLoader), a parent of the system class loader such as the loader used for installed extensions, or the bootstrap class loader (which may be represented by null), then the value of the java.rmi.server.codebase property (or possibly an earlier cached value) is returned, or null is returned if that property is not set.

  • Otherwise, if the class loader is an instance of URLClassLoader, then the returned string is a space-separated list of the external forms of the URLs returned by invoking the getURLs methods of the loader.

  • Finally, if the class loader is not an instance of URLClassLoader, then the value of the java.rmi.server.codebase property (or possibly an earlier cached value) is returned, or null is returned if that property is not set.

For the implementations of the methods described below, which all take a String parameter named codebase that is a space-separated list of URLs, the codebase argument is ignored. Class loading proceeds using the the current thread's context class loader (see Thread#getContextClassLoader()), which is also considered to be the codebase loader, irrespective of any value passed as the codebase argument.

The getClassLoader method returns the current thread's context class loader.

The loadClass method attempts to load the class with the specified name as follows:

If the defaultLoader argument is non-null, it first attempts to load the class with the specified name using the defaultLoader, as if by evaluating
    Class.forName(name, false, defaultLoader)
If the class is successfully loaded from the defaultLoader, that class is returned. If an exception other than ClassNotFoundException is thrown, that exception is thrown to the caller.

Next, the loadClass method attempts to load the class with the specified name using the current thread's context class loader.

The loadProxyClass method attempts to return a dynamic proxy class with the named interface as follows:

If the defaultLoader argument is non-null and all of the named interfaces can be resolved through that loader, then,

  • if all of the resolved interfaces are public, then it first attempts to obtain a dynamic proxy class (using Proxy.getProxyClass) for the resolved interfaces defined in the codebase loader; if that attempt throws an IllegalArgumentException, it then attempts to obtain a dynamic proxy class for the resolved interfaces defined in the defaultLoader. If both attempts throw IllegalArgumentException, then this method throws a ClassNotFoundException. If any other exception is thrown, that exception is thrown to the caller.
  • if all of the non-public resolved interfaces are defined in the same class loader, then it attempts to obtain a dynamic proxy class for the resolved interfaces defined in that loader.
  • otherwise, a LinkageError is thrown (because a class that implements all of the specified interfaces cannot be defined in any loader).

Otherwise, if all of the named interfaces can be resolved through the codebase loader, then,

  • if all of the resolved interfaces are public, then it attempts to obtain a dynamic proxy class for the resolved interfaces in the codebase loader. If the attempt throws an IllegalArgumentException, then this method throws a ClassNotFoundException.
  • if all of the non-public resolved interfaces are defined in the same class loader, then it attempts to obtain a dynamic proxy class for the resolved interfaces defined in that loader.
  • otherwise, a LinkageError is thrown (because a class that implements all of the specified interfaces cannot be defined in any loader).

Otherwise, a ClassNotFoundException is thrown for one of the named interfaces that could not be resolved.

Returns:RMIClassLoaderSpi

the canonical instance of the default service provider

Since
1.4
getSecurityContextback to summary
public static Object getSecurityContext(ClassLoader loader)

Deprecated

no replacement. This method has no purpose in the absence of a Security Manager.

Always returns null.

Parameters
loader:ClassLoader

a class loader from which to get the security context

Returns:Object

null

Annotations
@Deprecated
initializeProviderback to summary
private static RMIClassLoaderSpi initializeProvider()

Chooses provider instance, following above documentation. This method assumes that it has been invoked in a privileged block.

loadClassback to summary
public static Class<?> loadClass(String name) throws MalformedURLException, ClassNotFoundException

Deprecated

replaced by loadClass(String,String) method

Loads the class with the specified name.

This method delegates to loadClass(String, String), passing null as the first argument and name as the second argument.

Parameters
name:String

the name of the class to load

Returns:Class<?>

the Class object representing the loaded class

Annotations
@Deprecated
Exceptions
MalformedURLException:
if a provider-specific URL used to load classes is invalid
ClassNotFoundException:
if a definition for the class could not be found at the codebase location
See Also
loadClass(String, String)
loadClassback to summary
public static Class<?> loadClass(URL codebase, String name) throws MalformedURLException, ClassNotFoundException

Loads a class from a codebase URL. If codebase is null, then this method will behave the same as loadClass(String, String) with a null codebase and the given class name.

This method delegates to the RMIClassLoaderSpi#loadClass(String, String, ClassLoader) method of the provider instance, passing the result of invoking URL#toString on the given URL (or null if codebase is null) as the first argument, name as the second argument, and null as the third argument.

Parameters
codebase:URL

the URL to load the class from, or null

name:String

the name of the class to load

Returns:Class<?>

the Class object representing the loaded class

Exceptions
MalformedURLException:
if codebase is null and a provider-specific URL used to load classes is invalid
ClassNotFoundException:
if a definition for the class could not be found at the specified URL
loadClassback to summary
public static Class<?> loadClass(String codebase, String name) throws MalformedURLException, ClassNotFoundException

Loads a class from a codebase URL path.

This method delegates to the RMIClassLoaderSpi#loadClass(String, String, ClassLoader) method of the provider instance, passing codebase as the first argument, name as the second argument, and null as the third argument.

Parameters
codebase:String

the list of URLs (separated by spaces) to load the class from, or null

name:String

the name of the class to load

Returns:Class<?>

the Class object representing the loaded class

Exceptions
MalformedURLException:
if codebase is non-null and contains an invalid URL, or if codebase is null and a provider-specific URL used to load classes is invalid
ClassNotFoundException:
if a definition for the class could not be found at the specified location
Since
1.2
loadClassback to summary
public static Class<?> loadClass(String codebase, String name, ClassLoader defaultLoader) throws MalformedURLException, ClassNotFoundException

Loads a class from a codebase URL path, optionally using the supplied loader. This method should be used when the caller would like to make available to the provider implementation an additional contextual class loader to consider, such as the loader of a caller on the stack. Typically, a provider implementation will attempt to resolve the named class using the given defaultLoader, if specified, before attempting to resolve the class from the codebase URL path.

This method delegates to the RMIClassLoaderSpi#loadClass(String, String, ClassLoader) method of the provider instance, passing codebase as the first argument, name as the second argument, and defaultLoader as the third argument.

Parameters
codebase:String

the list of URLs (separated by spaces) to load the class from, or null

name:String

the name of the class to load

defaultLoader:ClassLoader

additional contextual class loader to use, or null

Returns:Class<?>

the Class object representing the loaded class

Exceptions
MalformedURLException:
if codebase is non-null and contains an invalid URL, or if codebase is null and a provider-specific URL used to load classes is invalid
ClassNotFoundException:
if a definition for the class could not be found at the specified location
Since
1.4
loadProxyClassback to summary
public static Class<?> loadProxyClass(String codebase, String[] interfaces, ClassLoader defaultLoader) throws ClassNotFoundException, MalformedURLException

Loads a dynamic proxy class (see java.lang.reflect.Proxy) that implements a set of interfaces with the given names from a codebase URL path.

The interfaces will be resolved similar to classes loaded via the loadClass(String, String) method using the given codebase.

This method delegates to the RMIClassLoaderSpi#loadProxyClass(String, String[], ClassLoader) method of the provider instance, passing codebase as the first argument, interfaces as the second argument, and defaultLoader as the third argument.

Parameters
codebase:String

the list of URLs (space-separated) to load classes from, or null

interfaces:String[]

the names of the interfaces for the proxy class to implement

defaultLoader:ClassLoader

additional contextual class loader to use, or null

Returns:Class<?>

a dynamic proxy class that implements the named interfaces

Exceptions
ClassNotFoundException:
if a definition for one of the named interfaces could not be found at the specified location, or if creation of the dynamic proxy class failed (such as if java.lang.reflect.Proxy#getProxyClass(ClassLoader, Class[]) would throw an IllegalArgumentException for the given interface list)
MalformedURLException:
if codebase is non-null and contains an invalid URL, or if codebase is null and a provider-specific URL used to load classes is invalid
Since
1.4
newDefaultProviderInstanceback to summary
private static RMIClassLoaderSpi newDefaultProviderInstance()

Creates an instance of the default provider class.