Listing 9: A Java CORBA client

//
// File: client.java
//
import org.omg.CORBA.*;
import java.io.*;
import Fortune.*;
import Fortune.FactoryPackage.*;

//
// Class: client
//
public class client
{
   public static void 
   main(String args[])
   {
      try {
         // Init ORB
         ORB orb = ORB.init(args, new java.util.Properties());

         // Obtain externalized Factory object reference
     String ref = null;
     String refFile = "factory.ior";
         BufferedReader ior = 
           new BufferedReader(
             new InputStreamReader(
               new FileInputStream(
                 refFile)));
     ref = ior.readLine();
     ior.close();
     org.omg.CORBA.Object obj = orb.string_to_object(ref);
     if(obj == null) 
           throw new RuntimeException();

         // Narrow object reference
     Factory f = FactoryHelper.narrow(obj);
     if(f == null) 
           throw new RuntimeException();
      
         short month = 0;
         short done = 0;
         System.out.println("Enter month value (1 to 12" 
           + ", 0 to quit)"); 

         // Wrap standard input
         BufferedReader in = 
           new BufferedReader( 
             new InputStreamReader(
               System.in));

         // Loop
         while (done == 0) {
            System.out.print("Month: ");
            month = Short.parseShort(in.readLine().trim());  
            if(month != 0) {
              // Get fortune server
              Teller t = f.getFortune(month);
              if(t == null) 
                throw new RuntimeException();
             
              // Display results
              System.out.println("Fortune for " + t.getMessage() );
              System.out.println("Lucky # is: " 
                + t.getLuckyNumber() );
            }
            else { done = 1; }
         }
      }
      // Check for error conditions
      catch(outOfBounds ex) {
         System.err.println("Month was out of bounds");
      }
      catch(SystemException ex) {
         System.err.println("Unexpected error '" + 
           ex.getMessage() + "'");
      }
      catch (NumberFormatException ex) 
      {
        System.out.println("Invalid Number: " + ex);
      }
      catch(IOException ex) {
         System.err.println("Can't read from ´" + 
           ex.getMessage() + "'");
      }
   }
}
//End of File