By Richard Monson-Haefel, Bill Burke, Sacha Labourey
Cover | Table of Contents | Colophon
Person would have matching
Person_Stub and Person_Skeleton
classes. As shown in Figure 1-2, the business
object and skeleton reside on the middle tier, and the stub resides
on the client.javax.ejb package, developers can create,
assemble, and deploy components that conform to the EJB
specification.CruiseHomeRemote cruiseHome = ... ; // use JNDI to get the home
// Get the cruise ID text field 1.
String cruiseID = textField1.getText( );
// Create an EJB primary key from the cruise ID.
Integer pk = new Integer(cruiseID);
// Use the primary key to find the cruise.
CruiseRemote cruise = cruiseHome.findByPrimaryKey(pk);
// Set text field 2 to show the cruise name.
textField2.setText(cruise.getName( ));
// Get a remote reference to the ship that will be used
// for the cruise from the cruise bean.
ShipRemote ship = cruise.getShip( );
// Set text field 3 to show the ship's name.
textField3.setText(ship.getName( ));
// Get all the cabins on the ship.
Collection cabins = ship.getCabins( );
Iterator cabinItr = cabins.iterator( );
// Iterate through the enumeration, adding the name of each cabin
// to a list box.
while(cabinItr.hasNext( ))
CabinRemote cabin = (CabinRemote)cabinItr.next( );
listBox1.addItem(cabin.getName( ));
}
EntityBean,
SessionBean
, and
MessageDrivenBean
interfaces provide callback methods
that notify the bean class of life-cycle events. At runtime, the
container invokes these methods on the bean instance when relevant
events occur. For example, when the container is about to write an
entity bean instance's state to the database, it
first calls the bean instance's ejbStore(
)
method. This call gives the bean
instance an opportunity to do cleanup on its state before
it's written to the database. The
ejbLoad( ) method is
called just after the bean's fields are populated
from the database, providing the bean developer with an opportunity
to manage the bean's state before the first business
method is called. Other
callback methods can be used by the bean class in a similar fashion.
EJB defines when these various callback methods are invoked and what
can be done within their contexts.EJBContext, and the JNDI environment-naming
context. The callback methods notify the bean class that it is
involved in a life-cycle event. The package com.titan.cabin;
import java.rmi.RemoteException;
public interface CabinRemote extends javax.ejb.EJBObject {
public String getName( ) throws RemoteException;
public void setName(String str) throws RemoteException;
public int getDeckLevel( ) throws RemoteException;
public void setDeckLevel(int level) throws RemoteException;
public int getShipId( ) throws RemoteException;
public void setShipId(int sp) throws RemoteException;
public int getBedCount( ) throws RemoteException;
public void setBedCount(int bc) throws RemoteException;
}
CabinRemote
interface defines four properties:
name, deckLevel,
shipId, and bedCount.
Properties
are attributes of an enterprise bean that can be accessed by public
set and get methods.CabinRemote interface
a part of a new package named com.titan.cabin.
Place all the classes and interfaces associated with each type of
bean in a package specific to the bean. Because our beans are for the
use of the Titan cruise line, we placed these packages in the
com.titan package hierarchy. We also created
directory structures that match package structures. If you are using
an IDE that works directly with Java files, create a new directory
called InitialContext, which it then used to
get a remote reference to the homes of the Cabin and TravelAgent
EJBs. The InitialContext is part of a larger API
called the Java Naming and Directory Interface ( JNDI). We use JNDI
to look up an EJB home in an EJB server just like we might use a
phone book to find the home number of a friend or business associate.InitialContext, which it then used to
get a remote reference to the homes of the Cabin and TravelAgent
EJBs. The InitialContext is part of a larger API
called the Java Naming and Directory Interface ( JNDI). We use JNDI
to look up an EJB home in an EJB server just like we might use a
phone book to find the home number of a friend or business associate.CabinLocal interface is
the local interface defined for the Cabin EJB:package com.titan.cabin;
import javax.ejb.EJBException;
public interface CabinLocal extends javax.ejb.EJBLocalObject {
public String getName( ) throws EJBException;
public void setName(String str) throws EJBException;
public int getDeckLevel( ) throws EJBException;
public void setDeckLevel(int level) throws EJBException;
public int getShipId( ) throws EJBException;
public void setShipId(int sp) throws EJBException;
public int getBedCount( ) throws EJBException;
public void setBedCount(int bc) throws EJBException;
}Customer.setName( ) than by executing an SQL
command against the database. In addition, using entity beans
provides opportunities for software reuse. Once an entity bean has
been defined, its definition can be used throughout
Titan's system in a consistent manner. The concept
of a customer, for example, is used in many areas of
Titan's business, including booking, accounts
receivable, and marketing. A Customer EJB provides Titan with one
complete way of accessing customer information, and thus it ensures
that access to the information is consistent and simple. Representing
data as entity beans can make development easier and more
cost-effective.AddressLocal. This is an
example of a relationship field called the
homeAddress field.
CUSTOMER
table from which to get our
customer data. The relational database table definition in SQL is as
follows:CREATE TABLE CUSTOMER
(
ID INT PRIMARY KEY NOT NULL,
LAST_NAME CHAR(20),
FIRST_NAME CHAR(20)
)
CustomerBean class is an abstract class that the
container uses for generating a concrete implementation, the
persistence entity class. The mechanism used by the container for
generating a persistence entity class varies, but most vendors
generate a subclass of the abstract class provided by the bean
developer (see Figure 6-4).
CustomerBean class:package com.titan.customer;
import javax.ejb.EntityContext;
public abstract class CustomerBean implements javax.ejb.EntityBean {
public Integer ejbCreate(Integer id){
setId(id);
return null;
}
public void ejbPostCreate(Integer id){
}
// abstract accessor methods
public abstract Integer getId( );
public abstract void setId(Integer id);
public abstract String getLastName( );
public abstract void setLastName(String lname);
public abstract String getFirstName( );
public abstract void setFirstName(String fname);
// standard callback methods
public void setEntityContext(EntityContext ec){}
public void unsetEntityContext( ){}
public void ejbLoad( ){}
public void ejbStore( ){}
public void ejbActivate( ){}
public void ejbPassivate( ){}
public void ejbRemove( ){}
}