package org.jboss.test.bankiiop.ejb;
import java.util.*;
import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
import org.jboss.test.util.ejb.SessionSupport;
import org.jboss.test.bankiiop.interfaces.*;
import org.apache.log4j.Category;
public class TellerBean
extends SessionSupport
{
static int invocations;
public void transfer(Account from, Account to, float amount)
throws BankException
{
try
{
Category.getInstance(TellerBean.class.getName()).info("Invocation #"+invocations++);
from.withdraw(amount);
to.deposit(amount);
} catch (Exception e)
{
throw new BankException("Could not transfer "+amount+" from "+from+" to "+to, e);
}
}
public Account createAccount(Customer customer, float balance)
throws BankException
{
try
{
BankHome bankHome = (BankHome)PortableRemoteObject.narrow(
new InitialContext().lookup(BankHome.COMP_NAME),
BankHome.class);
Bank bank = bankHome.create();
AccountHome home = (AccountHome)PortableRemoteObject.narrow(
new InitialContext().lookup(AccountHome.COMP_NAME),
AccountHome.class);
AccountData data = new AccountData();
data.setId(bank.createAccountId(customer));
data.setBalance(balance);
data.setOwner(customer);
Account acct = home.create(data);
customer.addAccount(acct);
return acct;
} catch (Exception e)
{
log.debug("failed", e);
throw new BankException("Could not create account", e);
}
}
public Account getAccount(Customer customer, float balance)
throws BankException
{
try
{
Collection accounts = customer.getAccounts();
if (accounts.size() > 0)
{
Iterator enum = accounts.iterator();
Account acct = (Account)PortableRemoteObject.narrow(enum.next(),
Account.class);
acct.withdraw(acct.getBalance()-balance);
return acct;
} else
{
return createAccount(customer, balance);
}
} catch (Exception e)
{
log.debug("failed", e);
throw new BankException("Could not get account for "+customer, e);
}
}
public Customer getCustomer(String name)
throws BankException
{
try
{
CustomerHome home = (CustomerHome)PortableRemoteObject.narrow(
new InitialContext().lookup(CustomerHome.COMP_NAME),
CustomerHome.class);
Collection customers = home.findAll();
Iterator enum = customers.iterator();
while(enum.hasNext())
{
Customer cust =
(Customer)PortableRemoteObject.narrow(enum.next(),
Customer.class);
if (cust.getName().equals(name))
return cust;
}
BankHome bankHome = (BankHome)PortableRemoteObject.narrow(
new InitialContext().lookup(BankHome.COMP_NAME),
BankHome.class);
Bank bank = bankHome.create();
Customer cust = home.create(bank.createCustomerId(), name);
log.debug("Customer created");
return cust;
} catch (Exception e)
{
log.debug("failed", e);
throw new BankException("Could not get customer for "+name, e);
}
}
public void transferTest(Account from, Account to, float amount, int iter)
throws java.rmi.RemoteException, BankException
{
for (int i = 0; i < iter; i++)
{
from.withdraw(amount);
to.deposit(amount);
}
}
}