Stateless Session Bean

Java Primer
Incremental J2EE
by Vince Huston
Listing 6. Stateless Session Bean.


// Purpose.  SLSB - stateless 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;
   String    placeOrder( ArrayList list ) 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 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 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' );
      }
      System.out.println( "returning " + list.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
//
// returning 5 items to client
//
// Practical Java
// Java for Resume Inflation
// C++ for Deadend Careers
// returning 3 orders to client

/*************
D:\j2ee> jar tvf storeappl.ear
  2915 Wed Oct 04 14:14:46 CDT 2000 ejb-jar-ic.jar
     2 Wed Oct 04 14:14:46 CDT 2000 META-INF/MANIFEST.MF
   378 Wed Oct 04 14:14:46 CDT 2000 META-INF/application.xml
   313 Wed Oct 04 14:14:46 CDT 2000 META-INF/sun-j2ee-ri.xml

D:\j2ee> jar tvf storeapplclient.jar
  2922 Wed Oct 04 14:14:48 CDT 2000 ejb-jar-ic.jar
  2372 Wed Oct 04 14:14:44 CDT 2000 StoreImpl.class
   367 Wed Oct 04 14:14:44 CDT 2000 Item.class
   239 Wed Oct 04 14:14:44 CDT 2000 HIStore.class
   297 Wed Oct 04 14:14:44 CDT 2000 RIStore.class
     2 Wed Oct 04 14:14:48 CDT 2000 META-INF/MANIFEST.MF
   378 Wed Oct 04 14:14:48 CDT 2000 META-INF/application.xml
   313 Wed Oct 04 14:14:48 CDT 2000 META-INF/sun-j2ee-ri.xml
  3813 Wed Oct 04 14:14:48 CDT 2000 _HIStore_Stub.class
  4297 Wed Oct 04 14:14:48 CDT 2000 _RIStore_Stub.class
*************/



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 ) );
      ArrayList theChoices = new ArrayList();
      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;
            theChoices.add( new Integer( choice-1 ) );
         }

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

      } 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