001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.bank.ejb;
023:
024: import java.util.*;
025:
026: import javax.naming.InitialContext;
027:
028: import org.jboss.test.util.ejb.SessionSupport;
029: import org.jboss.test.bank.interfaces.*;
030:
031: /**
032: *
033: * @author Rickard Oberg
034: * @author $Author: dimitris@jboss.org $
035: * @version $Revision: 57211 $
036: */
037: public class TellerBean extends SessionSupport {
038: // Constants -----------------------------------------------------
039:
040: // Attributes ----------------------------------------------------
041: static int invocations;
042:
043: // Static --------------------------------------------------------
044:
045: // Constructors --------------------------------------------------
046:
047: // Public --------------------------------------------------------
048: public void transfer(Account from, Account to, float amount)
049: throws BankException {
050: try {
051: log.debug("Invocation #" + invocations++);
052: from.withdraw(amount);
053: to.deposit(amount);
054: } catch (Exception e) {
055: throw new BankException("Could not transfer " + amount
056: + " from " + from + " to " + to, e);
057: }
058: }
059:
060: public Account createAccount(Customer customer, float balance)
061: throws BankException {
062: try {
063: BankHome bankHome = (BankHome) new InitialContext()
064: .lookup(BankHome.COMP_NAME);
065: Bank bank = bankHome.create();
066:
067: AccountHome home = (AccountHome) new InitialContext()
068: .lookup(AccountHome.COMP_NAME);
069: AccountData data = new AccountData();
070: data.setId(bank.createAccountId(customer));
071: data.setBalance(balance);
072: data.setOwner(customer);
073: Account acct = home.create(data);
074: customer.addAccount(acct);
075:
076: return acct;
077: } catch (Exception e) {
078: log.debug("failed", e);
079: throw new BankException("Could not create account", e);
080: }
081: }
082:
083: public Account getAccount(Customer customer, float balance)
084: throws BankException {
085: try {
086: // Check for existing account
087: Collection accounts = customer.getAccounts();
088: if (accounts.size() > 0) {
089: Iterator i = accounts.iterator();
090: Account acct = (Account) i.next();
091: // Set balance
092: acct.withdraw(acct.getBalance() - balance);
093:
094: return acct;
095: } else {
096: // Create account
097: return createAccount(customer, balance);
098: }
099: } catch (Exception e) {
100: log.debug("failed", e);
101: throw new BankException("Could not get account for "
102: + customer, e);
103: }
104: }
105:
106: public Customer getCustomer(String name) throws BankException {
107: try {
108: // Check for existing customer
109: CustomerHome home = (CustomerHome) new InitialContext()
110: .lookup(CustomerHome.COMP_NAME);
111: Collection customers = home.findAll();
112:
113: Iterator i = customers.iterator();
114: while (i.hasNext()) {
115: Customer cust = (Customer) i.next();
116: if (cust.getName().equals(name))
117: return cust;
118:
119: }
120:
121: // Create customer
122: BankHome bankHome = (BankHome) new InitialContext()
123: .lookup(BankHome.COMP_NAME);
124: Bank bank = bankHome.create();
125:
126: Customer cust = home.create(bank.createCustomerId(), name);
127: log.debug("Customer created");
128: return cust;
129: } catch (Exception e) {
130: log.debug("failed", e);
131: throw new BankException("Could not get customer for "
132: + name, e);
133: }
134: }
135:
136: public void transferTest(Account from, Account to, float amount,
137: int iter) throws java.rmi.RemoteException, BankException {
138: for (int i = 0; i < iter; i++) {
139: from.withdraw(amount);
140: to.deposit(amount);
141: }
142: }
143: }
|