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