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.jca.bank.ejb;
023:
024: import java.sql.Connection;
025: import java.sql.SQLException;
026: import java.sql.Statement;
027: import java.util.*;
028: import javax.ejb.EJBException;
029: import javax.ejb.SessionBean;
030: import javax.ejb.SessionContext;
031: import javax.naming.InitialContext;
032: import javax.sql.DataSource;
033: import org.apache.log4j.Category;
034: import org.jboss.test.jca.bank.interfaces.Account;
035: import org.jboss.test.jca.bank.interfaces.AccountHome;
036: import org.jboss.test.jca.bank.interfaces.AccountLocal;
037: import org.jboss.test.jca.bank.interfaces.AccountLocalHome;
038:
039: /**
040: * Describe class <code>TellerBean</code> here.
041: * The equals and hashCode methods have been overridden to test bug 595738,
042: * a problem with CachedConnectionManager if it directly puts objects in a hashmap.
043: *
044: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
045: * @version 1.0
046: *
047: * @ejb:bean name="Teller"
048: * jndi-name="Teller"
049: * local-jndi-name="LocalTellerBean"
050: * view-type="both"
051: * type="Stateless"
052: */
053: public class TellerBean implements SessionBean {
054: static int invocations;
055:
056: private Connection c;
057:
058: /**
059: * Describe <code>setUp</code> method here.
060: *
061: * @exception EJBException if an error occurs
062: * @ejb:interface-method
063: */
064: public void setUp() {
065: try {
066: tearDown();
067: } catch (Exception e) {
068: //ignore
069: } // end of try-catch
070:
071: try {
072: Statement s = getConnection().createStatement();
073: s
074: .execute("CREATE TABLE CCBMPCUSTOMER (ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(64))");
075: s
076: .execute("CREATE TABLE CCBMPACCOUNT (ID INTEGER NOT NULL PRIMARY KEY, BALANCE INTEGER NOT NULL, CUSTOMERID INTEGER)");
077: s.close();
078:
079: } catch (Exception e) {
080: throw new EJBException(e);
081: } // end of try-catch
082:
083: }
084:
085: /**
086: * Describe <code>tearDown</code> method here.
087: *
088: * @exception EJBException if an error occurs
089: * @ejb:interface-method
090: */
091: public void tearDown() {
092: try {
093: Statement s = getConnection().createStatement();
094: s.execute("DROP TABLE CCBMPCUSTOMER");
095: s.execute("DROP TABLE CCBMPACCOUNT");
096: s.close();
097:
098: } catch (Exception e) {
099: throw new EJBException(e);
100: } // end of try-catch
101:
102: }
103:
104: /**
105: * This <code>equals</code> method tests whether the CachedConnectionManager deals
106: * properly with ejbs that override equals and hashCode. See bug 595738
107: *
108: * @param other an <code>Object</code> value
109: * @return a <code>boolean</code> value
110: */
111: public boolean equals(Object other) {
112: return other.getClass() == this .getClass();
113: }
114:
115: public int hashCode() {
116: return 1;
117: }
118:
119: /**
120: * Describe <code>transfer</code> method here.
121: *
122: * @param from an <code>Account</code> value
123: * @param to an <code>Account</code> value
124: * @param amount a <code>float</code> value
125: * @exception EJBException if an error occurs
126: * @ejb:interface-method
127: */
128: public void transfer(Account from, Account to, int amount) {
129: try {
130: Category.getInstance(TellerBean.class.getName()).info(
131: "Invocation #" + invocations++);
132: from.withdraw(amount);
133: to.deposit(amount);
134: } catch (Exception e) {
135: throw new EJBException("Could not transfer " + amount
136: + " from " + from + " to " + to, e);
137: }
138: }
139:
140: /**
141: * Describe <code>createAccount</code> method here.
142: *
143: * @param id a <code>Integer</code> value, id of account
144: * @return an <code>Account</code> value
145: * @exception EJBException if an error occurs
146: * @ejb:interface-method
147: */
148: public Account createAccount(Integer id) {
149: try {
150: AccountHome home = (AccountHome) new InitialContext()
151: .lookup("Account");
152: Account acct = home.create(id, 0, null);
153:
154: return acct;
155: } catch (Exception e) {
156: throw new EJBException("Could not create account", e);
157: }
158: }
159:
160: /**
161: * Describe <code>getAccountBalance</code> method here.
162: *
163: * @param id a <code>integer</code> value, id of account
164: * @return an <code>int</code> value, balbance of account
165: * @exception EJBException if an error occurs
166: * @ejb:interface-method
167: */
168: public int getAccountBalance(Integer id) {
169: try {
170: AccountLocalHome home = (AccountLocalHome) new InitialContext()
171: .lookup("AccountLocal");
172: AccountLocal a = home.findByPrimaryKey(id);
173: return a.getBalance();
174: } catch (Exception e) {
175: Category.getInstance(getClass().getName()).info(
176: "getAccountBalance failed", e);
177: throw new EJBException(
178: "Could not get account for id " + id, e);
179: }
180: }
181:
182: /**
183: * Describe <code>transferTest</code> method here.
184: *
185: * @param from an <code>AccountLocal</code> value
186: * @param to an <code>AccountLocal</code> value
187: * @param amount a <code>float</code> value
188: * @param iter an <code>int</code> value
189: * @exception java.rmi.RemoteException if an error occurs
190: * @exception EJBException if an error occurs
191: * @ejb:interface-method
192: */
193: public void transferTest(AccountLocal from, AccountLocal to,
194: int amount, int iter) {
195: for (int i = 0; i < iter; i++) {
196: from.withdraw(amount);
197: to.deposit(amount);
198: }
199: }
200:
201: public void ejbCreate() {
202: }
203:
204: public void ejbActivate() {
205: }
206:
207: public void ejbPassivate() {
208: if (c != null) {
209: try {
210: c.close();
211: } catch (SQLException e) {
212: Category.getInstance(getClass().getName()).info(
213: "SQLException closing c: " + e);
214: } // end of try-catch
215: c = null;
216: } // end of if ()
217: }
218:
219: public void ejbRemove() {
220: }
221:
222: public void setSessionContext(SessionContext ctx) {
223: }
224:
225: public void unsetSessionContext() {
226: }
227:
228: private Connection getConnection() throws Exception {
229: if (c == null) {
230: DataSource ds = (DataSource) new InitialContext()
231: .lookup("java:/DefaultDS");
232: c = ds.getConnection();
233:
234: } // end of if ()
235:
236: return c;
237: }
238: }
|