// Purpose. JNDI import java.util.*; import java.rmi.*; // Remote, RemoteException import javax.rmi.*; // PortableRemoteObject import javax.naming.*; // InitialContext 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 PortableRemoteObject 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 { InitialContext ic = new InitialContext(); ic.rebind( "StoreServer", new StoreImpl() ); System.out.println( "StoreServer bound in JNDI" ); } catch (NamingException ex ) { ex.printStackTrace(); } catch (RemoteException ex ) { ex.printStackTrace(); } } } // Design Patterns by GOF // C++ for Deadend Careers // Practical Java import java.util.*; import java.io.*; import javax.rmi.*; // PortableRemoteObject import javax.naming.*; // InitialContext public class StoreClient { public static void main( String[] args ) { BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); ArrayList theChoices = new ArrayList(); int choice; try { InitialContext ic = new InitialContext(); Object obj = ic.lookup( "StoreServer" ); RIStore theStore = (RIStore) PortableRemoteObject.narrow( obj, RIStore.class ); 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 (NamingException 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