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: JBean.java 5649 2004-10-20 10:03:11Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.util;
027:
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032:
033: import javax.ejb.EJBException;
034: import javax.ejb.Timer;
035: import javax.ejb.TimerHandle;
036: import javax.transaction.Status;
037: import javax.transaction.Transaction;
038: import javax.transaction.TransactionManager;
039: import javax.transaction.UserTransaction;
040:
041: import org.objectweb.jonas.jtm.TransactionService;
042: import org.objectweb.jonas.service.ServiceManager;
043: import org.objectweb.util.monolog.api.BasicLevel;
044:
045: /**
046: * This class can be extended in every bean of the jonas test suite. It provide
047: * some utilities to look transaction status, ... Most of them are not a clean
048: * and standard way to do this, but it is OK for jonas tests.
049: */
050: public class JBean {
051:
052: /**
053: * @return current Transaction
054: */
055: public Transaction getCurrentTransaction() {
056: try {
057: TransactionService ts = (TransactionService) ServiceManager
058: .getInstance().getTransactionService();
059: TransactionManager tm = ts.getTransactionManager();
060: return tm.getTransaction();
061: } catch (Exception e) {
062: throw new EJBException("Cannot get the current transaction");
063: }
064: }
065:
066: public TimerHandle getDeserializedHandle(TimerHandle handle) {
067: TimerHandle newhandle = null;
068: try {
069: ByteArrayOutputStream baos = new ByteArrayOutputStream();
070: ObjectOutputStream os = new ObjectOutputStream(baos);
071: os.writeObject(handle);
072: byte[] b = baos.toByteArray();
073: ByteArrayInputStream bais = new ByteArrayInputStream(b);
074: ObjectInputStream is = new ObjectInputStream(bais);
075: newhandle = (TimerHandle) is.readObject();
076: } catch (Exception e) {
077: throw new EJBException("Bad deserialized timer handle:" + e);
078: }
079: return newhandle;
080: }
081:
082: public static boolean timersAreIdentical(TimerHandle th1,
083: TimerHandle th2) {
084: Timer timer1, timer2;
085:
086: try {
087: timer1 = th1.getTimer();
088: if (timer1 == null) {
089: return false;
090: }
091: timer2 = th2.getTimer();
092: if (timer2 == null) {
093: return false;
094: }
095: return timer1.equals(timer2);
096: } catch (Exception e) {
097: throw new EJBException("Cannot compare 2 timers:" + e);
098: }
099: }
100:
101: /**
102: * Utility method that returns true if the thread is associated to a
103: * transaction
104: */
105: public boolean isAssociated() {
106: int ret;
107: try {
108: // Get UserTransaction.
109: TransactionService ts = (TransactionService) ServiceManager
110: .getInstance().getTransactionService();
111: UserTransaction ut = ts.getUserTransaction();
112: ret = ut.getStatus();
113: } catch (Exception e) {
114: System.err.println("isAssociated: " + e);
115: return false;
116: }
117: if (ret == Status.STATUS_UNKNOWN
118: || ret == Status.STATUS_NO_TRANSACTION) {
119: return false;
120: } else {
121: return true;
122: }
123: }
124:
125: /**
126: * sleep n millisec.
127: */
128: public void sleep(int msec) {
129: try {
130: Thread.sleep(msec);
131: } catch (InterruptedException e) {
132: System.err.println("sleep interrupted");
133: }
134: }
135:
136: }
|