1 18 package org.jahia.services.cache.treecache; 19 20 import org.jboss.cache.TransactionManagerLookup; 21 import org.jboss.cache.transaction.DummyTransactionManager; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import javax.transaction.TransactionManager ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 29 35 public class TomcatTransactionManagerLookup implements TransactionManagerLookup { 36 37 40 private static Log log= LogFactory.getLog(TomcatTransactionManagerLookup.class); 41 42 45 private static boolean lookupDone=false; 46 47 50 private static boolean lookupFailed=false; 51 52 55 private static TransactionManager tm=null; 56 57 60 private static String [][] knownJNDIManagers={ 61 {"java:comp/env/UserTransaction", "Tomcat"}, 62 {"java:/TransactionManager", "JBoss, JRun4"}, 63 {"java:comp/UserTransaction", "Resin, Orion, JOnAS (JOTM)"}, 64 {"javax.transaction.TransactionManager", "BEA WebLogic"} 65 }; 66 67 72 public TransactionManager getTransactionManager() { 73 if(!lookupDone) 74 doLookups(); 75 if(tm != null) 76 return tm; 77 if(lookupFailed) { 78 tm= DummyTransactionManager.getInstance(); 80 log.warn("Falling back to DummyTransactionManager from JBossCache"); 81 } 82 return tm; 83 } 84 85 88 private static void doLookups() { 89 if(lookupFailed) 90 return; 91 InitialContext ctx; 92 try { 93 ctx=new InitialContext (); 94 } 95 catch(NamingException e) { 96 log.error("Could not create an initial JNDI context!", e); 97 lookupFailed=true; 98 return; 99 } 100 Object jndiObject=null; 102 for(int i=0; i < knownJNDIManagers.length; i++) { 103 try { 104 if(log.isDebugEnabled()) log.debug("Trying to lookup TransactionManager for " + knownJNDIManagers[i][1]); 105 jndiObject=ctx.lookup("java:comp/env/UserTransaction"); 106 } 107 catch(NamingException e) { 108 log.info("Failed to perform a lookup for [" + knownJNDIManagers[i][0] + " (" + knownJNDIManagers[i][1] + ")]"); 109 } 110 if(jndiObject instanceof TransactionManager) { 111 tm=(TransactionManager)jndiObject; 112 log.info("Found TransactionManager for " + knownJNDIManagers[i][1]); 113 return; 114 } 115 } 116 } 117 118 } 119 | Popular Tags |