Using The J2EE Reference Edition version 1.3 (EJB 2.0) Some changes need to be made to the programs in Chapter 10 in order to use the version 1.3 of the J2EE which implements the EJB 2.0 specification. See the J2EE tutorial at http://java.sun.com/j2ee/tutorial/1_3-fcs/index.html for more information. Section 10.1 Example 10.4 HelloClient.java becomes a J2EE application client. We need to use the deploytool to deploy it. It will have a JAR file as part of the EJB container and a JAR file on the client. The lookup uses the JNDI name java:comp/env/ejb/ClientHello so the first two lines in the try block become Context context = new InitialContext(); Object o = context.lookup("java:comp/env/ejb/ClientHello"); HelloHome home = (HelloHome)PortableRemoteObject.narrow(o,HelloHome.class); Add the import statement import javax.rmi.*; In the deploytool we configure the name, ClientHello, used by the client to correspond to the name, HiBean, used by the Hello bean. This decouples the client from the specific bean in the container. In the General screen, use Remote interfaces only. Do not use the Local interface section. To deploy HelloClient Click on File, New, Application Client Click Next on the Intro screen Select the Hello application, add HelloClient.class, and click Next Select HelloClient as the main class and click Next Click Finish Select HelloClient in the Hello application Select the EJB Refs tab and press Add Enter ejb/ClientHello as CodedName, HelloHome as the Home interface, and HelloRemote as the Local/Remote interface Execute SET APPCPATH=HelloClient.jar and run HelloClient using %j2ee_home%\bin\runclient -client Hello.ear -name HelloClient -textauth When prompted, enter guest as the user name and guest123 as the password. Section 2 For container-managed persistence, the EJB class is abstract, the get and set method are abstract, and the data fields are virtual and not included in the class. There must be get and set methods for each field. The revised Example 10.7, CustomerEJB.java is import javax.ejb.*; public abstract class CustomerEJB implements EntityBean { private EntityContext context; public abstract String getId(); public abstract void setId(String id); public abstract String getName(); public abstract void setName(String name); public abstract String getAddress(); public abstract void setAddress(String address); public abstract double getDue(); public abstract void setDue(double due); public String ejbCreate(String id, String name, String address, double due) throws CreateException { setId(id); setName(name); setAddress(address); setDue(due); return id; } public void ejbPostCreate(String id, String name, String address, double due) { } public void setEntityContext(EntityContext context) { this.context = context; } public void unsetEntityContext() { context = null; } public void ejbActivate() { } public void ejbPassivate() { } public void ejbRemove() { } public void ejbLoad() { } public void ejbStore() { } } The finder methods should return a Collection (see Chapter 11) rather than an Enumeration. Thus Example 10.6 CustomerHome.java becomes import java.rmi.RemoteException; import javax.ejb.*; import java.util.*; public interface CustomerHome extends EJBHome { CustomerRemote create(String id, String name, String address, double due) throws RemoteException, CreateException; CustomerRemote findByPrimaryKey(String id) throws FinderException, RemoteException; Collection findByName(String name) throws FinderException, RemoteException; Collection findGreaterDue(double amount) throws FinderException, RemoteException; } Change and deploy CustomerClient using steps analogous to those above for HelloClient. Include the change from Enumeration to Collection. The revised Example 10.8 is import javax.naming.*; import javax.rmi.*; import java.util.*; public class CustomerClient { public static void main(String[] args) { try { Context initial = new InitialContext(); Object o = initial.lookup("java:comp/env/ejb/ClientCustomer"); CustomerHome home = (CustomerHome)PortableRemoteObject.narrow (o,CustomerHome.class); CustomerRemote customer = home.create("1234", "Fred Flynn", "22 First St.", 1667.00); customer = home.create("5678", "Darnell Davis", "33 Second St.", 130.95); customer = home.create("4321", "Marla Martinez", "44 Third St.", 0.0); customer = home.create("8765", "Carla Kahn", "55 Fourth St.", 0.0); CustomerRemote id5678 = home.findByPrimaryKey("5678"); System.out.println(id5678.getName()); Collection c = home.findByName("Carla Kahn"); Iterator e = c.iterator(); while (e.hasNext()) { CustomerRemote carla = (CustomerRemote)e.next(); String id = (String)carla.getPrimaryKey(); String name = carla.getName(); String address = carla.getAddress(); double due = carla.getDue(); System.out.println(id + ' ' + name + ' ' + address + ' ' + due); } c = home.findGreaterDue(100.00); e = c.iterator(); while (e.hasNext()) { CustomerRemote greater = (CustomerRemote)e.next(); String id = (String)greater.getPrimaryKey(); String name = greater.getName(); String address = greater.getAddress(); double due = greater.getDue(); System.out.println(id + ' ' + name + ' ' + address + ' ' + due); } }catch (Exception e) { e.printStackTrace(); } } } After creating the Sales application using deploytool the revised steps to package the bean are: Click File, New, Enterprise Bean "Introduction" Screen Click Next "EJB JAR" Screen Check Create New JAR in Application and select Sales Enter TestJAR as the JAR Display Name Click Edit "Edit Contents of SalesJAR" Subscreen Enter the Starting Directory name Select the CustomerEJB, CustromerHome, and CustomerRemote class files. Click Add Click OK Click Next "General" Screen Select Entity as the Bean Type Select CustomerEJB as the Enterprise Bean Class Enter CustomerBean as the Enterprise Bean Name and hit Enter Select CustomerHome as the Remote Home Interface Select CustomerRemote as the Remote Interface Click Next "Entity Settings" Screen Select Container Managed Persistence (2.0) Check name, id, address, due Enter Customer as the Abstract Schema Name Enter java.lang.String as the Primary Key Class Select id as the Primary Key Field Name Press Finder/Select Methods "Finder/Select Methods for CustomerBean" Subscreen Select findByName and enter the EJB-QL Query SELECT OBJECT(c) FROM Customer c WHERE c.name = ?1 Select findGreaterDue and enter the EJB-QL Query SELECT OBJECT(c) FROM Customer WHERE c.due > ?1 Press OK Press Finish To specify deployment settings Select CustomerBean in the Local Applications area Select the Entity tab Click Deployment Settings "Deployment Settings" Subscreen Click Database Settings "Deployment Settings" Sub Subscreen Enter jdbc/Cloudscape as the Database JNDI Name, and hit Enter Click Generate SQL Now Click OK Click Generate Default SQL "Information Dialog" Click OK Click OK Execute SET APPCPATH=CustomerClient.jar and run the client using %j2ee_home%\bin\runclient -client Sales.ear -name CustomerClient -textauth Section 10.3 In Example 10.11, AgentEJB.java, change the lookup to Object o = initial.lookup("java:comp/env/ejb/CustomerHome"); CustomerHome home = (CustomerHome)javax.rmi.PortableRemoteObject .narrow(o,CustomerHome.class); In Example 10.12, SalesClient.java, change the lookup to Object o = initial.lookup("java:comp/env/ejb/ClientAgent"); AgentHome home = (AgentHome)javax.rmi.PortableRemoteObject .narrow(o,AgentHome.class); Line 3 from the bottom page 406 Choose AgentHome as the Remote Home Interface. Bottom line p. 406 Enter AgentBean as the Enterprise Bean display name and hit Enter Add as a new third line p. 407 Click Next (for Transaction Management) Change the old line 5 on p. 407 to Enter ejb/CustomerHome as the Coded name. Remove the line on p. 407 Click Undeploy Click Yes in the Confirm dialog Change and deploy CustomerClient using steps analogous to those above for HelloClient. Because the Sales application is redployed we need to run the CustomerClient again to create the customer database. Then execute SET APPCPATH=SalesClient.jar and %j2ee_home%\bin\runclient -client Sales.ear -name SalesClient -textauth Section 10.4 In Example 10.13, SalesServlet.java, change the lookup to Object o = initial.lookup("java:comp/env/ejb/ClientAgent"); Replace the first 11 lines of p. 413 with Select Create New WAR File in Application and Select Sales. Enter SalesWar as the Display name. Click Edit. Select SalesServlet.html. Click Add. Select SalesServlet.class. Click Add. Click OK. Click Next (WAR File). After line 20 from the bottom of p. 413, (WAR file Environment),change the remainder of the page to Click Next (Context Parameters). Enter ejb/ClientAgent as the Coded Name. Select Session as the Type. Select AgentHome as the Home. Select AgentRemote as the Remote. Click Finish. Deploying the Application Select Sales in the Server Application area. Click Tools, Deploy. Do not check Return Client Jar. Enter MyAgent as the JNDI name. Click Next. Enter SalesContextRoot as the Context Root. Click Next. Click Finish. Click OK. In Example 10.14, CustomerBean.java, change the lookup to Object o = initial.lookup("java:comp/env/ejb/CustomerHome"); In Example 10.15, Customer.jsp, add after <%@ page import="CustomerBean" %> (Classes in an unnamed package need to be imported) Change the last two lines of p. 417 to Click Edit. Enter the Starting Directory. Change the first eight lines of p. 418 to Select Customer.jsp Click Add. Select CustomerBean.class. Click Add. Click OK. Click Next (WAR File). In line 12 change "Display name" to "Web Component name, and hit Enter". After (WAR file environment) add Click Next (Context Parameters) Change the next two lines to Click Add (Enterprise Bean References) Enter ejb/CustomerHome as the Coded Name Change "Remote" to "Local/Remote" Omit the two lines Click Undeploy. Click Yes in the Confirm dialog.