Building Java Enterprise Applications Volume I: Architecture phần 10 ppt

32 187 0
Building Java Enterprise Applications Volume I: Architecture phần 10 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Building Java™ Enterprise Applications Volume I: Architecture 239 public AccountTypeLocal findByPrimaryKey(Integer accountTypeID) throws FinderException, EJBException; public AccountTypeLocal findByType(String type) throws FinderException, EJBException; } Example E-12 is the implementation class for the AccountType bean. Example E-12. The AccountTypeBean Implementation Class package com.forethought.ejb.accountType; 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; public abstract class AccountTypeBean extends EntityAdapter { public Integer ejbCreate(String type) 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 accountTypeKey = (String)context.lookup( "java:comp/env/constants/AccountTypeKey"); Integer id = sequence.getNextValue(accountTypeKey); // Set values setId(id); setType(type); 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 type) { // Empty implementation } public abstract void setId(Integer id); public abstract Integer getId( ); Building Java™ Enterprise Applications Volume I: Architecture 240 public abstract String getType( ); public abstract void setType(String type); } E.1.4 The Fund Bean Example E-13 is the remote interface for the Fund entity bean. Example E-13. The Fund Remote Interface package com.forethought.ejb.fund; import java.rmi.RemoteException; import javax.ejb.EJBObject; public interface Fund extends EJBObject { public FundInfo getInfo( ) throws RemoteException; public void setInfo(FundInfo fundInfo) throws RemoteException; public Integer getId( ) throws RemoteException; public String getName( ) throws RemoteException; public void setName(String name) throws RemoteException; public String getDescription( ) throws RemoteException; public void setDescription(String description) throws RemoteException; } Example E-14 is the local interface for the Fund bean, and is used in container-managed relationships. Example E-14. The Fund Local Interface package com.forethought.ejb.fund; import javax.ejb.EJBException; import javax.ejb.EJBLocalObject; public interface FundLocal extends EJBLocalObject { public Integer getId( ) throws EJBException; public String getName( ) throws EJBException; public void setName(String name) throws EJBException; public String getDescription( ) throws EJBException; public void setDescription(String description) throws EJBException; } Example E-15 shows the information class (FundInfo) for the Fund entity bean. Building Java™ Enterprise Applications Volume I: Architecture 241 Example E-15. The FundInfo Class package com.forethought.ejb.fund; import java.io.Serializable; public class FundInfo implements Serializable { private int id; private String name; private String description; protected FundInfo(int id, String name, String description) { this.id = id; this.name = name; this.description = description; } public int getId( ) { return id; } public String getName( ) { return name; } public void setName(String name) { this.name = name; } public String getDescription( ) { return description; } public void setDescription(String description) { this.description = description; } } The home interface for the Fund entity bean is shown in Example E-16. Example E-16. The FundHome Interface package com.forethought.ejb.fund; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; import javax.ejb.FinderException; public interface FundHome extends EJBHome { public Fund create(String name, String description) throws CreateException, RemoteException; public Fund findByPrimaryKey(Integer fundID) throws FinderException, RemoteException; Building Java™ Enterprise Applications Volume I: Architecture 242 public Fund findByName(String name) throws FinderException, RemoteException; } The local home interface for the Fund bean is shown in Example E-17. Example E-17. The FundLocalHome Interface package com.forethought.ejb.fund; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.EJBLocalHome; import javax.ejb.FinderException; public interface FundLocalHome extends EJBLocalHome { public FundLocal create(String name, String description) throws CreateException, EJBException; public FundLocal findByPrimaryKey(Integer fundID) throws FinderException, EJBException; public FundLocal findByName(String name) throws FinderException, EJBException; } Example E-18 is the Fund bean's implementation class. Example E-18. The FundBean Implementation Class package com.forethought.ejb.fund; 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; public abstract class FundBean extends EntityAdapter { public Integer ejbCreate(String name, String description) 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 fundKey = (String)context.lookup( "java:comp/env/constants/FundKey"); Integer id = sequence.getNextValue(fundKey); Building Java™ Enterprise Applications Volume I: Architecture 243 // Set values setId(id); setName(name); setDescription(description); 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 name, String description) { // Empty implementation } public FundInfo getInfo( ) { FundInfo fundInfo = new FundInfo(getId().intValue(), getName(), getDescription()); return fundInfo; } public void setInfo(FundInfo fundInfo) { setName(fundInfo.getName( )); setDescription(fundInfo.getDescription( )); } public abstract Integer getId( ); public abstract void setId(Integer id); public abstract String getName( ); public abstract void setName(String name); public abstract String getDescription( ); public abstract void setDescription(String description); } E.1.5 The Account Bean Example E-19 is the Account bean's remote interface. Example E-19. The Account Remote Interface package com.forethought.ejb.account; import java.rmi.RemoteException; import javax.ejb.EJBObject; // AccountType bean import com.forethought.ejb.accountType.UnknownAccountTypeException; // User bean import com.forethought.ejb.user.User; public interface Account extends EJBObject { public AccountInfo getInfo( ) throws RemoteException; Building Java™ Enterprise Applications Volume I: Architecture 244 public void setInfo(AccountInfo accountInfo) throws RemoteException, UnknownAccountTypeException; public Integer getId( ) throws RemoteException; public User getUser( ) throws RemoteException; public void setUser(User user) throws RemoteException; public String getType( ) throws RemoteException; public void setType(String type) throws RemoteException, UnknownAccountTypeException; public float getBalance( ) throws RemoteException; public void setBalance(float balance) throws RemoteException; } Example E-20 is the local interface for the Account bean, used in CMP relationships. Example E-20. The AccountLocal Interface package com.forethought.ejb.account; import javax.ejb.EJBException; import javax.ejb.EJBLocalObject; // AccountType bean import com.forethought.ejb.accountType.UnknownAccountTypeException; // User bean import com.forethought.ejb.user.UserLocal; public interface AccountLocal extends EJBLocalObject { public Integer getId( ) throws EJBException; public UserLocal getUserLocal( ) throws EJBException; public void setUserLocal(UserLocal userLocal) throws EJBException; public String getType( ) throws EJBException; public void setType(String type) throws EJBException, UnknownAccountTypeException; public float getBalance( ) throws EJBException; public void setBalance(float balance) throws EJBException; } The information map for the Account bean is shown in Example E-21. Example E-21. The AccountInfo Class package com.forethought.ejb.account; import java.io.Serializable; // User bean import com.forethought.ejb.user.UserInfo; Building Java™ Enterprise Applications Volume I: Architecture 245 public class AccountInfo implements Serializable { private int id; private UserInfo userInfo; private String type; private float balance; AccountInfo(int id, String type, float balance, UserInfo userInfo) { this.id = id; this.type = type; this.balance = balance; this.userInfo = userInfo; } public int getId( ) { return id; } public UserInfo getUserInfo( ) { return userInfo; } public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } public String getType( ) { return type; } public void setType(String type) { this.type = type; } public float getBalance( ) { return balance; } public void setBalance(float balance) { this.balance = balance; } } The home interface for the Account bean is shown in Example E-22. Example E-22. The AccountHome Interface package com.forethought.ejb.account; import java.rmi.RemoteException; import java.util.Collection; import javax.ejb.CreateException; import javax.ejb.EJBHome; import javax.ejb.FinderException; // AccountType bean import com.forethought.ejb.accountType.UnknownAccountTypeException; Building Java™ Enterprise Applications Volume I: Architecture 246 // User bean import com.forethought.ejb.user.User; public interface AccountHome extends EJBHome { public Account create(String type, float balance, User user) throws CreateException, RemoteException, UnknownAccountTypeException; public Account findByPrimaryKey(Integer accountID) throws FinderException, RemoteException; public Collection findByBalance(float minBalance, float maxBalance) throws FinderException, RemoteException; } Example E-23 shows the Account bean's local home interface. Example E-23. The AccountLocalHome Interface package com.forethought.ejb.account; import java.util.Collection; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.EJBLocalHome; import javax.ejb.FinderException; // User bean import com.forethought.ejb.user.User; // AccountType bean import com.forethought.ejb.accountType.UnknownAccountTypeException; public interface AccountLocalHome extends EJBLocalHome { public AccountLocal create(String type, float balance, User user) throws CreateException, EJBException, UnknownAccountTypeException; public AccountLocal findByPrimaryKey(Integer accountID) throws FinderException, EJBException; public Collection findByBalance(float minBalance, float maxBalance) throws FinderException, EJBException; } Example E-24 is the implementation class for the Account bean. Example E-24. The AccountBean Implementation Class package com.forethought.ejb.account; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.FinderException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; Building Java™ Enterprise Applications Volume I: Architecture 247 import javax.rmi.PortableRemoteObject; import com.forethought.ejb.util.EntityAdapter; // Sequence bean import com.forethought.ejb.sequence.SequenceException; import com.forethought.ejb.sequence.SequenceLocal; import com.forethought.ejb.sequence.SequenceLocalHome; // AccountType bean import com.forethought.ejb.accountType.AccountTypeLocal; import com.forethought.ejb.accountType.AccountTypeLocalHome; import com.forethought.ejb.accountType.UnknownAccountTypeException; // User bean import com.forethought.ejb.user.User; import com.forethought.ejb.user.UserInfo; import com.forethought.ejb.user.UserLocal; import com.forethought.ejb.user.UserLocalHome; import com.forethought.ejb.user.UserHome; public abstract class AccountBean extends EntityAdapter { public Integer ejbCreate(String type, float balance, User user) throws CreateException, UnknownAccountTypeException { // 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 accountKey = (String)context.lookup("java:comp/env/constants/AccountKey"); Integer id = sequence.getNextValue(accountKey); // Set values setId(id); setBalance(balance); return null; } catch (NamingException e) { throw new CreateException("Could not obtain an " + "InitialContext: " + e.getMessage( )); } catch (SequenceException e) { throw new CreateException("Error getting primary key value: " + e.getMessage( )); } } public void ejbPostCreate(String type, float balance, User user) throws UnknownAccountTypeException { // Handle CMP relationships setType(type); setUser(user); } Building Java™ Enterprise Applications Volume I: Architecture 248 public AccountInfo getInfo( ) throws RemoteException { AccountInfo accountInfo = new AccountInfo(getId().intValue(), getAccountTypeLocal().getType( ), getBalance(), getUser().getInfo( )); return accountInfo; } public void setInfo(AccountInfo accountInfo) throws UnknownAccountTypeException { setType(accountInfo.getType( )); setBalance(accountInfo.getBalance( )); setUser(accountInfo.getUserInfo( )); } public void setType(String type) throws UnknownAccountTypeException { try { Context context = new InitialContext( ); AccountTypeLocalHome accountTypeLocalHome = (AccountTypeLocalHome)context.lookup( "java:comp/env/ejb/AccountTypeLocalHome"); AccountTypeLocal accountTypeLocal = accountTypeLocalHome.findByType(type); setAccountTypeLocal(accountTypeLocal); } catch (NamingException e) { throw new EJBException("Error looking up AccountType bean: " + e.getMessage( )); } catch (FinderException e) { // Couldn't find supplied type throw new UnknownAccountTypeException(type); } } public String getType( ) { return getAccountTypeLocal().getType( ); } public void setUser(User user) { try { // Construct primary key for this user Integer userID = user.getId( ); // Find the local interface for this office Context context = new InitialContext( ); UserLocalHome userLocalHome = (UserLocalHome)context.lookup( "java:comp/env/ejb/UserLocalHome"); UserLocal userLocal = userLocalHome.findByPrimaryKey(userID); setUserLocal(userLocal); } catch (NamingException e) { throw new EJBException("Error looking up User bean: " + e.getMessage( )); } catch (RemoteException e) { throw new EJBException("Error looking up User bean: " + e.getMessage( )); [...]... { 269 Building Java Enterprise Applications Volume I: Architecture Colophon Our look is the result of reader comments, our own experimentation, and feedback from distribution channels Distinctive covers complement our distinctive approach to technical topics, breathing personality and life into potentially dry subjects The animal on the cover of Building Java Enterprise Applications Volume I: Architecture. .. attendees.remove(userInfo); } 263 Building Java Enterprise Applications Volume I: Architecture Example E-35 is the home interface for the Event bean Example E-35 The EventHome Interface package com.forethought.ejb.event; import import import import import import java. rmi.RemoteException; java. util.Collection; java. util.Date; javax.ejb.CreateException; javax.ejb.EJBHome; javax.ejb.FinderException; public... com.forethought.ejb.transaction; import import import import import import java. rmi.RemoteException; java. util.Date; java. util.Collection; javax.ejb.CreateException; javax.ejb.EJBHome; javax.ejb.FinderException; // Account bean import com.forethought.ejb.account.Account; 251 Building Java Enterprise Applications Volume I: Architecture public interface TransactionHome extends EJBHome { public Transaction... home interface for the Investment bean is shown in Example E-31 256 Building Java Enterprise Applications Volume I: Architecture Example E-31 The InvestmentHome Interface package com.forethought.ejb.investment; import import import import import java. rmi.RemoteException; java. util.Collection; javax.ejb.CreateException; javax.ejb.EJBHome; javax.ejb.FinderException; // Account bean import com.forethought.ejb.account.Account;... com.forethought.ejb.event; import import import import import import import import import import import java. rmi.RemoteException; java. util.Collection; java. util.Date; java. util.Iterator; java. util.LinkedList; javax.ejb.CreateException; javax.ejb.EJBException; javax.ejb.FinderException; javax.naming.Context; javax.naming.InitialContext; javax.naming.NamingException; import import import import com.forethought.ejb.sequence.SequenceException;... public abstract void setYield(float yield); 261 Building Java Enterprise Applications Volume I: Architecture E.1.8 The Event Bean Example E-33 is the remote interface for the Event bean Example E-33 The Event Remote Interface package com.forethought.ejb.event; import import import import java. rmi.RemoteException; java. util.Collection; java. util.Date; javax.ejb.EJBObject; public interface Event extends... import java. io.Serializable; java. rmi.RemoteException; java. util.Collection; java. util.Date; java. util.Iterator; java. util.LinkedList; // User bean import com.forethought.ejb.user.User; import com.forethought.ejb.user.UserInfo; public class EventInfo implements Serializable { private private private private int id; String description; Date dateTime; Collection attendees; 262 Building Java Enterprise Applications. .. required SequenceLocalHome home = (SequenceLocalHome) context.lookup( "java: comp/env/ejb/SequenceLocalHome"); SequenceLocal sequence = home.create( ); String transactionKey = (String)context.lookup( "java: comp/env/constants/TransactionKey"); Integer id = sequence.getNextValue(transactionKey); 252 Building Java Enterprise Applications Volume I: Architecture // Set values setId(id); setAmount(amount); setDateTime(dateTime);... setAmount(float amount); 254 Building Java Enterprise Applications Volume I: Architecture } public abstract Date getDateTime( ); public abstract void setDateTime(Date dateTime); E.1.7 The Investment Bean Example E-29 is the Investment bean's remote interface Example E-29 The Investment Remote Interface package com.forethought.ejb.investment; import java. rmi.RemoteException; import javax.ejb.EJBObject; //... Transaction bean's information/value class Example E-26 The TransactionInfo Class package com.forethought.ejb.transaction; import java. io.Serializable; import java. util.Date; // Account bean import com.forethought.ejb.account.AccountInfo; 250 Building Java Enterprise Applications Volume I: Architecture public class TransactionInfo implements Serializable { private private private private int id; AccountInfo . import javax.naming.InitialContext; import javax.naming.NamingException; Building Java Enterprise Applications Volume I: Architecture 247 import javax.rmi.PortableRemoteObject; import com.forethought.ejb.util.EntityAdapter;. import java. io.Serializable; import java. util.Date; // Account bean import com.forethought.ejb.account.AccountInfo; Building Java Enterprise Applications Volume I: Architecture. Fund entity bean. Building Java Enterprise Applications Volume I: Architecture 241 Example E-15. The FundInfo Class package com.forethought.ejb.fund; import java. io.Serializable;

Ngày đăng: 05/08/2014, 10:20

Từ khóa liên quan

Mục lục

  • E. Supplemental Code Listings

    • E.2 Application Exceptions

    • Colophon

Tài liệu cùng người dùng

Tài liệu liên quan