1 25 26 package org.objectweb.jonas.jtests.util; 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectOutputStream ; 32 33 import javax.ejb.EJBException ; 34 import javax.ejb.Timer ; 35 import javax.ejb.TimerHandle ; 36 import javax.transaction.Status ; 37 import javax.transaction.Transaction ; 38 import javax.transaction.TransactionManager ; 39 import javax.transaction.UserTransaction ; 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 50 public class JBean { 51 52 55 public Transaction getCurrentTransaction() { 56 try { 57 TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService(); 58 TransactionManager tm = ts.getTransactionManager(); 59 return tm.getTransaction(); 60 } catch (Exception e) { 61 throw new EJBException ("Cannot get the current transaction"); 62 } 63 } 64 65 public TimerHandle getDeserializedHandle(TimerHandle handle) { 66 TimerHandle newhandle = null; 67 try { 68 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 69 ObjectOutputStream os = new ObjectOutputStream (baos); 70 os.writeObject(handle); 71 byte[] b = baos.toByteArray(); 72 ByteArrayInputStream bais = new ByteArrayInputStream (b); 73 ObjectInputStream is = new ObjectInputStream (bais); 74 newhandle = (TimerHandle ) is.readObject(); 75 } catch (Exception e) { 76 throw new EJBException ("Bad deserialized timer handle:" + e); 77 } 78 return newhandle; 79 } 80 81 public static boolean timersAreIdentical(TimerHandle th1, TimerHandle th2) { 82 Timer 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 e) { 95 throw new EJBException ("Cannot compare 2 timers:" + e); 96 } 97 } 98 99 103 public boolean isAssociated() { 104 int ret; 105 try { 106 TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService(); 108 UserTransaction ut = ts.getUserTransaction(); 109 ret = ut.getStatus(); 110 } catch (Exception 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 124 public void sleep(int msec) { 125 try { 126 Thread.sleep(msec); 127 } catch (InterruptedException e) { 128 System.err.println("sleep interrupted"); 129 } 130 } 131 132 } | Popular Tags |