TransferFundsCommand.java
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:

Java编程

开发平台:

Java

  1. package examples.command;
  2. import examples.dualpersistent.AccountHome;
  3. import examples.dualpersistent.Account;
  4. import examples.dualpersistent.ProcessingErrorException;
  5. import javax.naming.InitialContext;
  6. import javax.naming.NamingException;
  7. import javax.rmi.PortableRemoteObject;
  8. import javax.ejb.FinderException;
  9. import java.rmi.RemoteException;
  10. import java.io.Serializable;
  11. public class TransferFundsCommand extends Command implements Serializable {
  12.     String withdrawAccountID;
  13.     String depositAccountID;
  14.     double transferAmount;
  15.     double withdrawAccountBalance;
  16.     double depositAccountBalance;
  17.     public void setWithdrawAccountID(String withdrawAccountID) {
  18.         this.withdrawAccountID = withdrawAccountID;
  19.     }
  20.     public void setDepositAccountID(String depositAccountID) {
  21.         this.depositAccountID = depositAccountID;
  22.     }
  23.     public void setTransferAmount(double transferAmount) {
  24.         this.transferAmount = transferAmount;
  25.     }
  26.     public double getDepositAccountBalance() {
  27.         return depositAccountBalance;
  28.     }
  29.     public double getWithdrawAccountBalance() {
  30.         return withdrawAccountBalance;
  31.     }
  32.     public void execute() throws CommandException
  33.     {
  34.         //at this point we are inside the EJB Server
  35.         try {
  36.             InitialContext ctx = new InitialContext();
  37.             AccountHome home = (AccountHome) PortableRemoteObject.narrow(ctx.lookup("dualPersistent"), AccountHome.class);
  38.             //locate accounts and perform transfer
  39.             Account account1 = home.findByPrimaryKey(withdrawAccountID);
  40.             Account account2 = home.findByPrimaryKey(depositAccountID);
  41.             account1.withdraw(this.transferAmount);
  42.             account2.deposit(this.transferAmount);
  43.             //populate command with final balances
  44.             this.depositAccountBalance = account2.balance();
  45.             this.withdrawAccountBalance = account1.balance();
  46.         }
  47.         catch (Exception e)
  48.         {
  49.             //wrap the exception as a command execption and throw to client
  50.             throw new CommandException(e);
  51.         }
  52.     }
  53.     public TransferFundsCommand()
  54.     {
  55.     }
  56. }