1 22 package org.jboss.ejb3.test.bank; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import javax.annotation.Resource; 27 import javax.ejb.TimerService ; 28 import javax.naming.InitialContext ; 29 import javax.transaction.TransactionManager ; 30 import org.jboss.logging.Logger; 31 32 37 public class TellerBean implements Teller 38 { 39 private static final Logger log = Logger.getLogger(TellerBean.class); 40 41 @Resource private TimerService ts; 42 private TransactionManager tm; 43 private Bank bank; 44 private boolean constructed = false; 45 private String defaultValue = "original"; 46 47 public boolean isConstructed() 48 { 49 return constructed; 50 } 51 52 public void setTransactionManager(TransactionManager tm) 53 { 54 this.tm = tm; 55 System.out.println("TransactionManager set: " + tm); 56 } 57 58 public void transfer(Account from, Account to, float amount) 59 throws BankException 60 { 61 try 62 { 63 from.withdraw(amount); 64 to.deposit(amount); 65 } catch (Exception e) 66 { 67 throw new BankException("Could not transfer " + amount + " from " 68 + from + " to " + to, e); 69 } 70 } 71 72 public Account createAccount(Customer customer, float balance) 73 throws BankException 74 { 75 try 76 { 77 Bank bank = (Bank) new InitialContext () 78 .lookup(Bank.JNDI_NAME); 79 80 return null; 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) throws BankException 116 { 117 try 118 { 119 121 return null; 122 } catch (Exception e) 123 { 124 log.debug("failed", e); 125 throw new BankException("Could not get customer for " + name, e); 126 } 127 } 128 129 public void transferTest(Account from, Account to, float amount, int iter) 130 throws java.rmi.RemoteException , BankException 131 { 132 for (int i = 0; i < iter; i++) 133 { 134 from.withdraw(amount); 135 to.deposit(amount); 136 } 137 } 138 139 public String greetWithRequiredTransaction(String greeting) throws Exception 140 { 141 if (tm.getTransaction() == null) throw new Exception ("method has no tx set"); 142 return greeting; 143 } 144 145 public String greetWithNotSupportedTransaction(String greeting) throws Exception 146 { 147 if (tm.getTransaction() != null) throw new Exception ("method has tx set"); 148 return greeting; 149 } 150 151 public String greetWithServiceTimer(String greeting) throws Exception 152 { 153 if (ts == null) throw new Exception ("TimerService @Inject failed"); 154 return greeting; 155 } 156 157 public String greetUnchecked(String greeting) throws Exception 158 { 159 if (tm.getTransaction() == null) throw new Exception ("method has no tx set"); 160 return greeting; 161 } 162 163 public String greetChecked(String greeting) throws Exception 164 { 165 if (tm.getTransaction() == null) throw new Exception ("method has no tx set"); 166 return greeting; 167 } 168 169 public void storeCustomerId(String customerId) throws Exception 170 { 171 bank.storeCustomerId(customerId); 172 } 173 174 public String retrieveCustomerId() throws Exception 175 { 176 return bank.retrieveCustomerId(); 177 } 178 179 public void excludedMethod() 180 { 181 182 } 183 184 public void postConstruct() 185 { 186 constructed = true; 187 } 188 189 public String getDefaultValue() 190 { 191 return defaultValue; 192 } 193 194 public void testTransactionTimeout() 195 { 196 boolean exceptionCaught = false; 197 try 198 { 199 log.info("************* calling bank.testTransactionTimeout()"); 200 bank.testTransactionTimeout(); 201 log.info("************* finished calling bank.testTransactionTimeout()"); 202 } 203 catch (Exception e) 204 { 205 log.info("********** caught exception"); 206 exceptionCaught = true; 207 } 208 if (!exceptionCaught) throw new RuntimeException ("Failed to catch transactionTimeout"); 209 } 210 } 211 | Popular Tags |