CONTENTS | PREV | NEXT | Java Remote Method Invocation |
The java.rmi.server.RMISocketFactory abstract class provides an interface for specifying how the transport should obtain sockets. Note that the class below uses Socket and ServerSocket from the java.net package.package java.rmi.server;The static method setSocketFactory is used to set the socket factory from which RMI obtains sockets. The application may invoke this method with its own RMISocketFactory instance only once. An application-defined implementation of RMISocketFactory could, for example, do preliminary filtering on the requested connection and throw exceptions, or return its own extension of the java.net.Socket or java.net.ServerSocket classes, such as ones that provide a secure communication channel. Note that the RMISocketFactory may only be set if the current security manager allows setting a socket factory; if setting the socket factory is disallowed, a SecurityException will be thrown.
public abstract class RMISocketFactory {
public abstract Socket createSocket(String host, int port)
throws IOException;
public Socket createSocket(String host, int port,
SocketType type)
throws IOException;
public abstract ServerSocket createServerSocket(int port)
throws IOException;
public ServerSocket createServerSocket(int port,
SocketType type)
throws IOException;
public static void setSocketFactory(RMISocketFactory fac)
throws IOException;
public static RMISocketFactory getSocketFactory();
public static void setFailureHandler(RMIFailureHandler fh);
public static RMIFailureHandler getFailureHandler();
}The static method getSocketFactory returns the socket factory used by RMI. The method returns null if the socket factory is not set.
The transport layer invokes the createSocket and createServerSocket methods on the RMISocketFactory returned by the
getSocketFactory
method when the transport needs to create sockets. For example:RMISocketFactory.getSocketFactory().createSocket(myhost, myport)The method createSocket should create a client socket connected to the specified host and port. The method createServerSocket should create a server socket on the specified port. In JDK1.2, an additionalcreateSocket
method andcreateServerSocket
method, both taking the additional parameter type, has been added to RMISocketFactory. These methods create a Socket or ServerSocket, respectively, for the specified SocketType, type.The default transport's implementation of RMISocketFactory provides for transparent RMI through firewalls using HTTP as follows:
- On
createSocket
, the factory automatically attempts HTTP connections to hosts that cannot be contacted with a direct socket.- On
createServerSocket
, the factory returns a server socket that automatically detects if a newly accepted connection is an HTTP POST request. If so, it returns a socket that will transparently expose only the body of the request to the transport and format its output as an HTTP response.
The methodsetFailureHandler
sets the failure handler to be called by the RMI runtime if the creation of a server socket fails. The failure handler returns a boolean to indicate if retry should occur. The default failure handler returns false, meaning that by default recreation of sockets is not attempted by the runtime.The method
getFailureHandler
returns the current handler for socket creation failure, or null if the failure handler is not set.