Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Building Java Enterprise Applications Volume I: Architecture phần 4 docx
Nội dung xem thử
Mô tả chi tiết
Building Java™ Enterprise Applications Volume I: Architecture
82
It is a good idea at this point to actually deploy the Sequence session
bean, along with the Office entity bean. You can compile these classes,
wrap them up in the JAR archive (as I talked about in the previous
chapter), and deploy the JAR into your container (as described in
Appendix D). Although you haven't added any functionality to your
entity bean or taken advantage of the new session bean, you can head
off any errors here. Choosing not to do this widens the window of errors
that may occur. It's better to catch small mistakes and typos in your
code or descriptor now, by deploying the beans before continuing.
5.1.3 Integrating the Changes
Now that the sequencing functionality is available to the application, you just need to take
advantage of it. With the need for an ID eliminated from entity beans' clients, you first need to
change the home interface's create( ) method, as I talked about earlier. This simply
involves removing the id variable from the method signature. That change results in the
following create( ) signature in the OfficeHome class:
public Office create(String city, String state)
throws CreateException, RemoteException;
You should make the same change to the OfficeLocalHome interface:
public OfficeLocal create(String city, String state)
throws CreateException, EJBException;
The next change in the code is the bean implementation itself. The ejbCreate( ) and
ejbPostCreate( ) methods both should have the id variable removed from their method
signatures. Be sure you change both of these, as it's easy to forget the ejbPostCreate( )
method. Finally, this bean needs to access the new Sequence bean and use it to obtain an ID
value for a new office. This is a replacement for accepting the value from a bean client.
Modify your bean's ejbCreate( ) method as shown in Example 5-9. Once you've added the
necessary import statements to deal with JNDI, RMI, and the Sequence bean, you are ready
to access the next primary key value through the functionality just provided.
Example 5-9. The OfficeBean Using the Sequence Bean for ID Values
package com.forethought.ejb.office;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.forethought.ejb.sequence.SequenceException;
import com.forethought.ejb.sequence.SequenceLocal;
import com.forethought.ejb.sequence.SequenceLocalHome;
import com.forethought.ejb.util.EntityAdapter;
Building Java™ Enterprise Applications Volume I: Architecture
83
public class OfficeBean extends EntityAdapter {
public Integer ejbCreate(String city, String state)
throws CreateException {
// Get the next primary key value
try {
Context context = new InitialContext( );
// Note that RMI-IIOP narrowing is not required
SequenceLocalHome home = (SequenceLocalHome)
context.lookup("java:comp/env/ejb/SequenceLocalHome");
SequenceLocal sequence = home.create( );
String officeKey =
String)context.lookup("java:comp/env/constants/OfficeKey");
Integer id = sequence.getNextValue(officeKey);
// Set values
setId(id);
setCity(city);
setState(state);
return null;
} catch (NamingException e) {
throw new CreateException("Could not obtain an " +
"InitialContext.");
} catch (SequenceException e) {
throw new CreateException("Error getting primary key value: " +
e.getMessage( ));
}
}
public void ejbPostCreate(String city, String state) {
// Empty implementation
}
public abstract Integer getId( );
public abstract void setId(Integer id);
public abstract String getCity( );
public abstract void setCity(String city);
public abstract String getState( );
public abstract void setState(String state);
}
Notice that you had to explicitly add a throws CreateException clause to the modified
ejbCreate( ) method; although the Office home interface already has this (and therefore, no
changes are needed in that respect), you must add it to the bean to allow it to throw the
Exception[3] within the method. You'll also notice that the code relies heavily on JNDI and
the ENC context for information: first for obtaining the Sequence bean's home interface, and
second for obtaining the constant for the key name of the OFFICES table. While both of these
could be obtained in more "normal" ways, such as looking up the JNDI name of the home
interface, and using a Java constant for the key name, using the environment context adds
options for the deployer. For example, changing the name of the OFFICES table would not
affect the bean; the deployer could change the CMP mapping and the JNDI constant for the
3 If this is confusing, note that the CreateException that the home interface declares is thrown by the remote stub when problems occur with
network communication, RMI, and other client-side components. Therefore, for the server-side component to throw the same Exception,
the throws clause must be added to the bean method declaration.