KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > lb > ManagerSF


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ManagerSF.java,v 1.2 2004/04/19 06:39:29 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package lb;
27
28 import java.rmi.RemoteException;
29 import java.sql.Connection;
30 import java.sql.SQLException;
31 import java.sql.Statement;
32 import java.util.Collection;
33 import java.util.Iterator;
34
35 import javax.ejb.CreateException;
36 import javax.ejb.EJBException;
37 import javax.ejb.FinderException;
38 import javax.ejb.SessionBean;
39 import javax.ejb.SessionContext;
40 import javax.ejb.TransactionRolledbackLocalException;
41 import javax.naming.Context;
42 import javax.naming.InitialContext;
43 import javax.naming.NamingException;
44 import javax.sql.DataSource;
45
46 /**
47  * Manager Implementation
48  * @author Philippe Durieux
49  */

50 public class ManagerSF implements SessionBean {
51
52     SessionContext ejbContext;
53
54     ManacLocalHome manacLocalHome = null;
55
56     int c1 = 0;
57
58     int d1 = 0;
59
60     ManacLocal cred1, deb1;
61
62     int initialValue = 1000;
63
64     int value = 10;
65
66     // ------------------------------------------------------------------
67
// init DataBase for Manac beans
68
// ------------------------------------------------------------------
69
private void initDB() {
70
71         // Get my DataSource from JNDI
72
DataSource ds = null;
73         InitialContext ictx = null;
74         try {
75             ictx = new InitialContext();
76         } catch (Exception e) {
77             System.out.println("new InitialContext() : " + e);
78             throw new EJBException("Cannot get JNDI InitialContext");
79         }
80         try {
81             ds = (DataSource) ictx.lookup("java:comp/env/jdbc/mydb");
82         } catch (Exception e) {
83             System.out.println("cannot lookup datasource " + e);
84             throw new EJBException("cannot lookup datasource");
85         }
86
87         // Drop table
88
Connection conn = null;
89         Statement stmt = null;
90         // myTable must be <jdbc-table-name> from jonas-xml file (Manac bean)
91
String myTable = "manacsample";
92         try {
93             conn = ds.getConnection();
94             stmt = conn.createStatement();
95             stmt.execute("drop table " + myTable);
96             stmt.close();
97         } catch (SQLException e) {
98             // The first time, table will not exist.
99
}
100
101         // Create table.
102
try {
103             stmt = conn.createStatement();
104             stmt.execute("create table " + 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.lookup("java:comp/env/ejb/manac");
152         } catch (NamingException e) {
153             System.out.println("ManagerSF : Cannot get ManacLocalHome:" + e);
154             throw new CreateException("Cannot get ManacLocalHome");
155         }
156
157         initialValue = ival;
158     }
159
160     /**
161      * A container invokes this method on an instance before the instance
162      * becomes disassociated with a specific EJB object.
163      */

164     public void ejbPassivate() {
165     }
166
167     /**
168      * A container invokes this method when the instance is taken out of the
169      * pool of available instances to become associated with a specific EJB
170      * object.
171      */

172     public void ejbActivate() {
173     }
174
175     // ------------------------------------------------------------------
176
// Manager implementation
177
// ------------------------------------------------------------------
178

179     public void createAll(int nb) throws RemoteException {
180
181         // init database for Manac bean
182
initDB();
183
184         // create accounts
185
for (int i = 0; i < nb; i++) {
186             try {
187                 manacLocalHome.createWithDefaultName(i, initialValue);
188             } catch (CreateException e) {
189                 System.out.println("createAll:\n" + e);
190                 throw new RemoteException("Cannot create Manac");
191             }
192         }
193     }
194
195     public void setAccounts(int d1, int c1) throws RemoteException {
196         this.d1 = d1;
197         this.c1 = c1;
198         try {
199             deb1 = manacLocalHome.findByNum(d1);
200             cred1 = manacLocalHome.findByNum(c1);
201         } catch (FinderException e) {
202             System.out.println("Cannot find manac bean:" + e);
203             throw new RemoteException("Cannot find manac bean");
204         }
205     }
206
207     public void setValue(int v) throws RemoteException {
208         this.value = v;
209     }
210
211     public void movement() throws RemoteException {
212
213         // credit accounts first because we don't want a rollback if
214
// same account is debited and credited in the same operation.
215
try {
216             cred1.credit(value);
217         } catch (EJBException e) {
218             System.out.println("ManagerSF: Cannot credit account:" + e);
219             throw new RemoteException("ManagerSF: Cannot credit account");
220         }
221
222         // debit accounts
223
try {
224             deb1.debit(value);
225         } catch (TransactionRolledbackLocalException e) {
226             System.out.println("ManagerSF: Rollback transaction");
227         } catch (EJBException e) {
228             System.out.println("ManagerSF debit:" + e);
229         }
230     }
231
232     public boolean checkAccount(int a) throws RemoteException {
233
234         boolean ret = false;
235         ManacLocal m = null;
236
237         try {
238             m = manacLocalHome.findByNum(a);
239             int b = m.getBalance();
240             if (b >= 0) {
241                 ret = true;
242             } else {
243                 System.out.println("ManagerSF checkAccount: bad balance=" + b);
244             }
245             return ret;
246         } catch (Exception e) {
247             System.out.println("ManagerSF checkAccount: cannot check account: " + e);
248             return false;
249         }
250     }
251
252     public boolean checkAll() throws RemoteException {
253
254         int count = 0;
255         int total = 0;
256         try {
257             Collection accCol = manacLocalHome.findAll();
258             for (Iterator accIter = accCol.iterator(); accIter.hasNext();) {
259                 count++;
260                 ManacLocal a = (ManacLocal) accIter.next();
261                 int balance = a.getBalance();
262                 if (balance < 0) {
263                     System.out.println("checkAllAccounts: bad balance: " + balance);
264                     return false;
265                 }
266                 String name = a.getName();
267                 total += balance;
268             }
269         } catch (Exception e) {
270             System.out.println("checkAllAccounts:" + e);
271             return false;
272         }
273         int exp = initialValue * count;
274         if (total != exp) {
275             System.out.println("checkAllAccounts: bad total: " + total + " (expected: " + exp + ")");
276             return false;
277         }
278         return true;
279     }
280
281 }
Popular Tags