Stateful Session Bean

Java Primer
Incremental J2EE
by Vince Huston
Listing 7. Stateful Session Bean.


// Purpose.  SFSB - stateful Session Bean

import java.util.*;
import java.rmi.RemoteException;
import javax.ejb.*;

class Item implements java.io.Serializable {
   private String name;
   public Item( String na ) { name = na; }
   public String toString() { return name; }
}

interface RIStore extends EJBObject {
   ArrayList getItems()            throws RemoteException;
   void      selectItem( int num ) throws RemoteException;
   String    placeOrder()          throws RemoteException;
}

interface HIStore extends EJBHome {
   public RIStore create() throws java.rmi.RemoteException, CreateException;
}

public class StoreImpl implements SessionBean {
   private ArrayList      itemList   = new ArrayList();
   private ArrayList      theChoices = new ArrayList();
   private SessionContext context;

   public void ejbCreate() {
      System.out.println( "ejbCreate:" );
      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() {
      System.out.println( "\nreturning " + itemList.size() + " items to client" );
      return itemList;
   }
   public void selectItem( int num ) {
      theChoices.add( new Integer( num ) );
   }
   public String placeOrder() {
      StringBuffer sb = new StringBuffer();
      Item  ele;
      System.out.println();
      sb.append( "The following items were ordered:\n" );
      for (Iterator it = theChoices.iterator(); it.hasNext(); ) {
         ele = (Item) itemList.get( ((Integer)it.next()).intValue() );
         System.out.println( ele );
         sb.append( "   " );
         sb.append( ele );
         sb.append( '\n' );
      }
      System.out.println( "returning " + theChoices.size() + " orders to client" );
      return sb.toString();
   }

   // from javax.ejb.SessionBean interface
   public void ejbRemove()    { System.out.println( "ejbRemove:" ); }
   public void ejbActivate()  { System.out.println( "ejbActivate:" ); }
   public void ejbPassivate() { System.out.println( "ejbPassivate:" ); }
   public void setSessionContext( SessionContext ctx ) {
      System.out.println( "setSessionContext:" );
      context = ctx;
}  }

// setSessionContext:
// ejbCreate:
//
// returning 5 items to client
//
// Design Patterns by GOF
// Using UML and Patterns
// Design Patterns by GOF
// returning 3 orders to client
// setSessionContext:               EXTRA
// ejbCreate:                       EXTRA
//
// returning 5 items to client
//
// Practical Java
// Java for Resume Inflation
// C++ for Deadend Careers
// returning 3 orders to client



import java.util.*;
import java.io.*;
import javax.rmi.*;
import javax.naming.*;
import javax.ejb.CreateException;

public class StoreClient {
   public static void main( String[] args ) {
      BufferedReader in = new BufferedReader(
                             new InputStreamReader( System.in ) );
      int choice;
      try {
         InitialContext ic = new InitialContext();
         Object    obj       = ic.lookup( "StoreHome" );
         HIStore   storeHome = (HIStore) PortableRemoteObject.narrow( obj, HIStore.class );
         RIStore   theStore  = storeHome.create();
         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;
            theStore.selectItem( choice-1 );
         }

         System.out.println( theStore.placeOrder() );

      } catch (NamingException ex ) { ex.printStackTrace();
      } catch (CreateException ex ) { ex.printStackTrace();
      } catch (IOException ex )     { ex.printStackTrace();
}  }  }

// D:> java StoreClient
// 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: 2
// Choice: 1
// Choice: 0
// The following items were ordered:
//    Design Patterns by GOF
//    Using UML and Patterns
//    Design Patterns by GOF

// D:> java StoreClient
// 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: 5
// Choice: 4
// Choice: 3
// Choice: 0
// The following items were ordered:
//    Practical Java
//    Java for Resume Inflation
//    C++ for Deadend Careers