1 22 package org.jboss.test.bank.ejb; 23 24 import java.util.*; 25 26 import javax.naming.InitialContext ; 27 28 import org.jboss.test.util.ejb.SessionSupport; 29 import org.jboss.test.bank.interfaces.*; 30 31 32 38 public class TellerBean 39 extends SessionSupport 40 { 41 43 static int invocations; 45 47 49 public void transfer(Account from, Account to, float amount) 51 throws BankException 52 { 53 try 54 { 55 log.debug("Invocation #"+invocations++); 56 from.withdraw(amount); 57 to.deposit(amount); 58 } catch (Exception e) 59 { 60 throw new BankException("Could not transfer "+amount+" from "+from+" to "+to, e); 61 } 62 } 63 64 public Account createAccount(Customer customer, float balance) 65 throws BankException 66 { 67 try 68 { 69 BankHome bankHome = (BankHome)new InitialContext ().lookup(BankHome.COMP_NAME); 70 Bank bank = bankHome.create(); 71 72 AccountHome home = (AccountHome)new InitialContext ().lookup(AccountHome.COMP_NAME); 73 AccountData data = new AccountData(); 74 data.setId(bank.createAccountId(customer)); 75 data.setBalance(balance); 76 data.setOwner(customer); 77 Account acct = home.create(data); 78 customer.addAccount(acct); 79 80 return acct; 81 } catch (Exception e) 82 { 83 log.debug("failed", e); 84 throw new BankException("Could not create account", e); 85 } 86 } 87 88 public Account getAccount(Customer customer, float balance) 89 throws BankException 90 { 91 try 92 { 93 Collection accounts = customer.getAccounts(); 95 if (accounts.size() > 0) 96 { 97 Iterator i = accounts.iterator(); 98 Account acct = (Account)i.next(); 99 acct.withdraw(acct.getBalance()-balance); 101 102 return acct; 103 } else 104 { 105 return createAccount(customer, balance); 107 } 108 } catch (Exception e) 109 { 110 log.debug("failed", e); 111 throw new BankException("Could not get account for "+customer, e); 112 } 113 } 114 115 public Customer getCustomer(String name) 116 throws BankException 117 { 118 try 119 { 120 CustomerHome home = (CustomerHome)new InitialContext ().lookup(CustomerHome.COMP_NAME); 122 Collection customers = home.findAll(); 123 124 Iterator i = customers.iterator(); 125 while(i.hasNext()) 126 { 127 Customer cust = (Customer)i.next(); 128 if (cust.getName().equals(name)) 129 return cust; 130 131 } 132 133 BankHome bankHome = (BankHome)new InitialContext ().lookup(BankHome.COMP_NAME); 135 Bank bank = bankHome.create(); 136 137 Customer cust = home.create(bank.createCustomerId(), name); 138 log.debug("Customer created"); 139 return cust; 140 } catch (Exception e) 141 { 142 log.debug("failed", e); 143 throw new BankException("Could not get customer for "+name, e); 144 } 145 } 146 147 public void transferTest(Account from, Account to, float amount, int iter) 148 throws java.rmi.RemoteException , BankException 149 { 150 for (int i = 0; i < iter; i++) 151 { 152 from.withdraw(amount); 153 to.deposit(amount); 154 } 155 } 156 } 157 | Popular Tags |