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.bankiiop.ejb;
023:
024: import java.util.*;
025:
026: import javax.rmi.PortableRemoteObject;
027: import javax.naming.InitialContext;
028:
029: import org.jboss.test.util.ejb.SessionSupport;
030: import org.jboss.test.bankiiop.interfaces.*;
031:
032: import org.apache.log4j.Category;
033:
034: /**
035: *
036: * @author Rickard Oberg
037: * @author $Author: dimitris@jboss.org $
038: * @version $Revision: 57211 $
039: */
040: public class TellerBean extends SessionSupport {
041: // Constants -----------------------------------------------------
042:
043: // Attributes ----------------------------------------------------
044: static int invocations;
045:
046: // Static --------------------------------------------------------
047:
048: // Constructors --------------------------------------------------
049:
050: // Public --------------------------------------------------------
051: public void transfer(Account from, Account to, float amount)
052: throws BankException {
053: try {
054: Category.getInstance(TellerBean.class.getName()).info(
055: "Invocation #" + invocations++);
056: from.withdraw(amount);
057: to.deposit(amount);
058: } catch (Exception e) {
059: throw new BankException("Could not transfer " + amount
060: + " from " + from + " to " + to, e);
061: }
062: }
063:
064: public Account createAccount(Customer customer, float balance)
065: throws BankException {
066: try {
067: BankHome bankHome = (BankHome) PortableRemoteObject.narrow(
068: new InitialContext().lookup(BankHome.COMP_NAME),
069: BankHome.class);
070: Bank bank = bankHome.create();
071:
072: AccountHome home = (AccountHome) PortableRemoteObject
073: .narrow(new InitialContext()
074: .lookup(AccountHome.COMP_NAME),
075: AccountHome.class);
076: AccountData data = new AccountData();
077: data.setId(bank.createAccountId(customer));
078: data.setBalance(balance);
079: data.setOwner(customer);
080: Account acct = home.create(data);
081: customer.addAccount(acct);
082:
083: return acct;
084: } catch (Exception e) {
085: log.debug("failed", e);
086: throw new BankException("Could not create account", e);
087: }
088: }
089:
090: public Account getAccount(Customer customer, float balance)
091: throws BankException {
092: try {
093: // Check for existing account
094: Collection accounts = customer.getAccounts();
095: if (accounts.size() > 0) {
096: Iterator i = accounts.iterator();
097: Account acct = (Account) PortableRemoteObject.narrow(i
098: .next(), Account.class);
099:
100: // Set balance
101: acct.withdraw(acct.getBalance() - balance);
102:
103: return acct;
104: } else {
105: // Create account
106: return createAccount(customer, balance);
107: }
108: } catch (Exception e) {
109: log.debug("failed", e);
110: throw new BankException("Could not get account for "
111: + customer, e);
112: }
113: }
114:
115: public Customer getCustomer(String name) throws BankException {
116: try {
117: // Check for existing customer
118: CustomerHome home = (CustomerHome) PortableRemoteObject
119: .narrow(new InitialContext()
120: .lookup(CustomerHome.COMP_NAME),
121: CustomerHome.class);
122: Collection customers = home.findAll();
123:
124: Iterator i = customers.iterator();
125: while (i.hasNext()) {
126: Customer cust = (Customer) PortableRemoteObject.narrow(
127: i.next(), Customer.class);
128: if (cust.getName().equals(name))
129: return cust;
130:
131: }
132:
133: // Create customer
134: BankHome bankHome = (BankHome) PortableRemoteObject.narrow(
135: new InitialContext().lookup(BankHome.COMP_NAME),
136: BankHome.class);
137: Bank bank = bankHome.create();
138:
139: Customer cust = home.create(bank.createCustomerId(), name);
140: log.debug("Customer created");
141: return cust;
142: } catch (Exception e) {
143: log.debug("failed", e);
144: throw new BankException("Could not get customer for "
145: + name, e);
146: }
147: }
148:
149: public void transferTest(Account from, Account to, float amount,
150: int iter) throws java.rmi.RemoteException, BankException {
151: for (int i = 0; i < iter; i++) {
152: from.withdraw(amount);
153: to.deposit(amount);
154: }
155: }
156: }
157: /*
158: * $Id: TellerBean.java 57211 2006-09-26 12:39:46Z dimitris@jboss.org $
159: * Currently locked by:$Locker$
160: * Revision:
161: * $Log$
162: * Revision 1.2.16.3 2005/10/29 05:04:35 starksm
163: * Update the LGPL header
164: *
165: * Revision 1.2.16.2 2005/05/18 00:16:04 adrian
166: * Fixes for java5 compilation
167: *
168: * Revision 1.2.16.1 2005/04/06 16:28:04 starksm
169: * Fix the license header
170: *
171: * Revision 1.2 2002/05/27 22:41:49 reverbel
172: * Making the bankiiop test work with the multiple invokers code:
173: * - The test client uses the CosNaming jndi provider.
174: * - Beans use ejb-refs to find each other.
175: * - These refs are properly set up for IIOP (in jboss.xml).
176: *
177: * Revision 1.1 2002/03/15 22:36:28 reverbel
178: * Initial version of the bank test for JBoss/IIOP.
179: *
180: * Revision 1.7 2002/02/16 11:26:57 user57
181: * o System.err, System.out & printStackTrace() 99.9% gone.
182: *
183: * Revision 1.6 2002/02/15 06:15:50 user57
184: * o replaced most System.out usage with Log4j. should really introduce
185: * some base classes to make this mess more maintainable...
186: *
187: * Revision 1.5 2001/08/19 14:45:20 d_jencks
188: * Modified TellerBean to use log4j logging
189: *
190: * Revision 1.4 2001/08/02 15:54:17 mnf999
191: * TestBankTest update with number of threads and the output for visual feedback on console
192: *
193: *
194: * Revision 1.3 2001/01/07 23:14:34 peter
195: * Trying to get JAAS to work within test suite.
196: *
197: * Revision 1.2 2000/09/30 01:00:55 fleury
198: * Updated bank tests to work with new jBoss version
199: *
200: * Revision 1.1.1.1 2000/06/21 15:52:37 oberg
201: * Initial import of jBoss test. This module contains CTS tests, some simple examples, and small bean suites.
202: *
203: *
204: *
205: */
|