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

public abstract Class RMISocketFactory

extends Object
implements RMIClientSocketFactory, RMIServerSocketFactory
Class Inheritance
All Implemented Interfaces
java.rmi.server.RMIServerSocketFactory, java.rmi.server.RMIClientSocketFactory
Known Direct Subclasses
sun.rmi.transport.tcp.TCPDirectSocketFactory
Imports
java.io.*, java.net.*

An RMISocketFactory instance is used by the RMI runtime in order to obtain client and server sockets for RMI calls. An application may use the setSocketFactory method to request that the RMI runtime use its socket factory instance instead of the default implementation.

The default socket factory implementation creates a direct socket connection to the remote host.

The default socket factory implementation creates server sockets that are bound to the wildcard address, which accepts requests from all network interfaces.

Implementation Note

You can use the RMISocketFactory class to create a server socket that is bound to a specific address, restricting the origin of requests. For example, the following code implements a socket factory that binds server sockets to an IPv4 loopback address. This restricts RMI to processing requests only from the local host.

class LoopbackSocketFactory extends RMISocketFactory {
        public ServerSocket createServerSocket(int port) throws IOException {
            return new ServerSocket(port, 5, InetAddress.getByName("127.0.0.1"));
        }

        public Socket createSocket(String host, int port) throws IOException {
            // just call the default client socket factory
            return RMISocketFactory.getDefaultSocketFactory()
                                   .createSocket(host, port);
        }
    }

    // ...

    RMISocketFactory.setSocketFactory(new LoopbackSocketFactory());
Set the java.rmi.server.hostname system property to 127.0.0.1 to ensure that the generated stubs connect to the right network interface.
Authors
Ann Wollrath, Peter Jones
Since
1.1

Field Summary

Modifier and TypeField and Description
private static RMISocketFactory
defaultSocketFactory

default socket factory used by this RMI implementation

private static RMISocketFactory
factory

Client/server socket factory to be used by RMI runtime

private static RMIFailureHandler
handler

Handler for socket creation failure

Constructor Summary

AccessConstructor and Description
public
RMISocketFactory()

Constructs an RMISocketFactory.

Method Summary

Modifier and TypeMethod and Description
public abstract ServerSocket

Returns:

the server socket on the specified port
createServerSocket
(int
the port number
port
)

Redeclares java.rmi.server.RMIServerSocketFactory.createServerSocket.

Create a server socket on the specified port (port 0 indicates an anonymous port).

public abstract Socket

Returns:

a socket connected to the specified host and port.
createSocket
(String
the host name
host
,
int
the port number
port
)

Redeclares java.rmi.server.RMIClientSocketFactory.createSocket.

Creates a client socket connected to the specified host and port.

public static synchronized RMISocketFactory

Returns:

the default RMI socket factory
getDefaultSocketFactory
()

Returns a reference to the default socket factory used by this RMI implementation.

public static synchronized RMIFailureHandler

Returns:

the failure handler
getFailureHandler
()

Returns the handler for socket creation failure set by the setFailureHandler method.

public static synchronized RMISocketFactory

Returns:

the socket factory
getSocketFactory
()

Returns the socket factory set by the setSocketFactory method.

public static synchronized void
setFailureHandler(RMIFailureHandler
the failure handler.
fh
)

Sets the failure handler to be called by the RMI runtime if server socket creation fails.

public static synchronized void
setSocketFactory(RMISocketFactory
the socket factory
fac
)

Set the global socket factory from which RMI gets sockets (if the remote object is not associated with a specific client and/or server socket factory).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultSocketFactoryback to summary
private static RMISocketFactory defaultSocketFactory

default socket factory used by this RMI implementation

factoryback to summary
private static RMISocketFactory factory

Client/server socket factory to be used by RMI runtime

handlerback to summary
private static RMIFailureHandler handler

Handler for socket creation failure

Constructor Detail

RMISocketFactoryback to summary
public RMISocketFactory()

Constructs an RMISocketFactory.

Since
1.1

Method Detail

createServerSocketback to summary
public abstract ServerSocket createServerSocket(int port) throws IOException

Redeclares java.rmi.server.RMIServerSocketFactory.createServerSocket.

Create a server socket on the specified port (port 0 indicates an anonymous port).

Parameters
port:int

the port number

Returns:ServerSocket

the server socket on the specified port

Exceptions
IOException:
if an I/O error occurs during server socket creation
Since
1.1
createSocketback to summary
public abstract Socket createSocket(String host, int port) throws IOException

Redeclares java.rmi.server.RMIClientSocketFactory.createSocket.

Creates a client socket connected to the specified host and port.

Parameters
host:String

the host name

port:int

the port number

Returns:Socket

a socket connected to the specified host and port.

Exceptions
IOException:
if an I/O error occurs during socket creation
Since
1.1
getDefaultSocketFactoryback to summary
public static synchronized RMISocketFactory getDefaultSocketFactory()

Returns a reference to the default socket factory used by this RMI implementation. This will be the factory used by the RMI runtime when getSocketFactory returns null.

Returns:RMISocketFactory

the default RMI socket factory

Since
1.1
getFailureHandlerback to summary
public static synchronized RMIFailureHandler getFailureHandler()

Returns the handler for socket creation failure set by the setFailureHandler method.

Returns:RMIFailureHandler

the failure handler

Since
1.1
See Also
setFailureHandler(RMIFailureHandler)
getSocketFactoryback to summary
public static synchronized RMISocketFactory getSocketFactory()

Returns the socket factory set by the setSocketFactory method. Returns null if no socket factory has been set.

Returns:RMISocketFactory

the socket factory

Since
1.1
See Also
setSocketFactory(RMISocketFactory)
setFailureHandlerback to summary
public static synchronized void setFailureHandler(RMIFailureHandler fh)

Sets the failure handler to be called by the RMI runtime if server socket creation fails. By default, if no failure handler is installed and server socket creation fails, the RMI runtime does attempt to recreate the server socket.

Parameters
fh:RMIFailureHandler

the failure handler.

Since
1.1
See Also
getFailureHandler, java.rmi.server.RMIFailureHandler#failure(Exception)
setSocketFactoryback to summary
public static synchronized void setSocketFactory(RMISocketFactory fac) throws IOException

Set the global socket factory from which RMI gets sockets (if the remote object is not associated with a specific client and/or server socket factory). The RMI socket factory can only be set once.

Parameters
fac:RMISocketFactory

the socket factory

Exceptions
IOException:
if the RMI socket factory is already set
Since
1.1
See Also
getSocketFactory