1 25 26 package org.objectweb.easybeans.component.jotm; 27 28 import java.rmi.RemoteException ; 29 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 import javax.transaction.SystemException ; 33 import javax.transaction.TransactionManager ; 34 35 import org.objectweb.easybeans.component.api.EZBComponentException; 36 import org.objectweb.easybeans.component.itf.TMComponent; 37 import org.objectweb.easybeans.log.JLog; 38 import org.objectweb.easybeans.log.JLogFactory; 39 import org.objectweb.jotm.Current; 40 import org.objectweb.jotm.TimerManager; 41 import org.objectweb.jotm.TransactionFactory; 42 import org.objectweb.jotm.TransactionFactoryImpl; 43 44 48 public class JOTMComponent implements TMComponent { 49 50 53 private static final int DEFAULT_TIMEOUT = 60; 54 55 58 private static JLog logger = JLogFactory.getLog(JOTMComponent.class); 59 60 63 private TransactionManager transactionManager = null; 64 65 68 private int timeout = DEFAULT_TIMEOUT; 69 70 74 public void init() throws EZBComponentException { 75 76 } 77 78 82 public void start() throws EZBComponentException { 83 84 TransactionFactory transactionFactory; 86 try { 87 transactionFactory = new TransactionFactoryImpl(); 88 } catch (RemoteException e) { 89 throw new EZBComponentException("Cannot create transaction factory", e); 90 } 91 92 try { 94 new InitialContext ().rebind("TMFactory", transactionFactory); 95 } catch (NamingException e) { 96 throw new EZBComponentException("Cannot bind transaction factory", e); 97 } 98 99 transactionManager = new Current(transactionFactory); 100 try { 101 transactionManager.setTransactionTimeout(this.timeout); 102 } catch (SystemException se) { 103 throw new EZBComponentException("Cannot set Transaction Timeout", se); 104 } 105 try { 106 new InitialContext ().rebind(JNDI_NAME, transactionManager); 107 } catch (NamingException e) { 108 throw new EZBComponentException("Cannot bind user transaction", e); 109 } 110 111 logger.info("Register {0} as transaction manager object", JNDI_NAME); 113 } 114 115 120 public void stop() throws EZBComponentException { 121 try { 123 new InitialContext ().unbind("TMFactory"); 124 } catch (NamingException e) { 125 throw new EZBComponentException("Cannot unbind transaction factory", e); 126 } 127 128 try { 130 new InitialContext ().unbind(JNDI_NAME); 131 } catch (NamingException e) { 132 throw new EZBComponentException("Cannot unbind user transaction", e); 133 } 134 135 TimerManager.stop(true); 137 } 138 139 143 public TransactionManager getTransactionManager() { 144 return transactionManager; 145 } 146 147 151 public void setTimeout(final int timeout) { 152 this.timeout = timeout; 153 } 154 } 155 | Popular Tags |