Mediator Pattern: defines how communication between objects can be simplified by using a separate object to keep all objects from having to know about each other.

 

Clients do a Lookup service to get access to the remote interfaces defined in the

AuctionServer project. Mediator Pattern is used to perform this operation. Mediator

Pattern encapsulates  how a client interacts with the server. A Mediator class is  implemented for this project to do an RMI lookup. If any time in future we need the server from an RMI implementation to CORBA then nothing in the client changes except the Mediator class for CORBA lookup. The code for the Mediator is as below.

 

 

public class Mediator {

 

    public static void setConfig(String[] args) {

      HOST_NAME = "localhost";

      String portNum = "1099";

      if( args != null && args.length > 0 ) {

        HOST_NAME = args[0];

      }

      if( args != null && args.length > 1 ) {

        portNum = args[1];

      }

 

      PORT = Integer.parseInt(portNum);

    }

 

    public static AuctionServices lookupAuctionServices() {

        try {

            String registryURL = "rmi://" +HOST_NAME+ ":" +PORT+"/Auction";

            //find the remote object and cast it to an interface object

            AuctionServices service = (AuctionServices) Naming.lookup(registryURL);

            System.out.println("Lookup completed ");

            return service;

        } catch(Exception e) {

            System.out.println("RMI Auction Server lookup failed with error "+e.getMessage());

            System.exit(1);

        }

        return null;

    }

 

    private static String HOST_NAME;

    private static int PORT;

}