1 22 package org.jboss.tm; 23 24 import javax.naming.InitialContext ; 25 import javax.naming.NamingException ; 26 import javax.transaction.TransactionManager ; 27 28 import org.jboss.logging.Logger; 29 import org.jboss.util.NestedRuntimeException; 30 31 39 public class TransactionManagerLocator 40 { 41 42 private static final Logger log = Logger.getLogger(TransactionManagerLocator.class); 43 44 45 private static TransactionManagerLocator instance = new TransactionManagerLocator(); 46 47 48 private TransactionManager tm; 49 50 53 private TransactionManagerLocator() 54 { 55 } 56 57 62 public static TransactionManagerLocator getInstance() 63 { 64 return instance; 65 } 66 67 72 public TransactionManager locate() 73 { 74 if (tm != null) 75 return tm; 76 77 TransactionManager result = tryJNDI(); 78 if (result == null) 79 result = usePrivateAPI(); 80 if (result == null) 81 throw new NestedRuntimeException("Unable to locate the transaction manager"); 82 83 return result; 84 } 85 86 91 protected TransactionManager tryJNDI() 92 { 93 try 94 { 95 InitialContext ctx = new InitialContext (); 96 tm = (TransactionManager ) ctx.lookup(TransactionManagerService.JNDI_NAME); 97 if (log.isTraceEnabled()) 98 log.trace("Got a transaction manager from jndi " + tm); 99 } 100 catch (NamingException e) 101 { 102 log.trace("Unable to lookup: " + TransactionManagerService.JNDI_NAME, e); 103 } 104 return tm; 105 } 106 107 114 protected TransactionManager usePrivateAPI() 115 { 116 log.trace("Using the JBoss transaction manager"); 117 return TxManager.getInstance(); 118 } 119 } 120 | Popular Tags |