KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bankiiop > 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.bankiiop.ejb;
23
24 import java.util.*;
25
26 import javax.rmi.PortableRemoteObject JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28
29 import org.jboss.test.util.ejb.SessionSupport;
30 import org.jboss.test.bankiiop.interfaces.*;
31
32 import org.jboss.logging.Logger;
33
34
35 /**
36  *
37  * @see <related>
38  * @author $Author: scott.stark@jboss.org $
39  * @version $Revision: 58115 $
40  */

41 public class TellerBean
42    extends SessionSupport
43 {
44    // Constants -----------------------------------------------------
45

46    // Attributes ----------------------------------------------------
47
static int invocations;
48    // Static --------------------------------------------------------
49

50    // Constructors --------------------------------------------------
51

52    // Public --------------------------------------------------------
53
public void transfer(Account from, Account to, float amount)
54       throws BankException
55    {
56       try
57       {
58          Logger.getLogger(TellerBean.class.getName()).info("Invocation #"+invocations++);
59          from.withdraw(amount);
60          to.deposit(amount);
61       } catch (Exception JavaDoc e)
62       {
63          throw new BankException("Could not transfer "+amount+" from "+from+" to "+to, e);
64       }
65    }
66    
67    public Account createAccount(Customer customer, float balance)
68       throws BankException
69    {
70       try
71       {
72          BankHome bankHome = (BankHome)PortableRemoteObject.narrow(
73                                new InitialContext JavaDoc().lookup(BankHome.COMP_NAME),
74                                BankHome.class);
75          Bank bank = bankHome.create();
76          
77          AccountHome home = (AccountHome)PortableRemoteObject.narrow(
78                             new InitialContext JavaDoc().lookup(AccountHome.COMP_NAME),
79                             AccountHome.class);
80          AccountData data = new AccountData();
81          data.setId(bank.createAccountId(customer));
82          data.setBalance(balance);
83          data.setOwner(customer);
84          Account acct = home.create(data);
85          customer.addAccount(acct);
86          
87          return acct;
88       } catch (Exception JavaDoc e)
89       {
90          log.debug("failed", e);
91          throw new BankException("Could not create account", e);
92       }
93    }
94    
95    public Account getAccount(Customer customer, float balance)
96       throws BankException
97    {
98       try
99       {
100          // Check for existing account
101
Collection accounts = customer.getAccounts();
102          if (accounts.size() > 0)
103          {
104             Iterator i = accounts.iterator();
105             Account acct = (Account)PortableRemoteObject.narrow(i.next(),
106                                 Account.class);
107
108             // Set balance
109
acct.withdraw(acct.getBalance()-balance);
110             
111             return acct;
112          } else
113          {
114             // Create account
115
return createAccount(customer, balance);
116          }
117       } catch (Exception JavaDoc e)
118       {
119          log.debug("failed", e);
120          throw new BankException("Could not get account for "+customer, e);
121       }
122    }
123    
124    public Customer getCustomer(String JavaDoc name)
125       throws BankException
126    {
127       try
128       {
129          // Check for existing customer
130
CustomerHome home = (CustomerHome)PortableRemoteObject.narrow(
131                            new InitialContext JavaDoc().lookup(CustomerHome.COMP_NAME),
132                            CustomerHome.class);
133          Collection customers = home.findAll();
134          
135          Iterator i = customers.iterator();
136          while(i.hasNext())
137          {
138             Customer cust =
139            (Customer)PortableRemoteObject.narrow(i.next(),
140                              Customer.class);
141             if (cust.getName().equals(name))
142                return cust;
143             
144          }
145          
146          // Create customer
147
BankHome bankHome = (BankHome)PortableRemoteObject.narrow(
148                                new InitialContext JavaDoc().lookup(BankHome.COMP_NAME),
149                                BankHome.class);
150          Bank bank = bankHome.create();
151          
152          Customer cust = home.create(bank.createCustomerId(), name);
153          log.debug("Customer created");
154          return cust;
155       } catch (Exception JavaDoc e)
156       {
157          log.debug("failed", e);
158          throw new BankException("Could not get customer for "+name, e);
159       }
160    }
161    
162    public void transferTest(Account from, Account to, float amount, int iter)
163       throws java.rmi.RemoteException JavaDoc, BankException
164    {
165       for (int i = 0; i < iter; i++)
166       {
167          from.withdraw(amount);
168          to.deposit(amount);
169       }
170    }
171 }
172 /*
173  * $Id: TellerBean.java 58115 2006-11-04 08:42:14Z scott.stark@jboss.org $
174  * Currently locked by:$Locker$
175  * Revision:
176  * $Log$
177  * Revision 1.4 2005/10/29 23:41:18 starksm
178  * Update the jboss LGPL headers
179  *
180  * Revision 1.3 2005/04/04 21:49:19 ejort
181  * Enum is a keyword in java5
182  *
183  * Revision 1.2 2002/05/27 22:41:49 reverbel
184  * Making the bankiiop test work with the multiple invokers code:
185  * - The test client uses the CosNaming jndi provider.
186  * - Beans use ejb-refs to find each other.
187  * - These refs are properly set up for IIOP (in jboss.xml).
188  *
189  * Revision 1.1 2002/03/15 22:36:28 reverbel
190  * Initial version of the bank test for JBoss/IIOP.
191  *
192  * Revision 1.7 2002/02/16 11:26:57 user57
193  * o System.err, System.out & printStackTrace() 99.9% gone.
194  *
195  * Revision 1.6 2002/02/15 06:15:50 user57
196  * o replaced most System.out usage with Log4j. should really introduce
197  * some base classes to make this mess more maintainable...
198  *
199  * Revision 1.5 2001/08/19 14:45:20 d_jencks
200  * Modified TellerBean to use log4j logging
201  *
202  * Revision 1.4 2001/08/02 15:54:17 mnf999
203  * TestBankTest update with number of threads and the output for visual feedback on console
204  *
205  *
206  * Revision 1.3 2001/01/07 23:14:34 peter
207  * Trying to get JAAS to work within test suite.
208  *
209  * Revision 1.2 2000/09/30 01:00:55 fleury
210  * Updated bank tests to work with new jBoss version
211  *
212  * Revision 1.1.1.1 2000/06/21 15:52:37 oberg
213  * Initial import of jBoss test. This module contains CTS tests, some simple examples, and small bean suites.
214  *
215  *
216  *
217  */

218
Popular Tags