`
kylinsoong
  • 浏览: 236134 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JDK Source Code & java.rmi.server.RMISocketFactory

阅读更多

Today's Source code analysing series I will dive into java.rmi.server.RMISocketFactory, First the UML Diagram:


 This Diagram containing 1 abstract Class: RMISocketFactory, and 3 Interface: RMIClientSocketFactory, RMIServerSocketFactory, RMIFailureHandler.

 

RMIServerSocketFactory only has one method createServerSocket(int port), through this method we can create a server socket on the specified port. RMIServerSocketFactory instance is used by the RMI runtime in order to obtain server sockets for RMI calls. Usually RMIServerSocketFactory associated with a remote object is used to obtain the ServerSocket which accept incoming calls from clients. The Sorce Code:

import java.io.*;
import java.net.*;

public interface RMIServerSocketFactory {

    public ServerSocket createServerSocket(int port)throws IOException;
}

 

 

RMIClientSocketFactory also has one method createSocket(String host, int port), through this method we can create a client socket connected to the specified host and port. Like the RMIServerSocketFactory, RMIClientSocketFactory instance is also used by the RMI runtime in order to obtain client sockets for RMI calls. The Socket RMIClientSocketFactory create can establish a connection with the ServerSoket, This connection use to transmit info between Client and Server. Also the Source Code:

 

import java.io.*;
import java.net.*;

public interface RMIClientSocketFactory {

    public Socket createSocket(String host, int port)throws IOException;
}

 

RMIFailureHandler defined a method failure(Exception ex), While the RMI Runtime is unable to create a ServerSocket via the RMISocketFactory, the failure callback will be invoked. There are no any implemetaion in JDK, if you wanna use this handler mechenism you must impementing yourseves handler. If no failure handler is installed, the default behavior is to attempt to re-create the ServerSocket. the handler can be installed through RMISocketFacotry.setFailureHandler, we will see later. The failure method returns a boolean indicating whether the runtime should attempt to re-create the ServerSocket. The Source Code:

 

public interface RMIFailureHandler {

    public boolean failure(Exception ex);
}

 

RMISocketFactory is an abstract Class, has 3 private stataic atrributes, implementing both RMIClientSocketFactory and RMIClientSocketFactory interface,  RMISocketFactory make this 2 interfaces method abstract, the user implement by themself. 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 Source Code:

import java.io.*;
import java.net.*;

public abstract class RMISocketFactory implements RMIClientSocketFactory, RMIServerSocketFactory{

    private static RMISocketFactory factory = null;
    private static RMISocketFactory defaultSocketFactory;
    private static RMIFailureHandler handler = null;

    public RMISocketFactory() {
		super();
    }
    
    public abstract Socket createSocket(String host, int port) throws IOException;

    public abstract ServerSocket createServerSocket(int port) throws IOException;

    public synchronized static void setSocketFactory(RMISocketFactory fac) throws IOException {
    	if (factory != null) {
			throw new SocketException("factory already defined");
		}
		SecurityManager security = System.getSecurityManager();
		if (security != null) {
			security.checkSetFactory();
		}
		factory = fac;
    }

    public synchronized static RMISocketFactory getSocketFactory() {
		return factory;
    }

    public synchronized static RMISocketFactory getDefaultSocketFactory() {
		if (defaultSocketFactory == null) {
			defaultSocketFactory = new sun.rmi.transport.proxy.RMIMasterSocketFactory();
		}
		return defaultSocketFactory;
    }

    public synchronized static void setFailureHandler(RMIFailureHandler fh) {
		SecurityManager security = System.getSecurityManager();
		if (security != null) {
			security.checkSetFactory();
		}
		handler = fh;
    }

    public synchronized static RMIFailureHandler getFailureHandler() {
		return handler;
    }
}

  

Inspiration From this Code:

--will be put later

 

  

  • 大小: 19.3 KB
0
0
分享到:
评论
1 楼 di1984HIT 2013-11-22  
鞋的不错。

相关推荐

Global site tag (gtag.js) - Google Analytics