1// ejb-jar.xml: TransferBean.transfer trans-attribute=Required
2// ejb-jar.xml: method-permission role-name=teller, supervisor
3public class TransferBean implements SessionBean {
4 private SessionContext ctx;
5
6 public void transfer(String fromId, String toId, BigDecimal amount)
7 throws InsufficientFundsException, FinderException {
8 AccountLocalHome home = (AccountLocalHome)
9 new InitialContext().lookup("java:comp/env/ejb/Account");
10 AccountLocal from = home.findByPrimaryKey(fromId);
11 AccountLocal to = home.findByPrimaryKey(toId);
12 if (amount.compareTo(new BigDecimal("10000.00")) > 0
13 && !ctx.isCallerInRole("supervisor")) {
14 throw new EJBException("Approval limit exceeded");
15 }
16 if (from.getBalance().compareTo(amount) < 0) {
17 throw new InsufficientFundsException(fromId);
18 }
19 from.setBalance(from.getBalance().subtract(amount));
20 to.setBalance(to.getBalance().add(amount));
21 }
22}