KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > util > JBean


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: JBean.java,v 1.2 2004/10/20 10:03:11 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.util;
27
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.Timer JavaDoc;
35 import javax.ejb.TimerHandle JavaDoc;
36 import javax.transaction.Status JavaDoc;
37 import javax.transaction.Transaction JavaDoc;
38 import javax.transaction.TransactionManager JavaDoc;
39 import javax.transaction.UserTransaction JavaDoc;
40
41 import org.objectweb.jonas.jtm.TransactionService;
42 import org.objectweb.jonas.service.ServiceManager;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 /**
46  * This class can be extended in every bean of the jonas test suite. It provide
47  * some utilities to look transaction status, ... Most of them are not a clean
48  * and standard way to do this, but it is OK for jonas tests.
49  */

50 public class JBean {
51
52     /**
53      * @return current Transaction
54      */

55     public Transaction JavaDoc getCurrentTransaction() {
56         try {
57             TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService();
58             TransactionManager JavaDoc tm = ts.getTransactionManager();
59             return tm.getTransaction();
60         } catch (Exception JavaDoc e) {
61             throw new EJBException JavaDoc("Cannot get the current transaction");
62         }
63     }
64     
65     public TimerHandle JavaDoc getDeserializedHandle(TimerHandle JavaDoc handle) {
66         TimerHandle JavaDoc newhandle = null;
67         try {
68             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
69             ObjectOutputStream JavaDoc os = new ObjectOutputStream JavaDoc(baos);
70             os.writeObject(handle);
71             byte[] b = baos.toByteArray();
72             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(b);
73             ObjectInputStream JavaDoc is = new ObjectInputStream JavaDoc(bais);
74             newhandle = (TimerHandle JavaDoc) is.readObject();
75         } catch (Exception JavaDoc e) {
76             throw new EJBException JavaDoc("Bad deserialized timer handle:" + e);
77         }
78         return newhandle;
79     }
80
81     public static boolean timersAreIdentical(TimerHandle JavaDoc th1, TimerHandle JavaDoc th2) {
82         Timer JavaDoc timer1, timer2;
83
84         try {
85             timer1 = th1.getTimer();
86             if (timer1 == null) {
87                 return false;
88             }
89             timer2 = th2.getTimer();
90             if (timer2 == null) {
91                 return false;
92             }
93             return timer1.equals(timer2);
94         } catch (Exception JavaDoc e) {
95             throw new EJBException JavaDoc("Cannot compare 2 timers:" + e);
96         }
97     }
98
99     /**
100      * Utility method that returns true if the thread is associated to a
101      * transaction
102      */

103     public boolean isAssociated() {
104         int ret;
105         try {
106             // Get UserTransaction.
107
TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService();
108             UserTransaction JavaDoc ut = ts.getUserTransaction();
109             ret = ut.getStatus();
110         } catch (Exception JavaDoc e) {
111             System.err.println("isAssociated: " + e);
112             return false;
113         }
114         if (ret == Status.STATUS_UNKNOWN || ret == Status.STATUS_NO_TRANSACTION) {
115             return false;
116         } else {
117             return true;
118         }
119     }
120
121     /**
122      * sleep n millisec.
123      */

124     public void sleep(int msec) {
125         try {
126             Thread.sleep(msec);
127         } catch (InterruptedException JavaDoc e) {
128             System.err.println("sleep interrupted");
129         }
130     }
131
132 }
Popular Tags