|
Download
FAQ History |
|
API
Search Feedback |
Method Invocations in RosterApp
To show how the various components interact, this section describes the sequence of method invocations that occur for particular functions. The source code for the components is in the
<INSTALL>/j2eetutorial14/examples/ejb/cmprosterdirectory.Creating a Player
1. RosterClient
The
RosterClientinvokes thecreatePlayerbusiness method of theRosterBeansession bean to create a new player. In the following line of code, the type of themyRosterobject isRoster, the remote interface ofRosterBean. The argument of thecreatePlayermethod is aPlayerDetailsobject, which encapsulates information about a particular player.2. RosterBean
The
createPlayermethod of theRosterBeansession bean creates a new instance of thePlayerBeanentity bean. Because the access ofPlayerBeanis local, thecreatemethod is defined in the local home interface,LocalPlayerHome. The type of theplayerHomeobject isLocalPlayerHome. Here is the source code for thecreatePlayermethod:public void createPlayer(PlayerDetails details) { try { LocalPlayer player = playerHome.create(details.getId(), details.getName(), details.getPosition(), details.getSalary()); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }3. PlayerBean
The
ejbCreatemethod assigns the input arguments to the bean's persistent fields by calling thesetaccess methods. At the end of the transaction that contains the create call, the container saves the persistent fields in the database by issuing an SQLINSERTstatement. The code for theejbCreatemethod follows.public String ejbCreate (String id, String name, String position, double salary) throws CreateException { setPlayerId(id); setName(name); setPosition(position); setSalary(salary); return null; }Adding a Player to a Team
1. RosterClient
The
RosterClientcalls theaddPlayerbusiness method of theRosterBeansession bean to add player P1 to team T1. TheP1andT1parameters are the primary keys of thePlayerBeanandTeamBeaninstances, respectively.2. RosterBean
The
addPlayermethod performs two steps. First, it callsfindByPrimaryKeyto locate thePlayerBeanandTeamBeaninstances. Second, it invokes theaddPlayerbusiness method of theTeamBeanentity bean. Here is the source code for theaddPlayermethod of theRosterBeansession bean:public void addPlayer(String playerId, String teamId) { try { LocalTeam team = teamHome.findByPrimaryKey(teamId); LocalPlayer player = playerHome.findByPrimaryKey(playerId); team.addPlayer(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }3. TeamBean
The
TeamBeanentity bean has a relationship field namedplayers, aCollectionthat represents the players that belong to the team. The access methods for theplayersrelationship field are as follows:The
addPlayermethod ofTeamBeaninvokes thegetPlayersaccess method to fetch theCollectionof relatedLocalPlayerobjects. Next, theaddPlayermethod invokes theaddmethod of theCollectioninterface. Here is the source code for theaddPlayermethod:public void addPlayer(LocalPlayer player) { try { Collection players = getPlayers(); players.add(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }Removing a Player
1. RosterClient
To remove player
P4, the client would invoke theremovePlayermethod of theRosterBeansession bean:2. RosterBean
The
removePlayermethod locates thePlayerBeaninstance by callingfindBy-PrimaryKeyand then invokes theremovemethod on the instance. This invocation signals the container to delete the row in the database that corresponds to thePlayerBeaninstance. The container also removes the item for this instance from theplayersrelationship field in theTeamBeanentity bean. By this removal, the container automatically updates theTeamBean-PlayerBeanrelationship. Here is theremovePlayermethod of theRosterBeansession bean:public void removePlayer(String playerId) { try { LocalPlayer player = playerHome.findByPrimaryKey(playerId); player.remove(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }Dropping a Player from a Team
1. RosterClient
To drop player
P2from teamT1, the client would call thedropPlayermethod of theRosterBeansession bean:2. RosterBean
The
dropPlayermethod retrieves thePlayerBeanandTeamBeaninstances by calling theirfindByPrimaryKeymethods. Next, it invokes thedropPlayerbusiness method of theTeamBeanentity bean. ThedropPlayermethod of theRosterBeansession bean follows:public void dropPlayer(String playerId, String teamId) { try { LocalPlayer player = playerHome.findByPrimaryKey(playerId); LocalTeam team = teamHome.findByPrimaryKey(teamId); team.dropPlayer(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }3. TeamBean
The
dropPlayermethod updates theTeamBean-PlayerBeanrelationship. First, the method retrieves theCollectionofLocalPlayerobjects that correspond to theplayersrelationship field. Next, it drops the targetplayerby calling theremovemethod of theCollectioninterface. Here is thedropPlayermethod of theTeamBeanentity bean:public void dropPlayer(LocalPlayer player) { try { Collection players = getPlayers(); players.remove(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }Getting the Players of a Team
1. RosterClient
The client can fetch a team's players by calling the
getPlayersOfTeammethod of theRosterBeansession bean. This method returns anArrayListofPlayerDetailsobjects. APlayerDetailobject contains four variables--playerId,name,position, andsalary--which are copies of thePlayerBeanpersistent fields. TheRosterClientcalls thegetPlayersOfTeammethod as follows:2. RosterBean
The
getPlayersOfTeammethod of theRosterBeansession bean locates theLocalTeamobject of the target team by invoking thefindByPrimaryKeymethod. Next, thegetPlayersOfTeammethod calls thegetPlayersmethod of theTeamBeanentity bean. Here is the source code for thegetPlayersOfTeammethod:public ArrayList getPlayersOfTeam(String teamId) { Collection players = null; try { LocalTeam team = teamHome.findByPrimaryKey(teamId); players = team.getPlayers(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } return copyPlayersToDetails(players); }The
getPlayersOfTeammethod returns theArrayListofPlayerDetailsobjects that is generated by thecopyPlayersToDetailsmethod:private ArrayList copyPlayersToDetails(Collection players) { ArrayList detailsList = new ArrayList(); Iterator i = players.iterator(); while (i.hasNext()) { LocalPlayer player = (LocalPlayer) i.next(); PlayerDetails details = new PlayerDetails(player.getPlayerId(), player.getName(), player.getPosition(), player.getSalary()); detailsList.add(details); } return detailsList; }3. TeamBean
The
getPlayersmethod of theTeamBeanentity bean is an access method of theplayersrelationship field:This method is exposed to local clients because it is defined in the local interface,
LocalTeam:When invoked by a local client, a
getaccess method returns a reference to the relationship field. If the local client alters the object returned by agetaccess method, it also alters the value of the relationship field inside the entity bean. For example, a local client of theTeamBeanentity bean could drop a player from a team as follows:LocalTeam team = teamHome.findByPrimaryKey(teamId); Collection players = team.getPlayers(); players.remove(player);If you want to prevent a local client from modifying a relationship field in this manner, you should take the approach described in the next section.
Getting a Copy of a Team's Players
In contrast to the methods discussed in the preceding section, the methods in this section demonstrate the following techniques:
1. RosterClient
If you wanted to hide the salary of a player from a remote client, you would require the client to call the
getPlayersOfTeamCopymethod of theRosterBeansession bean. Like thegetPlayersOfTeammethod, thegetPlayersOfTeamCopymethod returns anArrayListofPlayerDetailsobjects. However, the objects returned bygetPlayersOfTeamCopyare different: theirsalaryvariables have been set to zero. TheRosterClientcalls thegetPlayersOfTeamCopymethod as follows:2. RosterBean
Unlike the
getPlayersOfTeammethod, thegetPlayersOfTeamCopymethod does not invoke thegetPlayersaccess method that is exposed in theLocalTeaminterface. Instead, thegetPlayersOfTeamCopymethod retrieves a copy of the player information by invoking thegetCopyOfPlayersbusiness method that is defined in theLocalTeaminterface. As a result, thegetPlayersOfTeamCopymethod cannot modify theplayersrelationship field ofTeamBean. Here is the source code for thegetPlayersOfTeamCopymethod ofRosterBean:public ArrayList getPlayersOfTeamCopy(String teamId) { ArrayList playersList = null; try { LocalTeam team = teamHome.findByPrimaryKey(teamId); playersList = team.getCopyOfPlayers(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } return playersList; }3. TeamBean
The
getCopyOfPlayersmethod ofTeamBeanreturns anArrayListofPlayerDetailsobjects. To create thisArrayList, the method iterates through theCollectionof relatedLocalPlayerobjects and copies information to the variables of thePlayerDetailsobjects. The method copies the values ofPlayerBeanpersistent fields--except for thesalaryfield, which it sets to zero. As a result, a player's salary is hidden from a client that invokes thegetPlayersOfTeamCopymethod. The source code for thegetCopyOfPlayersmethod ofTeamBeanfollows.public ArrayList getCopyOfPlayers() { ArrayList playerList = new ArrayList(); Collection players = getPlayers(); Iterator i = players.iterator(); while (i.hasNext()) { LocalPlayer player = (LocalPlayer) i.next(); PlayerDetails details = new PlayerDetails(player.getPlayerId(), player.getName(), player.getPosition(), 0.00); playerList.add(details); } return playerList; }Finding the Players by Position
1. RosterClient
The client starts the procedure by invoking the
getPlayersByPositionmethod of theRosterBeansession bean:2. RosterBean
The
getPlayersByPositionmethod retrieves theplayerslist by invoking thefindByPositionmethod of thePlayerBeanentity bean:public ArrayList getPlayersByPosition(String position) { Collection players = null; try { players = playerHome.findByPosition(position); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } return copyPlayersToDetails(players); }3. PlayerBean
The
LocalPlayerHomeinterface defines thefindByPositionmethod:Because the
PlayerBeanentity bean uses container-managed persistence, the entity bean class (PlayerBean) does not implement its finder methods. To specify the queries associated with the finder methods, EJB QL queries must be defined in the bean's deployment descriptor. For example, thefindByPositionmethod has this EJB QL query:At runtime, when the container invokes the
findByPositionmethod, it will execute the corresponding SQLSELECTstatement.For details about EJB QL, please refer to Chapter 29. To learn how to view and edit an EJB QL query in
deploytool, see the section Finder/Select Methods Dialog Box (PlayerBean).Getting the Sports of a Player
1. RosterClient
The client invokes the
getSportsOfPlayermethod of theRosterBeansession bean:2. RosterBean
The
getSportsOfPlayermethod returns anArrayListofStringobjects that represent the sports of the specified player. It constructs theArrayListfrom aCollectionreturned by thegetSportsbusiness method of thePlayerBean entitybean. Here is the source code for thegetSportsOfPlayermethod of theRosterBeansession bean:public ArrayList getSportsOfPlayer(String playerId) { ArrayList sportsList = new ArrayList(); Collection sports = null; try { LocalPlayer player = playerHome.findByPrimaryKey(playerId); sports = player.getSports(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } Iterator i = sports.iterator(); while (i.hasNext()) { String sport = (String) i.next(); sportsList.add(sport); } return sportsList; }3. PlayerBean
The
getSportsmethod is a wrapper for theejbSelectSportsmethod. Because the parameter of theejbSelectSportsmethod is of typeLocalPlayer, thegetSportsmethod passes along a reference to the entity bean instance. ThePlayerBeanclass implements thegetSportsmethod as follows:public Collection getSports() throws FinderException { LocalPlayer player = (team.LocalPlayer)context.getEJBLocalObject(); return ejbSelectSports(player); }The
PlayerBeanclass defines theejbSelectSportsmethod:The bean's deployment descriptor specifies the following EJB QL query for the
ejbSelectSportsmethod:Because
PlayerBeanuses container-managed persistence, when theejbSelectSportsmethod is invoked the EJB container will execute its corresponding SQLSELECTstatement.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.