KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bank > ejb > TellerBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.bank.ejb;
23
24 import java.util.*;
25
26 import javax.naming.InitialContext JavaDoc;
27
28 import org.jboss.test.util.ejb.SessionSupport;
29 import org.jboss.test.bank.interfaces.*;
30
31
32 /**
33  *
34  * @see <related>
35  * @author $Author: starksm $
36  * @version $Revision: 41967 $
37  */

38 public class TellerBean
39    extends SessionSupport
40 {
41    // Constants -----------------------------------------------------
42

43    // Attributes ----------------------------------------------------
44
static int invocations;
45    // Static --------------------------------------------------------
46

47    // Constructors --------------------------------------------------
48

49    // Public --------------------------------------------------------
50
public void transfer(Account from, Account to, float amount)
51       throws BankException
52    {
53       try
54       {
55          log.debug("Invocation #"+invocations++);
56          from.withdraw(amount);
57          to.deposit(amount);
58       } catch (Exception JavaDoc e)
59       {
60          throw new BankException("Could not transfer "+amount+" from "+from+" to "+to, e);
61       }
62    }
63
64    public Account createAccount(Customer customer, float balance)
65       throws BankException
66    {
67       try
68       {
69          BankHome bankHome = (BankHome)new InitialContext JavaDoc().lookup(BankHome.COMP_NAME);
70          Bank bank = bankHome.create();
71
72          AccountHome home = (AccountHome)new InitialContext JavaDoc().lookup(AccountHome.COMP_NAME);
73          AccountData data = new AccountData();
74          data.setId(bank.createAccountId(customer));
75          data.setBalance(balance);
76          data.setOwner(customer);
77          Account acct = home.create(data);
78        customer.addAccount(acct);
79
80          return acct;
81       } catch (Exception JavaDoc 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          // Check for existing account
94
Collection accounts = customer.getAccounts();
95          if (accounts.size() > 0)
96          {
97             Iterator i = accounts.iterator();
98             Account acct = (Account)i.next();
99             // Set balance
100
acct.withdraw(acct.getBalance()-balance);
101
102             return acct;
103          } else
104          {
105             // Create account
106
return createAccount(customer, balance);
107          }
108       } catch (Exception JavaDoc 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 JavaDoc name)
116       throws BankException
117    {
118       try
119       {
120          // Check for existing customer
121
CustomerHome home = (CustomerHome)new InitialContext JavaDoc().lookup(CustomerHome.COMP_NAME);
122          Collection customers = home.findAll();
123
124          Iterator i = customers.iterator();
125          while(i.hasNext())
126          {
127             Customer cust = (Customer)i.next();
128             if (cust.getName().equals(name))
129                return cust;
130
131          }
132
133          // Create customer
134
BankHome bankHome = (BankHome)new InitialContext JavaDoc().lookup(BankHome.COMP_NAME);
135          Bank bank = bankHome.create();
136
137          Customer cust = home.create(bank.createCustomerId(), name);
138          log.debug("Customer created");
139          return cust;
140       } catch (Exception JavaDoc e)
141       {
142          log.debug("failed", e);
143          throw new BankException("Could not get customer for "+name, e);
144       }
145    }
146
147    public void transferTest(Account from, Account to, float amount, int iter)
148       throws java.rmi.RemoteException JavaDoc, BankException
149    {
150       for (int i = 0; i < iter; i++)
151       {
152          from.withdraw(amount);
153          to.deposit(amount);
154       }
155    }
156 }
157
Popular Tags