// Purpose. RMI import java.util.*; import java.rmi.*; // Remote, RemoteException import java.rmi.server.*; // UnicastRemoteObject import java.net.MalformedURLException; class Item implements java.io.Serializable { private String name; public Item( String na ) { name = na; } public String toString() { return name; } } interface RIStore extends Remote { ArrayList getItems() throws RemoteException; String placeOrder( ArrayList list ) throws RemoteException; } class StoreImpl extends UnicastRemoteObject implements RIStore { private ArrayList itemList = new ArrayList(); public StoreImpl() throws RemoteException { itemList.add( new Item( "Design Patterns by GOF" ) ); itemList.add( new Item( "Using UML and Patterns" ) ); itemList.add( new Item( "C++ for Deadend Careers" ) ); itemList.add( new Item( "Java for Resume Inflation" ) ); itemList.add( new Item( "Practical Java" ) ); } public ArrayList getItems() { return itemList; } public String placeOrder( ArrayList list ) { StringBuffer sb = new StringBuffer(); Item ele; System.out.println(); sb.append( "The following items were ordered:\n" ); for (Iterator it = list.iterator(); it.hasNext(); ) { ele = (Item) itemList.get( ((Integer)it.next()).intValue() ); System.out.println( ele ); sb.append( " " ); sb.append( ele ); sb.append( '\n' ); } return sb.toString(); } public static void main( String[] args ) { try { Naming.rebind( "rmi://127.0.0.1/StoreServer", new StoreImpl() ); System.out.println( "StoreServer bound in rmiregistry" ); } catch (MalformedURLException ex ) { ex.printStackTrace(); } catch (RemoteException ex ) { ex.printStackTrace(); } } } import java.util.*; import java.io.*; import java.rmi.*; import java.net.MalformedURLException; public class StoreClient { public static void main( String[] args ) { BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); ArrayList theChoices = new ArrayList(); int choice; try { RIStore theStore = (RIStore) Naming.lookup( "rmi://localhost/StoreServer" ); ArrayList theItems = theStore.getItems(); System.out.println( "Select from (0 when done):" ); Iterator it = theItems.iterator(); for (int i=1; it.hasNext(); i++) System.out.println( " " + i + " - " + it.next() ); while (true) { System.out.print( "Choice: " ); if ((choice = Integer.parseInt( in.readLine() )) == 0) break; theChoices.add( new Integer( choice-1 ) ); } System.out.println( theStore.placeOrder( theChoices ) ); } catch (NotBoundException ex ) { ex.printStackTrace(); } catch (RemoteException ex ) { ex.printStackTrace(); } catch (IOException ex ) { ex.printStackTrace(); } } } // Select from (0 when done): // 1 - Design Patterns by GOF // 2 - Using UML and Patterns // 3 - C++ for Deadend Careers // 4 - Java for Resume Inflation // 5 - Practical Java // Choice: 1 // Choice: 3 // Choice: 5 // Choice: 0 // The following items were ordered: // Design Patterns by GOF // C++ for Deadend Careers // Practical Java