📄 cmp4.html
字号:
}<a name="wp80135"> </a></pre></div><a name="wp80136"> </a><h3 class="pHeading2">Dropping a Player from a Team</h3><a name="wp80137"> </a><h4 class="pHeading3">1. RosterClient</h4><a name="wp80138"> </a><p class="pBody">To drop player <code class="cCode">P2</code> from team <code class="cCode">T1</code>, the client would call the <code class="cCode">dropPlayer</code> method of the <code class="cCode">RosterEJB</code> session bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">myRoster.dropPlayer("P2", "T1");<a name="wp80139"> </a></pre></div><a name="wp80140"> </a><h4 class="pHeading3">2. RosterEJB</h4><a name="wp80141"> </a><p class="pBody">The <code class="cCode">dropPlayer</code> method retrieves the <code class="cCode">PlayerEJB</code> and <code class="cCode">TeamEJB</code> instances by calling their <code class="cCode">findByPrimaryKey</code> methods. Next, it invokes the <code class="cCode">dropPlayer</code> business method of the <code class="cCode">TeamEJB</code> entity bean. The <code class="cCode">dropPlayer</code> method of the <code class="cCode">RosterEJB</code> session bean follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">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()); }}<a name="wp80142"> </a></pre></div><a name="wp80143"> </a><h4 class="pHeading3">3. TeamEJB</h4><a name="wp80144"> </a><p class="pBody">The <code class="cCode">dropPlayer</code> method updates the <code class="cCode">TeamEJB-PlayerEJB</code> relationship. First, the method retrieves the <code class="cCode">Collection</code> of <code class="cCode">LocalPlayer</code> objects that correspond to the <code class="cCode">players</code> relationship field. Next, it drops the target <code class="cCode">player</code> by calling the <code class="cCode">remove</code> method of the <code class="cCode">Collection</code> interface. Here is the <code class="cCode">dropPlayer</code> method of the <code class="cCode">TeamEJB</code> entity bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void dropPlayer(LocalPlayer player) { try { Collection players = getPlayers(); players.remove(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); }}<a name="wp80145"> </a></pre></div><a name="wp80146"> </a><h3 class="pHeading2">Getting the Players of a Team</h3><a name="wp80147"> </a><h4 class="pHeading3">1. RosterClient</h4><a name="wp80148"> </a><p class="pBody">The client can fetch a team's players by calling the <code class="cCode">getPlayersOfTeam</code> method of the <code class="cCode">RosterEJB</code> session bean. This method returns an <code class="cCode">ArrayList</code> of <code class="cCode">PlayerDetails</code> objects. A <code class="cCode">PlayerDetail</code> object contains four variables--<code class="cCode">playerId</code>, <code class="cCode">name</code>, <code class="cCode">position</code>, and <code class="cCode">salary</code>--which are copies of the <code class="cCode">PlayerEJB</code> persistent fields. The <code class="cCode">RosterClient</code> calls the <code class="cCode">getPlayersOfTeam</code> method as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">playerList = myRoster.getPlayersOfTeam("T2");<a name="wp80149"> </a></pre></div><a name="wp80150"> </a><h4 class="pHeading3">2. RosterEJB</h4><a name="wp80151"> </a><p class="pBody">The <code class="cCode">getPlayersOfTeam</code> method of the <code class="cCode">RosterEJB</code> session bean locates the <code class="cCode">LocalTeam</code> object of the target team by invoking the <code class="cCode">findByPrimaryKey</code> method. Next, the <code class="cCode">getPlayersOfTeam</code> method calls the <code class="cCode">getPlayers</code> method of the <code class="cCode">TeamEJB</code> entity bean. Here is the source code for the <code class="cCode">getPlayersOfTeam</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">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);}<a name="wp80153"> </a></pre></div><a name="wp80154"> </a><p class="pBody">The <code class="cCode">getPlayersOfTeam</code> method returns the <code class="cCode">ArrayList</code> of <code class="cCode">PlayerDetails</code> objects that is generated by the <code class="cCode">copyPlayersToDetails</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">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;} <a name="wp80155"> </a></pre></div><a name="wp80156"> </a><h4 class="pHeading3">3. TeamEJB</h4><a name="wp80157"> </a><p class="pBody">The <code class="cCode">getPlayers</code> method of the <code class="cCode">TeamEJB</code> entity bean is an access method of the <code class="cCode">players</code> relationship field:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public abstract Collection getPlayers();<a name="wp80158"> </a></pre></div><a name="wp80159"> </a><p class="pBody">This method is exposed to local clients because it is defined in the local interface, <code class="cCode">LocalTeam</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public Collection getPlayers();<a name="wp80160"> </a></pre></div><a name="wp80161"> </a><p class="pBody">When invoked by a local client, a <code class="cCode">get</code> access method returns a reference to the relationship field. If the local client alters the object returned by a <code class="cCode">get</code> access method, it also alters the value of the relationship field inside the entity bean. For example, a local client of the <code class="cCode">TeamEJB</code> entity bean could drop a player from a team as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">LocalTeam team = teamHome.findByPrimaryKey(teamId);Collection players = team.getPlayers();players.remove(player);<a name="wp80162"> </a></pre></div><a name="wp80164"> </a><p class="pBody">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.</p><a name="wp80165"> </a><h3 class="pHeading2">Getting a Copy of a Team's Players</h3><a name="wp80166"> </a><p class="pBody">In contrast to the methods discussed in the preceding section, the methods in this section demonstrate the following techniques:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80167"> </a><div class="pSmartList1"><li>Filtering the information passed back to the remote client</li></div><a name="wp80168"> </a><div class="pSmartList1"><li>Preventing the local client from directly modifying a relationship field</li></div></ul></div><a name="wp80169"> </a><h4 class="pHeading3">1. RosterClient</h4><a name="wp80170"> </a><p class="pBody">If you wanted to hide the salary of a player from a remote client, you would require the client to call the <code class="cCode">getPlayersOfTeamCopy</code> method of the <code class="cCode">RosterEJB</code> session bean. Like the <code class="cCode">getPlayersOfTeam</code> method, the <code class="cCode">getPlayersOfTeamCopy</code> method returns an <code class="cCode">ArrayList</code> of <code class="cCode">PlayerDetails</code> objects. However, the objects returned by <code class="cCode">getPlayersOfTeamCopy</code> are different--their <code class="cCode">salary</code> variables have been set to zero. The <code class="cCode">RosterClient</code> calls the <code class="cCode">getPlayersOfTeamCopy</code> method as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">playerList = myRoster.getPlayersOfTeamCopy("T5");<a name="wp80171"> </a></pre></div><a name="wp80172"> </a><h4 class="pHeading3">2. RosterEJB</h4><a name="wp80173"> </a><p class="pBody">Unlike the <code class="cCode">getPlayersOfTeam</code> method, the <code class="cCode">getPlayersOfTeamCopy</code> method does not invoke the <code class="cCode">getPlayers</code> access method that is exposed in the <code class="cCode">LocalTeam</code> interface. Instead, the <code class="cCode">getPlayersOfTeamCopy</code> method retrieves a copy of the player information by invoking the <code class="cCode">getCopyOfPlayers</code> business method that is defined in the <code class="cCode">LocalTeam</code> interface. As a result, the <code class="cCode">getPlayersOfTeamCopy</code> method cannot modify the <code class="cCode">players</code> relationship field of <code class="cCode">TeamEJB</code>. Here is the source code for the <code class="cCode">getPlayersOfTeamCopy</code> method of <code class="cCode">RosterEJB</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">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;}<a name="wp80174"> </a></pre></div><a name="wp80175"> </a><h4 class="pHeading3">3. TeamEJB</h4><a name="wp80176"> </a><p class="pBody">The <code class="cCode">getCopyOfPlayers</code> method of <code class="cCode">TeamEJB</code> returns an <code class="cCode">ArrayList</code> of <code class="cCode">PlayerDetails</code> objects. To create this <code class="cCode">ArrayList</code>, the method iterates through the <code class="cCode">Collection</code> of related <code class="cCode">LocalPlayer</code> objects and copies information to the variables of the <code class="cCode">PlayerDetails</code> objects. The method copies the values of <code class="cCode">PlayerEJB</code> persistent fields--except for the <code class="cCode">salary</code> field, which it sets to zero. As a result, a player's salary is hidden from a client that invokes the <code class="cCode">getPlayersOfTeamCopy</code> method. The source code for the <code class="cCode">getCopyOfPlayers</code> method of <code class="cCode">TeamEJB</code> follows.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public ArrayList getCopyOfPlayers() { ArrayList playerList = new ArrayList(); Collection players = getPlayers();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -