// WordBean is a CMP EntityBean // WordSummary is a Summary Object // PrimaryKey is of type String // WordBean.getSummary() returns a WordSummary object // transactions were turned on for increment() only (Load-Store output) // WordCountBean is a stateless SessionBean // WordCountBean.ejbCreate() looks up an HIWord factory // WordCountBean.parse() first tries HIWord.findByPrimaryKey(String) // if that succeeds, it calls RIWord.increment() // if it throws a FinderException, it calls HIWord.create(String) // WordCountBean.getTotals() uses HIWord.findAllWords() and asks for // a WordSummary object that formats itself with toString() // WordCountClient // looks up an HIWordCount factory // uses an RIWordCount proxy package demos; import java.util.Collection; import java.rmi.RemoteException; import javax.ejb.*; class WordSummary implements java.io.Serializable { private String word; private int count; public WordSummary( String w, int c ) { word = w; count = c; } public String toString() { char ch = (word.length() < 5) ? '\t' : 0; return word + ch + '\t' + count; } } interface RIWord extends EJBObject { public void increment() throws RemoteException; public WordSummary getSummary() throws RemoteException; } interface HIWord extends EJBHome { RIWord create( String w ) throws RemoteException, CreateException; RIWord findByPrimaryKey( String pk ) throws RemoteException, FinderException; Collection findAllWords() throws RemoteException, FinderException; } public class WordBean implements EntityBean { transient private EntityContext context; public String word; public int count; // corresponds to HIWord.create( String ) public String ejbCreate( String w ) { word = w; count = 1; System.out.println( "WordBean.ejbCreate - " + word ); return null; } public void ejbPostCreate( String w ) { System.out.println( "WordBean.ejbPostCreate - " + word ); } // "business" interface from RIWord public void increment() { count++; } public WordSummary getSummary() { return new WordSummary( word, count ); } // "technical" interface from SessionBean and EntityBean public void ejbRemove() { System.out.println( "WordBean.ejbRemove - " + word ); } public void ejbActivate() { word = (String) context.getPrimaryKey(); System.out.println( "WordBean.ejbActivate - " + word ); } public void ejbPassivate() { word = null; System.out.println( "WordBean.ejbPassivate - " + word ); } public void setEntityContext( EntityContext ctx ) { System.out.println( "WordBean.setEntityContext: " + word ); context = ctx; } // "technical" interface from EntityBean public void ejbLoad() { System.out.print( "WordBean.ejbLoad - " + word ); } public void ejbStore() { System.out.println( " WordBean.ejbStore - " + word ); } public void unsetEntityContext() { System.out.println( "WordBean.unsetEntityContext - " + word ); context = null; } } package demos; import java.util.*; import java.rmi.RemoteException; import javax.rmi.PortableRemoteObject; import javax.ejb.*; import javax.naming.*; interface RIWordCount extends EJBObject { void parse( String in ) throws RemoteException; String getTotals() throws RemoteException; } interface HIWordCount extends EJBHome { RIWordCount create() throws CreateException, RemoteException; } public class WordCountBean implements SessionBean { private SessionContext context; private HIWord wordHome; // corresponds to HIWordCount.create() public void ejbCreate() { System.out.print( "WordCountBean.ejbCreate" ); try { InitialContext ic = new InitialContext(); Object obj = ic.lookup( "WordHome" ); wordHome = (HIWord) PortableRemoteObject.narrow( obj, HIWord.class ); } catch (NamingException ex) { ex.printStackTrace(); } System.out.println( " - wordHome is " + wordHome.getClass().getName() ); } // "business" interface from RISort void parse( String in ) { RIWord word = null; String str = null; StringTokenizer st = new StringTokenizer( in ); while (st.hasMoreTokens()) { str = (String) st.nextToken(); try { word = wordHome.findByPrimaryKey( str ); word.increment(); } catch (FinderException ex) { try { wordHome.create( str ); } catch (CreateException cx) { cx.printStackTrace(); } catch (RemoteException rx) { rx.printStackTrace(); } } catch (RemoteException ex) { ex.printStackTrace(); } } } String getTotals() { Collection coll = null; RIWord word = null; StringBuffer sb = new StringBuffer(); try { coll = wordHome.findAllWords(); for (Iterator it = coll.iterator(); it.hasNext(); ) { sb.append( " " ); word = (RIWord) PortableRemoteObject.narrow( it.next(), RIWord.class ); sb.append( word.getSummary().toString() ); sb.append( '\n' ); } } catch (FinderException ex) { ex.printStackTrace(); } catch (RemoteException ex) { ex.printStackTrace(); } return sb.toString(); } // "technical" interface from SessionBean public void ejbActivate() { System.out.println( "WordCountBean.ejbActivate" ); } public void ejbPassivate() { System.out.println( "WordCountBean.ejbPassivate" ); } public void ejbRemove() { System.out.println( "WordCountBean.ejbRemove" ); } public void setSessionContext( SessionContext ctx ) { System.out.println( "WordCountBean.setSessionContext" ); context = ctx; } } package demos; import java.io.*; import java.rmi.RemoteException; import javax.rmi.PortableRemoteObject; import javax.naming.*; import javax.ejb.CreateException; public class WordCountClient { public static void main( String[] args ) { BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); String str; try { InitialContext ic = new InitialContext(); Object obj = ic.lookup( "WordCountHome" ); HIWordCount countHome = (HIWordCount) PortableRemoteObject.narrow( obj, HIWordCount.class ); RIWordCount theCounter = countHome.create(); while (true) { System.out.print( "Enter: " ); str = in.readLine(); if (str.equals( "quit" )) break; theCounter.parse( str ); System.out.print( theCounter.getTotals() ); } } catch (NamingException ex ) { ex.printStackTrace(); } catch (CreateException ex ) { ex.printStackTrace(); } catch (RemoteException ex ) { ex.printStackTrace(); } catch (IOException ex ) { ex.printStackTrace(); } } } // D:\> j2ee -verbose // WordCountBean.setSessionContext // WordCountBean.ejbCreate - wordHome is demos._HIWord_Stub // WordBean.setEntityContext: null // WordBean.ejbCreate - 1234 // WordBean.ejbPostCreate - 1234 // WordBean.setEntityContext: null // WordBean.ejbCreate - 12345 // WordBean.ejbPostCreate - 12345 // WordBean.setEntityContext: null // WordBean.ejbCreate - 123456 // WordBean.ejbPostCreate - 123456 // WordBean.setEntityContext: null // WordBean.ejbLoad - 1234 WordBean.ejbStore - 1234 // WordBean.ejbLoad - 12345 WordBean.ejbStore - 12345 // WordBean.ejbLoad - 1234 WordBean.ejbStore - 1234 // WordBean.ejbLoad - 123456 WordBean.ejbStore - 123456 // WordBean.ejbLoad - 12345 WordBean.ejbStore - 12345 // WordBean.ejbLoad - 1234 WordBean.ejbStore - 1234 // WordBean.ejbCreate - 1234567 // WordBean.ejbPostCreate - 1234567 // WordBean.setEntityContext: null // WordBean.ejbLoad - 1234 WordBean.ejbStore - 1234 // WordBean.ejbLoad - 1234567 WordBean.ejbStore - 1234567 // WordBean.ejbLoad - 123456 WordBean.ejbStore - 123456 // WordBean.ejbLoad - 12345 WordBean.ejbStore - 12345 // D:\> cloudscape -start // D:\j2ee> deploytool // D:\j2ee> java demos.WordCountClient // Enter: 1234 12345 123456 1234 12345 1234 // 1234 3 // 12345 2 // 123456 1 // Enter: 123456 12345 1234 // 1234 4 // 12345 3 // 123456 2 // Enter: 1234567 1234 // 1234 5 // 12345 3 // 123456 2 // 1234567 1 // Enter: quit // // D:\j2ee> java demos.WordCountClient // Enter: 1234567 123456 12345 // 1234 5 // 12345 4 // 123456 3 // 1234567 2