RMI code example                          Peter Komisar
                                                                                                                                                        revised Feb 28/2000


 /* RMI code example                                                            Peter Komisar */

//     RMI remote interface

public interface InCoExchange extends java.rmi.Remote{

   public double lowest_asking( ) throws java.rmi.RemoteException;
   public double highest_bid( ) throws java.rmi.RemoteException;
   }

//  Note the characteristic extension of the Remote interface and
//  the declaration of the abstract methods throwing RemoteException


//    RMI client

import java.rmi.*;

public class ExCheckClient{
public static void main(String[] args){
  try{
     Remote remote_object=Naming.lookup("//localhost/InCoServer");
     InCoExchange exchange=(InCoExchange)remote_object;

//  Here is the static lookup call on the RMI server which is returned to the Remote
//  interface type. Remote then is cast to the specific interface desired, InCoExchange

     double lowest_asking=exchange.lowest_asking( );
     double highest_bid=exchange.highest_bid( );
     System.out.println("The lowest asking price for in-company stock is "
     + "$" + lowest_asking);
     System.out.println("The highest bid for in-company stock is "
     + "$" + highest_bid);
     }
     catch(Exception e){
     System.out.println
     ("Exception: looking up and accessing the remote");
     }
   }
 }



//    RMI Server, the remote object implementation

/*  Observe the RMI server's extension of  UnicastRemoteObject and the implementation
     of the remote interface subclass. The constructor appealing to super is required as
     the no-arg constructor is undefined  in the parent because of other constructor forms  */

import java.rmi.*;                                                                              // RMI imports
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RMI.SecurityManager;

public class InCoServer extends UnicastRemoteObject implements InCoExchange{
 public InCoServer() throws java.rmi.RemoteException {
   super( );
   }

// Implementations of the methods of the interface InCoExchange

/*1*/

public double lowest_asking() throws java.rmi.RemoteException{
 // getLowestAsking() returns 9.50 from somewhere and is assigned to asking
    double asking;
    asking=9.50;
    return asking;
    }

/*2*/

public double highest_bid() throws java.rmi.RemoteException{
   // getHighestBid() returns 7.50 from somewhere and is assigned to bid
      double bid;
      bid=7.50;
      return bid;
      }

/* Within the auspices of main( ), the server object, (a.k.a. the remote object by virtue of  implementing
     the remote interface subclass), is instantiated and bound via a static call to the Naming class
     method  rebind(), announcing it's service is available in the rmi Registry (name service)               */

public static void main(String[] args){
// System.setSecurityManager(new RMISecurityManager( ));
//                     add back Security after running default without
    try{
       InCoServer inco=new InCoServer();
       Naming.rebind("InCoServer",inco);
       }
       catch(Exception e){
       System.out.println
       ("Exception: instantiating & binding the remote");
       }
    }
 }