1 package org.jboss.cache; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.jboss.cache.transaction.DummyTransactionManager; 6 7 import javax.naming.InitialContext ; 8 import javax.naming.NamingException ; 9 import javax.transaction.TransactionManager ; 10 import java.lang.reflect.Method ; 11 12 18 public class GenericTransactionManagerLookup implements TransactionManagerLookup { 19 20 23 private static Log log=LogFactory.getLog(GenericTransactionManagerLookup.class); 24 25 28 private static boolean lookupDone=false; 29 30 33 private static boolean lookupFailed=false; 34 35 38 private static TransactionManager tm=null; 39 40 43 private static String [][] knownJNDIManagers={ 44 {"java:/TransactionManager", "JBoss, JRun4"}, 45 {"java:comp/UserTransaction", "Resin, Orion, JOnAS (JOTM)"}, 46 {"javax.transaction.TransactionManager", "BEA WebLogic"} 47 }; 48 49 52 private static final String WS_FACTORY_CLASS_5_1="com.ibm.ws.Transaction.TransactionManagerFactory"; 53 54 57 private static final String WS_FACTORY_CLASS_5_0="com.ibm.ejs.jts.jta.TransactionManagerFactory"; 58 59 62 private static final String WS_FACTORY_CLASS_4="com.ibm.ejs.jts.jta.JTSXA"; 63 64 69 public TransactionManager getTransactionManager() { 70 if(!lookupDone) 71 doLookups(); 72 if(tm != null) 73 return tm; 74 if(lookupFailed) { 75 tm=DummyTransactionManager.getInstance(); 77 log.warn("Falling back to DummyTransactionManager from JBossCache"); 78 } 79 return tm; 80 } 81 82 83 86 private static void doLookups() { 87 if(lookupFailed) 88 return; 89 InitialContext ctx; 90 try { 91 ctx=new InitialContext (); 92 } 93 catch(NamingException e) { 94 log.error("Could not create an initial JNDI context!", e); 95 lookupFailed=true; 96 return; 97 } 98 Object jndiObject=null; 100 for(int i=0; i < knownJNDIManagers.length; i++) { 101 try { 102 if(log.isDebugEnabled()) log.debug("Trying to lookup TransactionManager for " + knownJNDIManagers[i][1]); 103 jndiObject=ctx.lookup(knownJNDIManagers[i][0]); 104 } 105 catch(NamingException e) { 106 log.info("Failed to perform a lookup for [" + knownJNDIManagers[i][0] + " (" + knownJNDIManagers[i][1] + ")]"); 107 } 108 if(jndiObject instanceof TransactionManager) { 109 tm=(TransactionManager)jndiObject; 110 log.info("Found TransactionManager for " + knownJNDIManagers[i][1]); 111 return; 112 } 113 } 114 Class clazz; 116 try { 117 log.debug("Trying WebSphere 5.1: " + WS_FACTORY_CLASS_5_1); 118 clazz=Class.forName(WS_FACTORY_CLASS_5_1); 119 log.info("Found WebSphere 5.1: " + WS_FACTORY_CLASS_5_1); 120 } 121 catch(ClassNotFoundException ex) { 122 try { 123 log.debug("Trying WebSphere 5.0: " + WS_FACTORY_CLASS_5_0); 124 clazz=Class.forName(WS_FACTORY_CLASS_5_0); 125 log.info("Found WebSphere 5.0: " + WS_FACTORY_CLASS_5_0); 126 } 127 catch(ClassNotFoundException ex2) { 128 try { 129 log.debug("Trying WebSphere 4: " + WS_FACTORY_CLASS_4); 130 clazz=Class.forName(WS_FACTORY_CLASS_4); 131 log.info("Found WebSphere 4: " + WS_FACTORY_CLASS_4); 132 } 133 catch(ClassNotFoundException ex3) { 134 log.info("Couldn't find any WebSphere TransactionManager factory class, neither for WebSphere version 5.1 nor 5.0 nor 4"); 135 lookupFailed=true; 136 return; 137 } 138 } 139 } 140 try { 141 Class [] signature=null; 142 Object [] args=null; 143 Method method=clazz.getMethod("getTransactionManager", signature); 144 tm=(TransactionManager)method.invoke(null, args); 145 } 146 catch(Exception ex) { 147 log.error("Found WebSphere TransactionManager factory class [" + clazz.getName() + 148 "], but couldn't invoke its static 'getTransactionManager' method", ex); 149 } 150 } 151 152 } 153 | Popular Tags |