KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > folder > FolderSY


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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: FolderSY.java,v 1.14 2005/02/09 10:35:33 coqp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.folder;
27
28 import java.io.Serializable JavaDoc;
29 import java.rmi.RemoteException JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.FinderException JavaDoc;
35 import javax.ejb.NoSuchObjectLocalException JavaDoc;
36 import javax.ejb.RemoveException JavaDoc;
37 import javax.ejb.SessionBean JavaDoc;
38 import javax.ejb.SessionSynchronization JavaDoc;
39 import javax.ejb.SessionContext JavaDoc;
40 import javax.ejb.Timer JavaDoc;
41 import javax.ejb.TimerHandle JavaDoc;
42 import javax.naming.Context JavaDoc;
43 import javax.naming.InitialContext JavaDoc;
44 import javax.naming.NamingException JavaDoc;
45 import javax.rmi.PortableRemoteObject JavaDoc;
46 import javax.sql.DataSource JavaDoc;
47 import org.objectweb.jonas.common.Log;
48 import org.objectweb.util.monolog.api.Logger;
49 import org.objectweb.util.monolog.api.BasicLevel;
50
51 /**
52  * FolderSY implementation
53  * This bean is a stateful session bean that implements SessionSynchronization
54  * @author Philippe Durieux, Philippe Coq
55  */

56 public class FolderSY implements SessionBean JavaDoc, SessionSynchronization JavaDoc {
57
58     protected static Logger logger = null;
59     SessionContext JavaDoc ejbContext;
60     InitialContext JavaDoc ictx;
61     Context JavaDoc myEnv;
62     FileHome filehome;
63     boolean rollbackOnly;
64
65     /**
66      * Check environment variables
67      */

68     void checkEnv(String JavaDoc method) {
69
70         // Check directly in my context
71
logger.log(BasicLevel.DEBUG, "Check directly in my context");
72         try {
73             String JavaDoc value = (String JavaDoc) myEnv.lookup("myname");
74             if (!value.equals("mysession")) {
75                 logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: myname=" + value);
76                 throw new EJBException JavaDoc("FolderSY 1: " + method);
77             }
78         } catch (NamingException JavaDoc e) {
79             logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e);
80             throw new EJBException JavaDoc("FolderSY 2: " + method);
81         }
82         // Idem with compound name
83
logger.log(BasicLevel.DEBUG, "Idem with compound name");
84         try {
85             String JavaDoc value = (String JavaDoc) myEnv.lookup("dir1/dir2/name");
86             if (!value.equals("sessionvalue")) {
87                 logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: dir1/dir2/name=" + value);
88                 throw new EJBException JavaDoc("FolderSY 3: " + method);
89             }
90         } catch (NamingException JavaDoc e) {
91             logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e);
92             throw new EJBException JavaDoc("FolderSY 4: " + method);
93         }
94         // Check from initial Context
95
logger.log(BasicLevel.DEBUG, "Check from initial Context");
96         try {
97             String JavaDoc value = (String JavaDoc) ictx.lookup("java:comp/env/myname");
98             if (!value.equals("mysession")) {
99                 logger.log(BasicLevel.ERROR, ": ictx.lookup failed: myname=" + value);
100                 throw new EJBException JavaDoc("FolderSY 6: " + method);
101             }
102         } catch (NamingException JavaDoc e) {
103             logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
104             throw new EJBException JavaDoc("FolderSY 7: " + method);
105         }
106
107         // Check datasource directly
108
logger.log(BasicLevel.DEBUG, "Check datasource directly");
109         DataSource JavaDoc ds1 = null;
110         try {
111             ds1 = (DataSource JavaDoc) ictx.lookup("jdbc_1");
112         } catch (NamingException JavaDoc e) {
113             logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
114             throw new EJBException JavaDoc("FolderSY 8: " + method);
115         }
116         Connection JavaDoc con = null;
117         if (!method.equals("afterCompletion")) {
118             try {
119                 con = (Connection JavaDoc) ds1.getConnection();
120                 if (con.isClosed()) {
121                     logger.log(BasicLevel.ERROR, ": connection is closed");
122                     throw new EJBException JavaDoc("FolderSY 8a: " + method);
123                 }
124                 con.close();
125             } catch (SQLException JavaDoc e) {
126                 logger.log(BasicLevel.ERROR, ": getConnection:\n" + e);
127                 throw new EJBException JavaDoc("FolderSY 8b: " + method);
128             }
129         }
130
131         // Check DataSource from resource ref in bean environment
132
logger.log(BasicLevel.DEBUG, "Check DataSource from resource ref");
133         DataSource JavaDoc ds2 = null;
134         try {
135             // The name is the one defined in FolderSY.xml
136
ds2 = (DataSource JavaDoc) myEnv.lookup("jdbc/mydb");
137         } catch (NamingException JavaDoc e) {
138             logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
139             throw new EJBException JavaDoc("FolderSY 9: " + method);
140         }
141         if (!method.equals("afterCompletion")) {
142             try {
143                 con = (Connection JavaDoc) ds2.getConnection();
144                 if (con.isClosed()) {
145                     logger.log(BasicLevel.ERROR, ": connection is closed");
146                     throw new EJBException JavaDoc("FolderSY 9a: " + method);
147                 }
148                 con.close();
149             } catch (SQLException JavaDoc e) {
150                 logger.log(BasicLevel.ERROR, ": getConnection:\n" + e);
151                 throw new EJBException JavaDoc("FolderSY 9b: " + method);
152             }
153         }
154
155         // Check boolean values
156
logger.log(BasicLevel.DEBUG, "Check boolean values");
157         try {
158             Boolean JavaDoc value = (Boolean JavaDoc) ictx.lookup("java:comp/env/bVrai");
159             if (!value.booleanValue()) {
160                 logger.log(BasicLevel.ERROR, ": ictx.lookup failed: bVrai=" + value);
161                 throw new EJBException JavaDoc("FolderSY 10a: " + method);
162             }
163         } catch (NamingException JavaDoc e) {
164             logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
165             throw new EJBException JavaDoc("FolderSY10a: " + method);
166         } catch (ClassCastException JavaDoc e) {
167             logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
168             throw new EJBException JavaDoc("FolderSY10a1: " + method);
169         }
170         try {
171             Boolean JavaDoc value = (Boolean JavaDoc) ictx.lookup("java:comp/env/bFaux");
172             if (value.booleanValue()) {
173                 logger.log(BasicLevel.ERROR, ": ictx.lookup failed: bFaux=" + value);
174                 throw new EJBException JavaDoc("FolderSY 10b: " + method);
175             }
176             // lookup char in env-entry
177
Character JavaDoc c = (Character JavaDoc) ictx.lookup("java:comp/env/testChar");
178
179         
180         } catch (NamingException JavaDoc e) {
181             logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
182             throw new EJBException JavaDoc("FolderSY10b: " + method);
183         } catch (ClassCastException JavaDoc e) {
184             logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
185             throw new EJBException JavaDoc("FolderSY10b1: " + method);
186         }
187
188         logger.log(BasicLevel.DEBUG, ": checkEnv OK");
189     }
190
191     // ------------------------------------------------------------------
192
// SessionBean implementation
193
// ------------------------------------------------------------------
194

195     public void setSessionContext(SessionContext JavaDoc ctx) {
196         if (logger == null) {
197             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
198         }
199         logger.log(BasicLevel.DEBUG, "");
200         ejbContext = ctx;
201         try {
202             // Get initial Context
203
ictx = new InitialContext JavaDoc();
204             myEnv = (Context JavaDoc) ictx.lookup("java:comp/env");
205             // lookup filehome in JNDI
206
filehome = (FileHome) PortableRemoteObject.narrow(ictx.lookup("java:comp/env/ejb/file"), FileHome.class);
207         } catch (NamingException JavaDoc e) {
208             throw new EJBException JavaDoc("FolderSY: Cannot get filehome:" + e);
209         }
210         checkEnv("setSessionContext");
211     }
212
213     public void ejbRemove() {
214         logger.log(BasicLevel.DEBUG, "");
215         checkEnv("ejbRemove");
216     }
217     public void ejbCreate() throws CreateException JavaDoc {
218         logger.log(BasicLevel.DEBUG, "");
219         checkEnv("ejbCreate");
220         rollbackOnly = false;
221     }
222     public void ejbCreateForRollback() throws CreateException JavaDoc {
223         logger.log(BasicLevel.DEBUG, "");
224         checkEnv("ejbCreate");
225         rollbackOnly = true;
226     }
227     public void ejbPassivate() {
228         logger.log(BasicLevel.DEBUG, "");
229         checkEnv("ejbPassivate");
230     }
231     public void ejbActivate() {
232         logger.log(BasicLevel.DEBUG, "");
233         checkEnv("ejbActivate");
234     }
235
236     // ------------------------------------------------------------------
237
// SessionSynchronization implementation
238
// ------------------------------------------------------------------
239

240     public void afterBegin() {
241         logger.log(BasicLevel.DEBUG, "");
242         checkEnv("afterBegin");
243         try {
244             ejbContext.getRollbackOnly();
245         } catch (IllegalStateException JavaDoc e) {
246             throw new EJBException JavaDoc("Cannot call getRollbackOnly in afterBegin");
247         }
248         if (rollbackOnly) {
249             try {
250                 ejbContext.setRollbackOnly();
251             } catch (IllegalStateException JavaDoc e) {
252                 throw new EJBException JavaDoc("Cannot call setRollbackOnly in afterBegin");
253             }
254         }
255     }
256
257     public void beforeCompletion() {
258         logger.log(BasicLevel.DEBUG, "");
259         checkEnv("beforeCompletion");
260         try {
261             ejbContext.getRollbackOnly();
262         } catch (IllegalStateException JavaDoc e) {
263             throw new EJBException JavaDoc("Cannot call getRollbackOnly in beforeCompletion");
264         }
265         if (rollbackOnly) {
266             try {
267                 ejbContext.setRollbackOnly();
268             } catch (IllegalStateException JavaDoc e) {
269                 throw new EJBException JavaDoc("Cannot call setRollbackOnly in afterBegin");
270             }
271         }
272     }
273
274     public void afterCompletion(boolean committed) {
275         logger.log(BasicLevel.DEBUG, "");
276         checkEnv("afterCompletion");
277         // Should not be able to call set or get RollbackOnly here.
278
try {
279             ejbContext.getRollbackOnly();
280             throw new EJBException JavaDoc("getRollbackOnly forbidden in afterCompletion");
281         } catch (IllegalStateException JavaDoc e) {
282         }
283         if (rollbackOnly) {
284             try {
285                 ejbContext.setRollbackOnly();
286                 throw new EJBException JavaDoc("setRollbackOnly forbidden in afterCompletion");
287             } catch (IllegalStateException JavaDoc e) {
288             }
289         }
290     }
291
292     // ------------------------------------------------------------------
293
// Folder implementation
294
// ------------------------------------------------------------------
295

296     public File newFile(String JavaDoc fname) throws RemoteException JavaDoc, CreateException JavaDoc {
297         logger.log(BasicLevel.DEBUG, "");
298         checkEnv("newFile");
299         File ret = filehome.create(fname);
300         return ret;
301     }
302
303     public File getFile(String JavaDoc fname) throws RemoteException JavaDoc, FinderException JavaDoc {
304         logger.log(BasicLevel.DEBUG, "");
305         checkEnv("getFile");
306         File ret = filehome.findByPrimaryKey(fname);
307         return ret;
308     }
309
310     public boolean testTimerCancel(String JavaDoc fname) throws RemoteException JavaDoc, CreateException JavaDoc {
311         logger.log(BasicLevel.DEBUG, "");
312         boolean ret = true;
313         
314         // Create a bean implementing TimedObject
315
File file = filehome.create(fname);
316         
317         // Create a Timer on this bean and get its Handle
318
TimerHandle JavaDoc th = file.getTimerHandle();
319         if (th == null) {
320             logger.log(BasicLevel.WARN, "Null Handle returned");
321             return false;
322         }
323         
324         // Check that Timer Info can be read and is correct.
325
Timer JavaDoc t = th.getTimer();
326         Serializable JavaDoc sz = t.getInfo();
327         if (!(sz instanceof Integer JavaDoc)) {
328             logger.log(BasicLevel.WARN, "Non-integer info value returned");
329             return false;
330         }
331
332         // Cancel the timer
333
file.cancelTimer(th);
334         
335         // Check that the TimerHandle cannot be used any longer
336
try {
337             th.getTimer();
338             logger.log(BasicLevel.WARN, "Timer still accessible after bean remove");
339             ret = false;
340         } catch (NoSuchObjectLocalException JavaDoc e) {
341         }
342         
343         // Remove the bean
344
try {
345             file.remove();
346         } catch (Exception JavaDoc e) {
347             logger.log(BasicLevel.WARN, "Cannot remove the bean after use.");
348             return false;
349         }
350         return ret;
351     }
352     
353     public boolean testTimerRemoved(String JavaDoc fname) throws RemoteException JavaDoc, CreateException JavaDoc, RemoveException JavaDoc {
354         logger.log(BasicLevel.DEBUG, "");
355         boolean ret = true;
356         
357         // Create a bean implementing TimedObject
358
File file = filehome.create(fname);
359         
360         // Create a Timer on this bean and get its Handle
361
TimerHandle JavaDoc th = file.getTimerHandle();
362         if (th == null) {
363             logger.log(BasicLevel.WARN, "Null Handle returned");
364             return false;
365         }
366         
367         // Check that Timer Info can be read and is correct.
368
Serializable JavaDoc sz = th.getTimer().getInfo();
369         if (!(sz instanceof Integer JavaDoc)) {
370             logger.log(BasicLevel.WARN, "Non-integer info value returned");
371             return false;
372         }
373
374         // Remove the bean (and the timer!)
375
file.remove();
376         
377         // Check that the TimerHandle cannot be used any longer
378
try {
379             th.getTimer();
380             logger.log(BasicLevel.WARN, "Timer still accessible after bean remove");
381             ret = false;
382         } catch (NoSuchObjectLocalException JavaDoc e) {
383         }
384         return ret;
385     }
386     
387     public void sendRef(Folder f) throws RemoteException JavaDoc {
388     }
389
390     public void sendInt(int i) throws RemoteException JavaDoc {
391     }
392
393     public void sendRefTS(Folder f) throws RemoteException JavaDoc {
394     }
395
396     public void sendIntTS(int i) throws RemoteException JavaDoc {
397     }
398
399     public Folder getRef(Folder f) throws RemoteException JavaDoc {
400         return f;
401     }
402
403     public int getInt(int i) throws RemoteException JavaDoc {
404         return i;
405     }
406
407     public Folder getRefTS(Folder f) throws RemoteException JavaDoc {
408         return f;
409     }
410
411     public int getIntTS(int i) throws RemoteException JavaDoc {
412         return i;
413     }
414     
415     public void sendArray(long[] x) throws RemoteException JavaDoc {
416     }
417     
418     public long[] getArray() throws RemoteException JavaDoc {
419         long[] anArray;
420         anArray = new long [100];
421         for (int i = 0; i < anArray.length; i++) {
422             anArray[i] = i;
423         }
424         return anArray;
425     }
426 }
427
Popular Tags