KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > bank > 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.ejb3.test.bank;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.annotation.Resource;
27 import javax.ejb.TimerService JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.transaction.TransactionManager JavaDoc;
30 import org.jboss.logging.Logger;
31
32 /**
33  * @see <related>
34  * @author $Author: bill.burke@jboss.com $
35  * @version $Revision: 58478 $
36  */

37 public class TellerBean implements Teller
38 {
39    private static final Logger log = Logger.getLogger(TellerBean.class);
40
41    @Resource private TimerService JavaDoc ts;
42    private TransactionManager JavaDoc tm;
43    private Bank bank;
44    private boolean constructed = false;
45    private String JavaDoc defaultValue = "original";
46
47    public boolean isConstructed()
48    {
49       return constructed;
50    }
51
52    public void setTransactionManager(TransactionManager JavaDoc tm)
53    {
54       this.tm = tm;
55       System.out.println("TransactionManager set: " + tm);
56    }
57
58    public void transfer(Account from, Account to, float amount)
59          throws BankException
60    {
61       try
62       {
63          from.withdraw(amount);
64          to.deposit(amount);
65       } catch (Exception JavaDoc e)
66       {
67          throw new BankException("Could not transfer " + amount + " from "
68                                  + from + " to " + to, e);
69       }
70    }
71
72    public Account createAccount(Customer customer, float balance)
73          throws BankException
74    {
75       try
76       {
77         Bank bank = (Bank) new InitialContext JavaDoc()
78                .lookup(Bank.JNDI_NAME);
79
80          return null;
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 JavaDoc accounts = customer.getAccounts();
95          if (accounts.size() > 0)
96          {
97             Iterator JavaDoc 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) throws BankException
116    {
117       try
118       {
119          // Check for existing customer
120

121          return null;
122       } catch (Exception JavaDoc e)
123       {
124          log.debug("failed", e);
125          throw new BankException("Could not get customer for " + name, e);
126       }
127    }
128
129    public void transferTest(Account from, Account to, float amount, int iter)
130          throws java.rmi.RemoteException JavaDoc, BankException
131    {
132       for (int i = 0; i < iter; i++)
133       {
134          from.withdraw(amount);
135          to.deposit(amount);
136       }
137    }
138
139    public String JavaDoc greetWithRequiredTransaction(String JavaDoc greeting) throws Exception JavaDoc
140    {
141       if (tm.getTransaction() == null) throw new Exception JavaDoc("method has no tx set");
142       return greeting;
143    }
144
145    public String JavaDoc greetWithNotSupportedTransaction(String JavaDoc greeting) throws Exception JavaDoc
146    {
147       if (tm.getTransaction() != null) throw new Exception JavaDoc("method has tx set");
148       return greeting;
149    }
150
151    public String JavaDoc greetWithServiceTimer(String JavaDoc greeting) throws Exception JavaDoc
152    {
153       if (ts == null) throw new Exception JavaDoc("TimerService @Inject failed");
154       return greeting;
155    }
156
157    public String JavaDoc greetUnchecked(String JavaDoc greeting) throws Exception JavaDoc
158    {
159       if (tm.getTransaction() == null) throw new Exception JavaDoc("method has no tx set");
160       return greeting;
161    }
162
163    public String JavaDoc greetChecked(String JavaDoc greeting) throws Exception JavaDoc
164    {
165       if (tm.getTransaction() == null) throw new Exception JavaDoc("method has no tx set");
166       return greeting;
167    }
168
169    public void storeCustomerId(String JavaDoc customerId) throws Exception JavaDoc
170    {
171       bank.storeCustomerId(customerId);
172    }
173
174    public String JavaDoc retrieveCustomerId() throws Exception JavaDoc
175    {
176       return bank.retrieveCustomerId();
177    }
178
179    public void excludedMethod()
180    {
181
182    }
183
184    public void postConstruct()
185    {
186       constructed = true;
187    }
188
189    public String JavaDoc getDefaultValue()
190    {
191       return defaultValue;
192    }
193    
194    public void testTransactionTimeout()
195    {
196       boolean exceptionCaught = false;
197       try
198       {
199          log.info("************* calling bank.testTransactionTimeout()");
200          bank.testTransactionTimeout();
201          log.info("************* finished calling bank.testTransactionTimeout()");
202       }
203       catch (Exception JavaDoc e)
204       {
205          log.info("********** caught exception");
206          exceptionCaught = true;
207       }
208       if (!exceptionCaught) throw new RuntimeException JavaDoc("Failed to catch transactionTimeout");
209    }
210 }
211
Popular Tags