Identify Host That Called Remote Method

Identify Host That Called Remote Method

When creating remote objects for use with Java’s Remote Method Invocation (RMI), you’ll often extend java.rmi.server.UnicastRemoteObject. When you do extend UnicastRemoteObject, it becomes easy for the remote methods to determine the host name of the caller. This can be useful for security or logging purposes, as it allows you to limit and/or record which hosts perform remote method calls. To identify the host that called a particular method, use the getClientHost() method. For example:

 public void myRemoteMethod() {try {String client = getClientHost();System.out.println("Called by " + client);}  //  trycatch (ServerNotActiveException snae) {		System.out.println("Server not active");}  //  catch (ServerNotActiveException snae)}  //  public void myRemoteMethod()

The getClientHost() method returns a String instance containing the name of the host which called the method. This allows you to easily determine where remote method calls originate from and to implement auditing or security which stores or processes that information.


Share the Post: