001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ManagerSF.java 4618 2004-04-19 06:39:30Z benoitf $
023: * --------------------------------------------------------------------------
024: */package lb;
025:
026: import java.rmi.RemoteException;
027: import java.sql.Connection;
028: import java.sql.SQLException;
029: import java.sql.Statement;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import javax.ejb.CreateException;
034: import javax.ejb.EJBException;
035: import javax.ejb.FinderException;
036: import javax.ejb.SessionBean;
037: import javax.ejb.SessionContext;
038: import javax.ejb.TransactionRolledbackLocalException;
039: import javax.naming.Context;
040: import javax.naming.InitialContext;
041: import javax.naming.NamingException;
042: import javax.sql.DataSource;
043:
044: /**
045: * Manager Implementation
046: * @author Philippe Durieux
047: */
048: public class ManagerSF implements SessionBean {
049:
050: SessionContext ejbContext;
051:
052: ManacLocalHome manacLocalHome = null;
053:
054: int c1 = 0;
055:
056: int d1 = 0;
057:
058: ManacLocal cred1, deb1;
059:
060: int initialValue = 1000;
061:
062: int value = 10;
063:
064: // ------------------------------------------------------------------
065: // init DataBase for Manac beans
066: // ------------------------------------------------------------------
067: private void initDB() {
068:
069: // Get my DataSource from JNDI
070: DataSource ds = null;
071: InitialContext ictx = null;
072: try {
073: ictx = new InitialContext();
074: } catch (Exception e) {
075: System.out.println("new InitialContext() : " + e);
076: throw new EJBException("Cannot get JNDI InitialContext");
077: }
078: try {
079: ds = (DataSource) ictx.lookup("java:comp/env/jdbc/mydb");
080: } catch (Exception e) {
081: System.out.println("cannot lookup datasource " + e);
082: throw new EJBException("cannot lookup datasource");
083: }
084:
085: // Drop table
086: Connection conn = null;
087: Statement stmt = null;
088: // myTable must be <jdbc-table-name> from jonas-xml file (Manac bean)
089: String myTable = "manacsample";
090: try {
091: conn = ds.getConnection();
092: stmt = conn.createStatement();
093: stmt.execute("drop table " + myTable);
094: stmt.close();
095: } catch (SQLException e) {
096: // The first time, table will not exist.
097: }
098:
099: // Create table.
100: try {
101: stmt = conn.createStatement();
102: stmt
103: .execute("create table "
104: + myTable
105: + "(c_name varchar(30) not null primary key, c_num integer, c_balance integer)");
106: stmt.close();
107: conn.close();
108: } catch (SQLException e) {
109: System.out.println("Exception in createTable : " + e);
110: throw new EJBException("Exception in createTable");
111: }
112: }
113:
114: // ------------------------------------------------------------------
115: // SessionBean implementation
116: // ------------------------------------------------------------------
117:
118: /**
119: * Set the associated session context. The container calls this method after
120: * the instance creation. The enterprise Bean instance should store the
121: * reference to the context object in an instance variable. This method is
122: * called with no transaction context.
123: * @param sessionContext A SessionContext interface for the instance.
124: * @throws EJBException Thrown by the method to indicate a failure caused by
125: * a system-level error.
126: */
127: public void setSessionContext(SessionContext ctx) {
128: ejbContext = ctx;
129: }
130:
131: /**
132: * A container invokes this method before it ends the life of the session
133: * object. This happens as a result of a client's invoking a remove
134: * operation, or when a container decides to terminate the session object
135: * after a timeout. This method is called with no transaction context.
136: * @throws EJBException Thrown by the method to indicate a failure caused by
137: * a system-level error.
138: */
139: public void ejbRemove() {
140: }
141:
142: /**
143: * The Session bean must define 1 or more ejbCreate methods.
144: * @throws CreateException Failure to create a session EJB object.
145: */
146: public void ejbCreate(int ival) throws CreateException {
147:
148: // lookup ManacLocalHome
149: try {
150: Context ictx = new InitialContext();
151: manacLocalHome = (ManacLocalHome) ictx
152: .lookup("java:comp/env/ejb/manac");
153: } catch (NamingException e) {
154: System.out.println("ManagerSF : Cannot get ManacLocalHome:"
155: + e);
156: throw new CreateException("Cannot get ManacLocalHome");
157: }
158:
159: initialValue = ival;
160: }
161:
162: /**
163: * A container invokes this method on an instance before the instance
164: * becomes disassociated with a specific EJB object.
165: */
166: public void ejbPassivate() {
167: }
168:
169: /**
170: * A container invokes this method when the instance is taken out of the
171: * pool of available instances to become associated with a specific EJB
172: * object.
173: */
174: public void ejbActivate() {
175: }
176:
177: // ------------------------------------------------------------------
178: // Manager implementation
179: // ------------------------------------------------------------------
180:
181: public void createAll(int nb) throws RemoteException {
182:
183: // init database for Manac bean
184: initDB();
185:
186: // create accounts
187: for (int i = 0; i < nb; i++) {
188: try {
189: manacLocalHome.createWithDefaultName(i, initialValue);
190: } catch (CreateException e) {
191: System.out.println("createAll:\n" + e);
192: throw new RemoteException("Cannot create Manac");
193: }
194: }
195: }
196:
197: public void setAccounts(int d1, int c1) throws RemoteException {
198: this .d1 = d1;
199: this .c1 = c1;
200: try {
201: deb1 = manacLocalHome.findByNum(d1);
202: cred1 = manacLocalHome.findByNum(c1);
203: } catch (FinderException e) {
204: System.out.println("Cannot find manac bean:" + e);
205: throw new RemoteException("Cannot find manac bean");
206: }
207: }
208:
209: public void setValue(int v) throws RemoteException {
210: this .value = v;
211: }
212:
213: public void movement() throws RemoteException {
214:
215: // credit accounts first because we don't want a rollback if
216: // same account is debited and credited in the same operation.
217: try {
218: cred1.credit(value);
219: } catch (EJBException e) {
220: System.out.println("ManagerSF: Cannot credit account:" + e);
221: throw new RemoteException(
222: "ManagerSF: Cannot credit account");
223: }
224:
225: // debit accounts
226: try {
227: deb1.debit(value);
228: } catch (TransactionRolledbackLocalException e) {
229: System.out.println("ManagerSF: Rollback transaction");
230: } catch (EJBException e) {
231: System.out.println("ManagerSF debit:" + e);
232: }
233: }
234:
235: public boolean checkAccount(int a) throws RemoteException {
236:
237: boolean ret = false;
238: ManacLocal m = null;
239:
240: try {
241: m = manacLocalHome.findByNum(a);
242: int b = m.getBalance();
243: if (b >= 0) {
244: ret = true;
245: } else {
246: System.out
247: .println("ManagerSF checkAccount: bad balance="
248: + b);
249: }
250: return ret;
251: } catch (Exception e) {
252: System.out
253: .println("ManagerSF checkAccount: cannot check account: "
254: + e);
255: return false;
256: }
257: }
258:
259: public boolean checkAll() throws RemoteException {
260:
261: int count = 0;
262: int total = 0;
263: try {
264: Collection accCol = manacLocalHome.findAll();
265: for (Iterator accIter = accCol.iterator(); accIter
266: .hasNext();) {
267: count++;
268: ManacLocal a = (ManacLocal) accIter.next();
269: int balance = a.getBalance();
270: if (balance < 0) {
271: System.out
272: .println("checkAllAccounts: bad balance: "
273: + balance);
274: return false;
275: }
276: String name = a.getName();
277: total += balance;
278: }
279: } catch (Exception e) {
280: System.out.println("checkAllAccounts:" + e);
281: return false;
282: }
283: int exp = initialValue * count;
284: if (total != exp) {
285: System.out.println("checkAllAccounts: bad total: " + total
286: + " (expected: " + exp + ")");
287: return false;
288: }
289: return true;
290: }
291:
292: }
|