Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click NOW to join Planet IT!
Click NOW to join Planet IT!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


CarSales.java

CarSales.java is the client class. CarSales is a stand-alone application that looks up CarFactories in the RMI server to get references to these remote objects.

The application tries to obtain references to three remote CarFactories. Once it obtains the references, actions such as selecting cars, ordering cars, selling cars, and destroying cars may be performed. This RMI client only refers to the CarFactory and Car interfaces, not their actual implementation, which is the main objective of this exercise. The implementation class instances run elsewhere on the network and are seamlessly accessed through RMI. Listing 10-27 shows the source code for CarSales.java.

Listing 10-27: CarSales.java.

//
// CarSales.java
//
// (C) 1996 Wim De Munck mailto: wimdm@dm-mediaware.be
//
// This code is not to be distributed without
// explicit confirmation by the auhor.
//
import java.awt.*;
import java.rmi.Naming;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.net.UnknownHostException;
/**
* CarSales-class connecting to a Remote Object
* registry.
* The Carsales instance allows simple order and selling
* of cars.
*/
public class CarSales extends Frame {
  TextArea ta = new TextArea("Application started");
  Button quitB = new Button ( "Quit" ), orderB = new Button (   "Order Car" );
  Button sellB = new Button ( "Sell" );
  Button wreckB = new Button ( "Wreck" );
  Choice brandCH = new Choice (), allCarsCH = new Choice();
  TextField modelTF = new TextField(10), colorTF = new TextField(10);
  TextField buyerTF = new TextField(10);
  CarFactory carFactory[];
  Car cars[] = null;
  /**
  * Construct the CarSales GUI
  */
  public CarSales ( ) {
      super ("BEST CARS IN THE WORLD");
      // Setup the User Interface
      Panel northPan = new Panel ( );
      northPan.setLayout ( new BorderLayout () );
      northPan.add ( "North",
      new Label ("Car Ordering: Select brand and model", Label.LEFT) );
      northPan.setLayout ( new BorderLayout() );
      Panel orderPanel = new Panel ();
      //orderPanel.setLayout ( new FlowLayout ( FlowLayout.LEFT ) );
      orderPanel.setLayout ( new GridLayout ( 1,7) );
      orderPanel.add ( new Label ("Brand:", Label.RIGHT ) );
      orderPanel.add ( brandCH );
      orderPanel.add ( new Label ("Model:", Label.RIGHT ) );
      orderPanel.add ( modelTF );
      orderPanel.add ( new Label ("Color:", Label.RIGHT ) );
      orderPanel.add ( colorTF );
      orderPanel.add ( orderB );
      Panel sellPanel = new Panel ();
      //sellPanel.setLayout ( new FlowLayout ( FlowLayout.LEFT ) );
      sellPanel.setLayout ( new GridLayout ( 1,7) );
      sellPanel.add ( new Label ("All Cars:", Label.RIGHT ) );
      sellPanel.add ( allCarsCH );
      sellPanel.add ( new Label ("Buyer:", Label.RIGHT ) );
      sellPanel.add ( buyerTF );
      sellPanel.add ( new Label ( "" ) );
      sellPanel.add ( new Label( "Actions:", Label.RIGHT ) );
      Panel buttonPan = new Panel ( );
      buttonPan.setLayout ( new GridLayout ( 1, 2) );
      buttonPan.add ( sellB );
      buttonPan.add ( wreckB );
      sellPanel.add ( buttonPan );
      Panel northCenterPanel = new Panel ();
      northCenterPanel.setLayout ( new GridLayout ( 2, 1 ) );
      northCenterPanel.add ( orderPanel );
      northCenterPanel.add ( sellPanel );
      northPan.add ( "Center", northCenterPanel );
      northPan.add ( "South", new Label ( "RMI/SQL log:", Label.CENTER ) );
      add("North", northPan );
      add("Center", ta);
      add("South", quitB );
      // In a real application there would be a remote object
      // giving us an array or enumeration with all brands.
      // In this example we do it hard coded.
      String carBrands[] = { "audi", "bmw", "vw" };
      carFactory = new CarFactory [ carBrands.length ];
      for (int ndx=0; ndx< carBrands.length; ndx ++ ) {
           try {
                    brandCH.addItem ( carBrands[ndx] );
                    carFactory[ndx] = (CarFactory)
                    (java.rmi.Naming.lookup ( "rmi://serverhost/CarFactory." +
                    carBrands[ndx] ) );
                    appendTA( carBrands[ndx] + ": " + carFactory[ndx]);}
           catch ( java.rmi.RemoteException ex) {
           appendTA( "Constructor() RemoteException: " + ex.getMessage() );}
           catch ( java.rmi.NotBoundException ex) {
                    appendTA( "Constructor() NotBoundException: " +
                    ex.getMessage() );}
           catch ( java.net.MalformedURLException ex) {
                    appendTA ( "Constructor() MalformedURLException: "+
                     ex.getMessage() );
           }
      } // end for carBrands[ndx]
      try {
           appendTA("");
           appendTA("Creating Cars:");
           updateAllCarsChoice ( ) ;
           //Car nr1 = audiFact.createCar("A6", "black");}
      catch ( Exception ex) {
           appendTA( "Constructor() Exception: " + ex.getMessage() );
           ex.printStackTrace();
           }
      pack();
      setVisible ( true );   // JDK1.1 replaced deprecated show()
  }
  private void updateAllCarsChoice ( ) {
  int ndx = brandCH.getSelectedIndex();
  int count = 0;
  allCarsCH.removeAll();
  try {
      cars = carFactory[ndx].getAll();
      for ( int i=0 ; i < cars.length; i++ ) {
      allCarsCH.addItem ( "" + cars[i].getSerialNr() + " " +
      cars[i].getModel() + "," + cars[i].getColor() );
      count++;
      }
  }
  catch ( Exception ex ) {
      appendTA ( "updateAllCarChoice() Exception: " + ex.getMessage() );
  }
  if ( count > 0 ) {
      allCarsCH.setEnabled ( true );
      allCarsCH.select( 0 );
      try {
           buyerTF.setText ( cars[0].getOwner() );
           colorTF.setText ( cars[0].getColor() );
           modelTF.setText ( cars[0].getModel() );
           }
      catch ( Exception ex ) {
           appendTA ( "updateAllCarChoice() Exception: " + ex.getMessage() );
      }
  }
  else {
      allCarsCH.setEnabled ( false );
      allCarsCH.addItem ( "No Cars Available" );
      }
  appendTA ( "Added " + count + " cars to allCarsCH for " + brandCH.getSelectedItem() );
  }
private void appendTA ( String text ) {
  ta.setText( ta.getText() + "\n" + text.toString() );
  }
private void appendTA ( Car car ) {
  try {
      ta.setText( ta.getText() + "\n" + car.getObjectString() ); }
catch ( java.rmi.RemoteException ex ) {
      ta.setText ( "RemoteException: " + ex.getMessage() );
      ex.printStackTrace();
      }
  }
  // JDK1.02 event-model
  public boolean action ( Event evt, Object arg ) {
      try {
           if ( evt.target == quitB ) {
                    setVisible ( false ); // JDK1.1's setVisible() replaced deprecated
                    hide()
                    dispose();
                    System.exit(0);
                    return true;
                    }
           else if ( evt.target == orderB ) {
                    int factIndex = brandCH.getSelectedIndex();
                    carFactory[factIndex].createCar ( modelTF.getText(),
                      colorTF.getText() );
                    updateAllCarsChoice ( ) ;
                    }
           else if ( evt.target == sellB ) {
                    int carIndex = allCarsCH.getSelectedIndex();
                    cars[carIndex].sellTo ( buyerTF.getText() );
                    appendTA ( "sold car: " + cars[carIndex].getSerialNr() + " to " +
                      buyerTF.getText() );
                    }
           else if ( evt.target == wreckB ) {
                    int carIndex = allCarsCH.getSelectedIndex();
                    int factIndex = brandCH.getSelectedIndex();
                    appendTA ( "wrecked car: " + cars[carIndex].getSerialNr() );
                    carFactory[factIndex].deleteCar ( cars[carIndex] );
                    updateAllCarsChoice ( ) ;
                    }
           else if ( evt.target == brandCH ) {
                    colorTF.setText ( "" );
                    modelTF.setText ( "" );
                    updateAllCarsChoice ( ) ;
                    }
           else if ( evt.target == allCarsCH ) {
                    int carIndex = allCarsCH.getSelectedIndex();
                    colorTF.setText ( cars[carIndex].getColor() );
                    modelTF.setText ( cars[carIndex].getModel() );
                    buyerTF.setText ( cars[carIndex].getOwner() );
                    }
           }
      catch ( java.rmi.RemoteException ex) {
           appendTA ( "action() RemoteException: " + ex.getMessage() ); }
      catch ( java.sql.SQLException ex) {
           appendTA ( "action() SQLException: " + ex.getMessage() );
           }
      return true;
      }
  public boolean handleEvent ( Event evt ) { // deprecated by processEvent( )
  if ( evt.id == Event.WINDOW_DESTROY ) {
  setVisible ( false); // JDK1.1's setVisible() replaced deprecated hide( )
  dispose();
  System.exit(0);
  return true;
  }
else {
  return super.handleEvent ( evt );
  }
}

public static void main ( String args[] ) {
// Create GUI-object
CarSales cs = new CarSales();
  }
}


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.