RMI – Client and run the Library

This is the last part in the build up to the RMI (Remote Method Invocation) this is the clients part, the previous part was the RMI library / server. Where the server will listen for a clients request.

So to start with we need to be able to call the remote server, so we need to be able to have permission from the RMI java security package.

System.setSecurityManager(new RMISecurityManager());

next we need to build up the RMI server request URL (where the server is and also the remote class that it is listening on and as taken from here we are listening on the “RemoteBook” class) (we are listening on the localhost as the server)

String remoteClass = "RemoteBook";
String rmiURL = "rmi://localhost/" + remoteClass;

since all the client needs to know is what interface has been implemented within the server (the borrowable interface) then we just need to create a new object based on that that then links to the servers listening class rmiURL (as taken from above)

Borrowable remoteBook = (Borrowable)Naming.lookup(rmiURL);

and then just call the functions that will have been implemented on the server (the server has used the Book.java which implements the Borrowable interface) because now they are linked.

The java Naming object is a RMI object that allows for the server to bind’s its class object to a URL and the client to link its interface object to the servers class object basically.

here is the client code in full

import java.util.*;
import java.rmi.*;
 
public class RMIClient {
	String remoteClass = "RemoteBook";
	String libraryCardNumber = "genuxCard";
 
	public RMIClient()
	{
		if (System.getSecurityManager()== null)
			System.setSecurityManager(new RMISecurityManager());
 
	}
 
	public boolean useLibrary()
	{
		boolean result = true;
		try {
			String rmiURL = "rmi://localhost/" + remoteClass;
			Borrowable remoteBook = (Borrowable)Naming.lookup(rmiURL);
			System.out.println("The book is checked out "+remoteBook.isCheckedOut());
 
			boolean outResult = remoteBook.checkOut(libraryCardNumber,new Date());
 
			if (outResult==true)
				System.out.println("Book checked out successfully");
			else
				result = false;
 
			if (remoteBook.isCheckedOut())
			{
				System.out.println("libraray Card number that has checked out the book is :"+remoteBook.checkedOutCardNumber());
			}
 
			boolean inResult = remoteBook.checkIn(libraryCardNumber, new Date());
 
			if (inResult==true)
				System.out.println("Book checked in");
			else
				result = false;
		}
		catch (Exception e)
		{
			System.out.println("RMI Error: "+e.getMessage());
			result = false;
		}
		return result;
	}
 
	public static void main(String[] args) {
		RMIClient RMIC = new RMIClient();
		boolean result = RMIC.useLibrary();
		if (result == false)
			System.out.println("Error using library");
 
	}
 
}

and if you save that as RMIClient.java, compile up the java files to create the class files

This is the policy file, if you save this as RMIpolicyfile.policy, it basically allows all permissions within the java security object, not the best security for servers but this will server as a local host test. (alter the where/your/class/files/are to where you are building up the java files into there class files, within eclipse it has a /src directory and a /bin so you would link to the /bin directory)

grant codeBase "file:/where/your/class/files/are" {
    permission java.security.AllPermission;
};

and now that you have the RMI server (RMIFactory) and the RMI client (RMIClient) all we need to do is to register the java RMI to allow connections on the linux command line I do

rmiregistry &

the “&” allows it to run in the back ground, then to run the server

java -Djava.security.policy=RMIpolicyfile.policy RMIFactory &
console output : Remote book object has been started

which this uses the security policy file from above and also runs again in the background (“&”) and now to just run the client

java -Djava.security.policy=RMIpolicyfile.policy RMIClient

once again since the client is requesting the RMI security, you need to tell it to use the RMI policy file from above, and the output would be

The book is checked out false
Book checked out successfully
libraray Card number that has checked out the book is :genuxCard Date taken out on Thu Apr 08 11:49:36 BST 2010
Book checked in