Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 185   Methods: 2
NCLOC: 120   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
GenericTransactionManagerLookup.java 66.7% 74% 100% 73.4%
coverage coverage
 1    package org.jboss.cache.transaction;
 2   
 3    import org.apache.commons.logging.Log;
 4    import org.apache.commons.logging.LogFactory;
 5   
 6    import javax.naming.InitialContext;
 7    import javax.naming.NamingException;
 8    import javax.transaction.TransactionManager;
 9    import java.lang.reflect.Method;
 10   
 11    /**
 12    * A transaction manager lookup class that attempts to locate a TransactionManager.
 13    * A variety of different classes and JNDI locations are tried, for servers
 14    * such as:
 15    * <ul>
 16    * <li> JBoss
 17    * <li> JRun4
 18    * <li> Resin
 19    * <li> Orion
 20    * <li> JOnAS
 21    * <li> BEA Weblogic
 22    * <li> Websphere 4.0, 5.0, 5.1, 6.0
 23    * <li> Sun, Glassfish
 24    * </ul>
 25    * If a transaction manager is not found, returns a {@link DummyTransactionManager}.
 26    *
 27    * @author Markus Plesser
 28    * @version $Id: GenericTransactionManagerLookup.java,v 1.3 2007/06/26 19:03:01 bstansberry Exp $
 29    */
 30    public class GenericTransactionManagerLookup implements TransactionManagerLookup
 31    {
 32   
 33    private static Log log = LogFactory.getLog(GenericTransactionManagerLookup.class);
 34   
 35    /**
 36    * JNDI lookups performed?
 37    */
 38    private static boolean lookupDone = false;
 39   
 40    /**
 41    * No JNDI available?
 42    */
 43    private static boolean lookupFailed = false;
 44   
 45    /**
 46    * The JVM TransactionManager found.
 47    */
 48    private static TransactionManager tm = null;
 49   
 50    /**
 51    * JNDI locations for TransactionManagers we know of
 52    */
 53    private static String[][] knownJNDIManagers =
 54    {
 55    {"java:/TransactionManager", "JBoss, JRun4"},
 56    {"java:comp/TransactionManager", "Resin 3.x"},
 57    {"java:pm/TransactionManager", "Borland, Sun"},
 58    {"java:appserver/TransactionManager", "Sun Glassfish"},
 59    {"javax.transaction.TransactionManager", "BEA WebLogic"},
 60    {"java:comp/UserTransaction", "Resin, Orion, JOnAS (JOTM)"},
 61    };
 62   
 63    /**
 64    * WebSphere 5.1 and 6.0 TransactionManagerFactory
 65    */
 66    private static final String WS_FACTORY_CLASS_5_1 = "com.ibm.ws.Transaction.TransactionManagerFactory";
 67   
 68    /**
 69    * WebSphere 5.0 TransactionManagerFactory
 70    */
 71    private static final String WS_FACTORY_CLASS_5_0 = "com.ibm.ejs.jts.jta.TransactionManagerFactory";
 72   
 73    /**
 74    * WebSphere 4.0 TransactionManagerFactory
 75    */
 76    private static final String WS_FACTORY_CLASS_4 = "com.ibm.ejs.jts.jta.JTSXA";
 77   
 78    /**
 79    * Get the systemwide used TransactionManager
 80    *
 81    * @return TransactionManager
 82    */
 83  538 public TransactionManager getTransactionManager()
 84    {
 85  538 if (!lookupDone)
 86  538 doLookups();
 87  538 if (tm != null)
 88  474 return tm;
 89  64 if (lookupFailed)
 90    {
 91    //fall back to a dummy from JBossCache
 92  64 tm = DummyTransactionManager.getInstance();
 93  64 log.warn("Falling back to DummyTransactionManager from JBossCache");
 94    }
 95  64 return tm;
 96    }
 97   
 98    /**
 99    * Try to figure out which TransactionManager to use
 100    */
 101  538 private static void doLookups()
 102    {
 103  538 if (lookupFailed)
 104  473 return;
 105  65 InitialContext ctx;
 106  65 try
 107    {
 108  65 ctx = new InitialContext();
 109    }
 110    catch (NamingException e)
 111    {
 112  0 log.error("Failed creating initial JNDI context", e);
 113  0 lookupFailed = true;
 114  0 return;
 115    }
 116    //probe jndi lookups first
 117  65 for (String[] knownJNDIManager : knownJNDIManagers)
 118    {
 119  385 Object jndiObject;
 120  385 try
 121    {
 122  385 if (log.isDebugEnabled())
 123  0 log.debug("Trying to lookup TransactionManager for " + knownJNDIManager[1]);
 124  385 jndiObject = ctx.lookup(knownJNDIManager[0]);
 125    }
 126    catch (NamingException e)
 127    {
 128  384 log.debug("Failed to perform a lookup for [" + knownJNDIManager[0] + " (" + knownJNDIManager[1]
 129    + ")]");
 130  384 continue;
 131    }
 132  1 if (jndiObject instanceof TransactionManager)
 133    {
 134  1 tm = (TransactionManager) jndiObject;
 135  1 log.debug("Found TransactionManager for " + knownJNDIManager[1]);
 136  1 return;
 137    }
 138    }
 139    //try to find websphere lookups since we came here
 140  64 Class clazz;
 141  64 try
 142    {
 143  64 log.debug("Trying WebSphere 5.1: " + WS_FACTORY_CLASS_5_1);
 144  64 clazz = Class.forName(WS_FACTORY_CLASS_5_1);
 145  0 log.debug("Found WebSphere 5.1: " + WS_FACTORY_CLASS_5_1);
 146    }
 147    catch (ClassNotFoundException ex)
 148    {
 149  64 try
 150    {
 151  64 log.debug("Trying WebSphere 5.0: " + WS_FACTORY_CLASS_5_0);
 152  64 clazz = Class.forName(WS_FACTORY_CLASS_5_0);
 153  0 log.debug("Found WebSphere 5.0: " + WS_FACTORY_CLASS_5_0);
 154    }
 155    catch (ClassNotFoundException ex2)
 156    {
 157  64 try
 158    {
 159  64 log.debug("Trying WebSphere 4: " + WS_FACTORY_CLASS_4);
 160  64 clazz = Class.forName(WS_FACTORY_CLASS_4);
 161  0 log.debug("Found WebSphere 4: " + WS_FACTORY_CLASS_4);
 162    }
 163    catch (ClassNotFoundException ex3)
 164    {
 165  64 log.debug("Couldn't find any WebSphere TransactionManager factory class, neither for WebSphere version 5.1 nor 5.0 nor 4");
 166  64 lookupFailed = true;
 167  64 return;
 168    }
 169    }
 170    }
 171  0 try
 172    {
 173  0 Class[] signature = null;
 174  0 Object[] args = null;
 175  0 Method method = clazz.getMethod("getTransactionManager", signature);
 176  0 tm = (TransactionManager) method.invoke(null, args);
 177    }
 178    catch (Exception ex)
 179    {
 180  0 log.error("Found WebSphere TransactionManager factory class [" + clazz.getName()
 181    + "], but couldn't invoke its static 'getTransactionManager' method", ex);
 182    }
 183    }
 184   
 185    }