KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > 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.jca.bank.ejb;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.util.*;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.SessionBean JavaDoc;
30 import javax.ejb.SessionContext JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.sql.DataSource JavaDoc;
33 import org.jboss.logging.Logger;
34 import org.jboss.test.jca.bank.interfaces.Account;
35 import org.jboss.test.jca.bank.interfaces.AccountHome;
36 import org.jboss.test.jca.bank.interfaces.AccountLocal;
37 import org.jboss.test.jca.bank.interfaces.AccountLocalHome;
38
39
40 /**
41  * Describe class <code>TellerBean</code> here.
42  * The equals and hashCode methods have been overridden to test bug 595738,
43  * a problem with CachedConnectionManager if it directly puts objects in a hashmap.
44  *
45  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
46  * @version 1.0
47  *
48  * @ejb:bean name="Teller"
49  * jndi-name="Teller"
50  * local-jndi-name="LocalTellerBean"
51  * view-type="both"
52  * type="Stateless"
53  */

54 public class TellerBean
55    implements SessionBean JavaDoc
56 {
57    static int invocations;
58
59    private Connection JavaDoc c;
60
61    /**
62     * Describe <code>setUp</code> method here.
63     *
64     * @exception EJBException if an error occurs
65     * @ejb:interface-method
66     */

67    public void setUp()
68    {
69       try
70       {
71          tearDown();
72       }
73       catch (Exception JavaDoc e)
74       {
75          //ignore
76
} // end of try-catch
77

78       try
79       {
80          Statement JavaDoc s = getConnection().createStatement();
81          s.execute("CREATE TABLE CCBMPCUSTOMER (ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(64))");
82          s.execute("CREATE TABLE CCBMPACCOUNT (ID INTEGER NOT NULL PRIMARY KEY, BALANCE INTEGER NOT NULL, CUSTOMERID INTEGER)");
83          s.close();
84
85       }
86       catch (Exception JavaDoc e)
87       {
88          throw new EJBException JavaDoc(e);
89       } // end of try-catch
90

91    }
92
93    /**
94     * Describe <code>tearDown</code> method here.
95     *
96     * @exception EJBException if an error occurs
97     * @ejb:interface-method
98     */

99    public void tearDown()
100    {
101       try
102       {
103          Statement JavaDoc s = getConnection().createStatement();
104          s.execute("DROP TABLE CCBMPCUSTOMER");
105          s.execute("DROP TABLE CCBMPACCOUNT");
106          s.close();
107
108       }
109       catch (Exception JavaDoc e)
110       {
111          throw new EJBException JavaDoc(e);
112       } // end of try-catch
113

114    }
115
116    /**
117     * This <code>equals</code> method tests whether the CachedConnectionManager deals
118     * properly with ejbs that override equals and hashCode. See bug 595738
119     *
120     * @param other an <code>Object</code> value
121     * @return a <code>boolean</code> value
122     */

123    public boolean equals(Object JavaDoc other)
124    {
125       return other.getClass() == this.getClass();
126    }
127
128    public int hashCode()
129    {
130       return 1;
131    }
132
133    /**
134     * Describe <code>transfer</code> method here.
135     *
136     * @param from an <code>Account</code> value
137     * @param to an <code>Account</code> value
138     * @param amount a <code>float</code> value
139     * @exception EJBException if an error occurs
140     * @ejb:interface-method
141     */

142    public void transfer(Account from, Account to, int amount)
143    {
144       try
145       {
146          Logger.getLogger(TellerBean.class.getName()).info("Invocation #"+invocations++);
147          from.withdraw(amount);
148          to.deposit(amount);
149       } catch (Exception JavaDoc e)
150       {
151          throw new EJBException JavaDoc("Could not transfer "+amount+" from "+from+" to "+to, e);
152       }
153    }
154
155    /**
156     * Describe <code>createAccount</code> method here.
157     *
158     * @param id a <code>Integer</code> value, id of account
159     * @return an <code>Account</code> value
160     * @exception EJBException if an error occurs
161     * @ejb:interface-method
162     */

163    public Account createAccount(Integer JavaDoc id)
164    {
165       try
166       {
167          AccountHome home = (AccountHome)new InitialContext JavaDoc().lookup("Account");
168          Account acct = home.create(id, 0, null);
169
170          return acct;
171       } catch (Exception JavaDoc e)
172       {
173          throw new EJBException JavaDoc("Could not create account", e);
174       }
175    }
176
177    /**
178     * Describe <code>getAccountBalance</code> method here.
179     *
180     * @param id a <code>integer</code> value, id of account
181     * @return an <code>int</code> value, balbance of account
182     * @exception EJBException if an error occurs
183     * @ejb:interface-method
184     */

185    public int getAccountBalance(Integer JavaDoc id)
186    {
187       try
188       {
189          AccountLocalHome home = (AccountLocalHome)new InitialContext JavaDoc().lookup("AccountLocal");
190          AccountLocal a = home.findByPrimaryKey(id);
191          return a.getBalance();
192       } catch (Exception JavaDoc e)
193       {
194          Logger.getLogger(getClass().getName()).info("getAccountBalance failed", e);
195          throw new EJBException JavaDoc("Could not get account for id " + id, e);
196       }
197    }
198
199
200    /**
201     * Describe <code>transferTest</code> method here.
202     *
203     * @param from an <code>AccountLocal</code> value
204     * @param to an <code>AccountLocal</code> value
205     * @param amount a <code>float</code> value
206     * @param iter an <code>int</code> value
207     * @exception java.rmi.RemoteException if an error occurs
208     * @exception EJBException if an error occurs
209     * @ejb:interface-method
210     */

211    public void transferTest(AccountLocal from, AccountLocal to, int amount, int iter)
212    {
213       for (int i = 0; i < iter; i++)
214       {
215          from.withdraw(amount);
216          to.deposit(amount);
217       }
218    }
219
220    public void ejbCreate()
221    {
222    }
223
224    public void ejbActivate()
225    {
226    }
227
228    public void ejbPassivate()
229    {
230       if (c != null)
231       {
232          try
233          {
234             c.close();
235          }
236          catch (SQLException JavaDoc e)
237          {
238             Logger.getLogger(getClass().getName()).info("SQLException closing c: " + e);
239          } // end of try-catch
240
c = null;
241       } // end of if ()
242
}
243
244    public void ejbRemove()
245    {
246    }
247
248    public void setSessionContext(SessionContext JavaDoc ctx)
249    {
250    }
251
252    public void unsetSessionContext()
253    {
254    }
255
256    private Connection JavaDoc getConnection() throws Exception JavaDoc
257    {
258       if (c == null)
259       {
260          DataSource JavaDoc ds = (DataSource JavaDoc)new InitialContext JavaDoc().lookup("java:/DefaultDS");
261          c = ds.getConnection();
262
263       } // end of if ()
264

265       return c;
266    }
267 }
268
Popular Tags