001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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: FolderSY.java 9239 2006-07-26 11:02:48Z coqp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.folder;
027:
028: import java.io.Serializable;
029: import java.rmi.RemoteException;
030: import java.sql.Connection;
031: import java.sql.SQLException;
032: import java.util.Enumeration;
033:
034: import javax.ejb.CreateException;
035: import javax.ejb.EJBException;
036: import javax.ejb.FinderException;
037: import javax.ejb.NoSuchObjectLocalException;
038: import javax.ejb.RemoveException;
039: import javax.ejb.SessionBean;
040: import javax.ejb.SessionSynchronization;
041: import javax.ejb.SessionContext;
042: import javax.ejb.Timer;
043: import javax.ejb.TimerHandle;
044: import javax.naming.Context;
045: import javax.naming.InitialContext;
046: import javax.naming.NamingException;
047: import javax.rmi.PortableRemoteObject;
048: import javax.sql.DataSource;
049: import org.objectweb.jonas.common.Log;
050: import org.objectweb.util.monolog.api.Logger;
051: import org.objectweb.util.monolog.api.BasicLevel;
052:
053: /**
054: * FolderSY implementation
055: * This bean is a stateful session bean that implements SessionSynchronization
056: * @author Philippe Durieux, Philippe Coq
057: */
058: public class FolderSY implements SessionBean, SessionSynchronization {
059:
060: protected static Logger logger = null;
061: SessionContext ejbContext;
062: transient InitialContext ictx;
063: Context myEnv;
064: FileHome filehome;
065: PaperLocalHome paperhome;
066: Paper2LocalHome paper2home;
067: Paper3LocalHome paper3home;
068: boolean rollbackOnly;
069:
070: /**
071: * Check environment variables
072: */
073: void checkEnv(String method) {
074:
075: // Check directly in my context
076: logger.log(BasicLevel.DEBUG, "Check directly in my context");
077: try {
078: String value = (String) myEnv.lookup("myname");
079: if (!value.equals("mysession")) {
080: logger.log(BasicLevel.ERROR,
081: ": myEnv.lookup failed: myname=" + value);
082: throw new EJBException("FolderSY 1: " + method);
083: }
084: } catch (Exception e) {
085: logger.log(BasicLevel.ERROR,
086: ": myEnv.lookup raised exception:\n" + e);
087: throw new EJBException("FolderSY 2: " + method);
088: }
089: // Idem with compound name
090: logger.log(BasicLevel.DEBUG, "Idem with compound name");
091: try {
092: String value = (String) myEnv.lookup("dir1/dir2/name");
093: if (!value.equals("sessionvalue")) {
094: logger.log(BasicLevel.ERROR,
095: ": myEnv.lookup failed: dir1/dir2/name="
096: + value);
097: throw new EJBException("FolderSY 3: " + method);
098: }
099: } catch (Exception e) {
100: logger.log(BasicLevel.ERROR,
101: ": myEnv.lookup raised exception:\n" + e);
102: throw new EJBException("FolderSY 4: " + method);
103: }
104: // Check from initial Context
105: logger.log(BasicLevel.DEBUG, "Check from initial Context");
106: try {
107: String value = (String) ictx.lookup("java:comp/env/myname");
108: if (!value.equals("mysession")) {
109: logger.log(BasicLevel.ERROR,
110: ": ictx.lookup failed: myname=" + value);
111: throw new EJBException("FolderSY 6: " + method);
112: }
113: } catch (Exception e) {
114: logger.log(BasicLevel.ERROR,
115: ": ictx.lookup raised exception:\n" + e);
116: throw new EJBException("FolderSY 7: " + method);
117: }
118:
119: // Check datasource directly
120: logger.log(BasicLevel.DEBUG, "Check datasource directly");
121: DataSource ds1 = null;
122: try {
123: ds1 = (DataSource) ictx.lookup("jdbc_1");
124: } catch (Exception e) {
125: logger.log(BasicLevel.ERROR,
126: ": ictx.lookup raised exception:\n" + e);
127: throw new EJBException("FolderSY 8: " + method);
128: }
129: Connection con = null;
130: if (!method.equals("afterCompletion")) {
131: try {
132: con = (Connection) ds1.getConnection();
133: if (con.isClosed()) {
134: logger.log(BasicLevel.ERROR,
135: ": connection is closed");
136: throw new EJBException("FolderSY 8a: " + method);
137: }
138: con.close();
139: } catch (Exception e) {
140: logger.log(BasicLevel.ERROR, ": getConnection:\n" + e);
141: throw new EJBException("FolderSY 8b: " + method);
142: }
143: }
144:
145: // Check DataSource from resource ref in bean environment
146: logger.log(BasicLevel.DEBUG,
147: "Check DataSource from resource ref");
148: DataSource ds2 = null;
149: try {
150: // The name is the one defined in FolderSY.xml
151: ds2 = (DataSource) myEnv.lookup("jdbc/mydb");
152: } catch (Exception e) {
153: logger.log(BasicLevel.ERROR,
154: ": ictx.lookup raised exception:\n" + e);
155: throw new EJBException("FolderSY 9: " + method);
156: }
157: if (!method.equals("afterCompletion")) {
158: try {
159: con = (Connection) ds2.getConnection();
160: if (con.isClosed()) {
161: logger.log(BasicLevel.ERROR,
162: ": connection is closed");
163: throw new EJBException("FolderSY 9a: " + method);
164: }
165: con.close();
166: } catch (Exception e) {
167: logger.log(BasicLevel.ERROR, ": getConnection:\n" + e);
168: throw new EJBException("FolderSY 9b: " + method);
169: }
170: }
171:
172: // Check boolean values
173: logger.log(BasicLevel.DEBUG, "Check boolean values");
174: try {
175: Boolean value = (Boolean) ictx
176: .lookup("java:comp/env/bVrai");
177: if (!value.booleanValue()) {
178: logger.log(BasicLevel.ERROR,
179: ": ictx.lookup failed: bVrai=" + value);
180: throw new EJBException("FolderSY 10a: " + method);
181: }
182: } catch (Exception e) {
183: logger.log(BasicLevel.ERROR,
184: ": ictx.lookup raised exception:\n" + e);
185: throw new EJBException("FolderSY10a: " + method);
186: }
187: try {
188: Boolean value = (Boolean) ictx
189: .lookup("java:comp/env/bFaux");
190: if (value.booleanValue()) {
191: logger.log(BasicLevel.ERROR,
192: ": ictx.lookup failed: bFaux=" + value);
193: throw new EJBException("FolderSY 10b: " + method);
194: }
195: // lookup char in env-entry
196: Character c = (Character) ictx
197: .lookup("java:comp/env/testChar");
198:
199: } catch (Exception e) {
200: logger.log(BasicLevel.ERROR,
201: ": ictx.lookup raised exception:\n" + e);
202: throw new EJBException("FolderSY10b: " + method);
203: }
204:
205: logger.log(BasicLevel.DEBUG, ": checkEnv OK");
206: }
207:
208: // ------------------------------------------------------------------
209: // SessionBean implementation
210: // ------------------------------------------------------------------
211:
212: public void setSessionContext(SessionContext ctx) {
213: if (logger == null) {
214: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
215: }
216: logger.log(BasicLevel.DEBUG, "");
217: ejbContext = ctx;
218: try {
219: // Get initial Context
220: ictx = new InitialContext();
221: myEnv = (Context) ictx.lookup("java:comp/env");
222: // lookup filehome in JNDI
223: filehome = (FileHome) PortableRemoteObject.narrow(ictx
224: .lookup("java:comp/env/ejb/file"), FileHome.class);
225: paperhome = (PaperLocalHome) PortableRemoteObject.narrow(
226: ictx.lookup("java:comp/env/ejb/paperec2"),
227: PaperLocalHome.class);
228: paper2home = (Paper2LocalHome) PortableRemoteObject.narrow(
229: ictx.lookup("java:comp/env/ejb/paper2"),
230: Paper2LocalHome.class);
231: paper3home = (Paper3LocalHome) PortableRemoteObject.narrow(
232: ictx.lookup("java:comp/env/ejb/paper3"),
233: Paper3LocalHome.class);
234: } catch (NamingException e) {
235: throw new EJBException("FolderSY: Cannot get filehome:" + e);
236: }
237: checkEnv("setSessionContext");
238: }
239:
240: public void ejbRemove() {
241: logger.log(BasicLevel.DEBUG, "");
242: checkEnv("ejbRemove");
243: }
244:
245: public void ejbCreate() throws CreateException {
246: logger.log(BasicLevel.DEBUG, "");
247: checkEnv("ejbCreate");
248: rollbackOnly = false;
249: }
250:
251: public void ejbCreateForRollback() throws CreateException {
252: logger.log(BasicLevel.DEBUG, "");
253: checkEnv("ejbCreate");
254: rollbackOnly = true;
255: }
256:
257: public void ejbPassivate() {
258: logger.log(BasicLevel.DEBUG, "");
259: checkEnv("ejbPassivate");
260: ictx = null;
261: }
262:
263: public void ejbActivate() {
264: logger.log(BasicLevel.DEBUG, "");
265: try {
266: // Get initial Context
267: ictx = new InitialContext();
268: } catch (NamingException e) {
269: throw new EJBException(
270: "FolderSY: Cannot get Initial Context:" + e);
271: }
272: checkEnv("ejbActivate");
273: }
274:
275: // ------------------------------------------------------------------
276: // SessionSynchronization implementation
277: // ------------------------------------------------------------------
278:
279: public void afterBegin() {
280: logger.log(BasicLevel.DEBUG, "");
281: checkEnv("afterBegin");
282: try {
283: ejbContext.getRollbackOnly();
284: } catch (IllegalStateException e) {
285: throw new EJBException(
286: "Cannot call getRollbackOnly in afterBegin");
287: }
288: if (rollbackOnly) {
289: try {
290: ejbContext.setRollbackOnly();
291: } catch (IllegalStateException e) {
292: throw new EJBException(
293: "Cannot call setRollbackOnly in afterBegin");
294: }
295: }
296: }
297:
298: public void beforeCompletion() {
299: logger.log(BasicLevel.DEBUG, "");
300: checkEnv("beforeCompletion");
301: try {
302: ejbContext.getRollbackOnly();
303: } catch (IllegalStateException e) {
304: throw new EJBException(
305: "Cannot call getRollbackOnly in beforeCompletion");
306: }
307: if (rollbackOnly) {
308: try {
309: ejbContext.setRollbackOnly();
310: } catch (IllegalStateException e) {
311: throw new EJBException(
312: "Cannot call setRollbackOnly in afterBegin");
313: }
314: }
315: }
316:
317: public void afterCompletion(boolean committed) {
318: logger.log(BasicLevel.DEBUG, "");
319: checkEnv("afterCompletion");
320: // Should not be able to call set or get RollbackOnly here.
321: try {
322: ejbContext.getRollbackOnly();
323: throw new EJBException(
324: "getRollbackOnly forbidden in afterCompletion");
325: } catch (IllegalStateException e) {
326: }
327: if (rollbackOnly) {
328: try {
329: ejbContext.setRollbackOnly();
330: throw new EJBException(
331: "setRollbackOnly forbidden in afterCompletion");
332: } catch (IllegalStateException e) {
333: }
334: }
335: }
336:
337: // ------------------------------------------------------------------
338: // Folder implementation
339: // ------------------------------------------------------------------
340:
341: public File newFile(String fname) throws RemoteException,
342: CreateException {
343: logger.log(BasicLevel.DEBUG, "");
344: checkEnv("newFile");
345: File ret = filehome.create(fname);
346: return ret;
347: }
348:
349: public File getFile(String fname) throws RemoteException,
350: FinderException {
351: logger.log(BasicLevel.DEBUG, "");
352: checkEnv("getFile");
353: File ret = filehome.findByPrimaryKey(fname);
354: return ret;
355: }
356:
357: public boolean testTimerCancel(String fname)
358: throws RemoteException, CreateException {
359: logger.log(BasicLevel.DEBUG, "");
360: boolean ret = true;
361:
362: // Create a bean implementing TimedObject
363: File file = filehome.create(fname);
364:
365: // Create a Timer on this bean and get its Handle
366: TimerHandle th = file.getTimerHandle();
367: if (th == null) {
368: logger.log(BasicLevel.WARN, "Null Handle returned");
369: return false;
370: }
371:
372: // Check that Timer Info can be read and is correct.
373: Timer t = th.getTimer();
374: Serializable sz = t.getInfo();
375: if (!(sz instanceof Integer)) {
376: logger.log(BasicLevel.WARN,
377: "Non-integer info value returned");
378: return false;
379: }
380:
381: // Cancel the timer
382: file.cancelTimer(th);
383:
384: // Check that the TimerHandle cannot be used any longer
385: try {
386: th.getTimer();
387: logger.log(BasicLevel.WARN,
388: "Timer still accessible after bean remove");
389: ret = false;
390: } catch (NoSuchObjectLocalException e) {
391: }
392:
393: // Remove the bean
394: try {
395: file.remove();
396: } catch (Exception e) {
397: logger.log(BasicLevel.WARN,
398: "Cannot remove the bean after use.");
399: return false;
400: }
401: return ret;
402: }
403:
404: public boolean testTimerRemoved(String fname)
405: throws RemoteException, CreateException, RemoveException {
406: logger.log(BasicLevel.DEBUG, "");
407: boolean ret = true;
408:
409: // Create a bean implementing TimedObject
410: File file = filehome.create(fname);
411:
412: // Create a Timer on this bean and get its Handle
413: TimerHandle th = file.getTimerHandle();
414: if (th == null) {
415: logger.log(BasicLevel.WARN, "Null Handle returned");
416: return false;
417: }
418:
419: // Check that Timer Info can be read and is correct.
420: Serializable sz = th.getTimer().getInfo();
421: if (!(sz instanceof Integer)) {
422: logger.log(BasicLevel.WARN,
423: "Non-integer info value returned");
424: return false;
425: }
426:
427: // Remove the bean (and the timer!)
428: file.remove();
429:
430: // Check that the TimerHandle cannot be used any longer
431: try {
432: th.getTimer();
433: logger.log(BasicLevel.WARN,
434: "Timer still accessible after bean remove");
435: ret = false;
436: } catch (NoSuchObjectLocalException e) {
437: }
438: return ret;
439: }
440:
441: public void sendRef(Folder f) throws RemoteException {
442: }
443:
444: public void sendInt(int i) throws RemoteException {
445: }
446:
447: public void sendRefTS(Folder f) throws RemoteException {
448: }
449:
450: public void sendIntTS(int i) throws RemoteException {
451: }
452:
453: public Folder getRef(Folder f) throws RemoteException {
454: return f;
455: }
456:
457: public int getInt(int i) throws RemoteException {
458: return i;
459: }
460:
461: public Folder getRefTS(Folder f) throws RemoteException {
462: return f;
463: }
464:
465: public int getIntTS(int i) throws RemoteException {
466: return i;
467: }
468:
469: public void sendArray(long[] x) throws RemoteException {
470: }
471:
472: public long[] getArray() throws RemoteException {
473: long[] anArray;
474: anArray = new long[100];
475: for (int i = 0; i < anArray.length; i++) {
476: anArray[i] = i;
477: }
478: return anArray;
479: }
480:
481: public int modify(String pname, int val) throws RemoteException {
482: logger.log(BasicLevel.DEBUG, "");
483: int sum = 0;
484: try {
485: PaperLocal plocal = paperhome.findByPrimaryKey(pname);
486: logger.log(BasicLevel.DEBUG, "findByPrimaryKey OK ");
487: int valb = plocal.getValue();
488: logger.log(BasicLevel.DEBUG, "valb= " + valb);
489: plocal.setValue(val);
490: sum = paperhome.getTotalValeur();
491: logger.log(BasicLevel.DEBUG, "sum= " + sum);
492:
493: } catch (FinderException fe) {
494:
495: } finally {
496: // to clean up the changes
497: ejbContext.setRollbackOnly();
498: }
499: return sum;
500: }
501:
502: public int modifypaper2And3(String pid, int val)
503: throws RemoteException {
504: logger.log(BasicLevel.DEBUG, "");
505: int sum = 0;
506: try {
507: Paper2Local plocal2 = paper2home.findByPrimaryKey(pid);
508: logger.log(BasicLevel.DEBUG, "findByPrimaryKey OK ");
509: int valb = plocal2.getValeur();
510: logger.log(BasicLevel.DEBUG, "valb= " + valb);
511: plocal2.setValeur(val);
512:
513: Paper3Local plocal3 = paper3home.findByPrimaryKey("2");
514: String v1 = plocal3.getValide();
515: if (v1 == "1") {
516: plocal3.setValide("0");
517: } else {
518: plocal3.setValide("1");
519:
520: }
521:
522: sum = paper2home.getSumOfValeurs();
523: logger.log(BasicLevel.DEBUG, "sum= " + sum);
524:
525: } catch (FinderException fe) {
526:
527: } finally {
528: // to clean up the changes
529: ejbContext.setRollbackOnly();
530: }
531: return sum;
532: }
533:
534: public int modifypaper2And3WithFinder(String pid, int val)
535: throws RemoteException {
536: logger.log(BasicLevel.DEBUG, "");
537: int nb = 0;
538: Enumeration eList = null;
539: try {
540: Paper2Local plocal2 = paper2home.findByPrimaryKey(pid);
541: logger.log(BasicLevel.DEBUG, "findByPrimaryKey OK ");
542: int valb = plocal2.getValeur();
543: logger.log(BasicLevel.DEBUG, "valb= " + valb);
544: plocal2.setValeur(val);
545:
546: Paper3Local plocal3 = paper3home.findByPrimaryKey("2");
547: String v1 = plocal3.getValide();
548: if (v1 == "1") {
549: plocal3.setValide("0");
550: } else {
551: plocal3.setValide("1");
552:
553: }
554: Paper2Local paper2local = null;
555: eList = paper2home.findAllValide();
556: while (eList.hasMoreElements()) {
557: paper2local = (Paper2Local) javax.rmi.PortableRemoteObject
558: .narrow(eList.nextElement(), Paper2Local.class);
559: nb++;
560: }
561: logger.log(BasicLevel.DEBUG, "nb= " + nb);
562:
563: } catch (FinderException fe) {
564:
565: } finally {
566: // to clean up the changes
567: ejbContext.setRollbackOnly();
568: }
569: return nb;
570: }
571: }
|