View Javadoc

1   /*
2    * $Id: WebsphereTransactionManagerLookupFactory.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transaction.lookup;
12  
13  import org.mule.umo.manager.UMOTransactionManagerFactory;
14  import org.mule.util.ClassUtils;
15  
16  import java.lang.reflect.Method;
17  
18  import javax.transaction.TransactionManager;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * The code borrowed from Spring's
25   * org.springframework.transaction.jta.WebSphereTransactionManagerFactoryBean. See
26   * the apache-2.0.license file in Mule's licenses folder for details.
27   * 
28   * @see com.ibm.ws.Transaction.TransactionManagerFactory#getTransactionManager
29   * @see com.ibm.ejs.jts.jta.JTSXA#getTransactionManager
30   * @see com.ibm.ejs.jts.jta.TransactionManagerFactory#getTransactionManager
31   */
32  public class WebsphereTransactionManagerLookupFactory implements UMOTransactionManagerFactory
33  {
34      private static final String FACTORY_CLASS_5_1_AND_ABOVE = "com.ibm.ws.Transaction.TransactionManagerFactory";
35  
36      private static final String FACTORY_CLASS_5_0 = "com.ibm.ejs.jts.jta.TransactionManagerFactory";
37  
38      private static final String FACTORY_CLASS_4 = "com.ibm.ejs.jts.jta.JTSXA";
39  
40      private final Log logger = LogFactory.getLog(getClass());
41  
42      /**
43       * This constructor retrieves the WebSphere TransactionManager factory class, so
44       * we can get access to the JTA TransactionManager.
45       */
46      public TransactionManager create()
47      {
48          Class clazz;
49          TransactionManager transactionManager;
50          try
51          {
52              logger.debug("Trying WebSphere 5.1+: " + FACTORY_CLASS_5_1_AND_ABOVE);
53              clazz = ClassUtils.loadClass(FACTORY_CLASS_5_1_AND_ABOVE, this.getClass());
54              logger.info("Found WebSphere 5.1+: " + FACTORY_CLASS_5_1_AND_ABOVE);
55          }
56          catch (ClassNotFoundException ex)
57          {
58              logger.debug("Could not find WebSphere 5.1+ TransactionManager factory class", ex);
59              try
60              {
61                  logger.debug("Trying WebSphere 5.0: " + FACTORY_CLASS_5_0);
62                  clazz = ClassUtils.loadClass(FACTORY_CLASS_5_0, this.getClass());
63                  logger.info("Found WebSphere 5.0: " + FACTORY_CLASS_5_0);
64              }
65              catch (ClassNotFoundException ex2)
66              {
67                  logger.debug("Could not find WebSphere 5.0 TransactionManager factory class", ex2);
68                  try
69                  {
70                      logger.debug("Trying WebSphere 4: " + FACTORY_CLASS_4);
71                      clazz = ClassUtils.loadClass(FACTORY_CLASS_4, this.getClass());
72                      logger.info("Found WebSphere 4: " + FACTORY_CLASS_4);
73                  }
74                  catch (ClassNotFoundException ex3)
75                  {
76                      logger.debug("Could not find WebSphere 4 TransactionManager factory class", ex3);
77                      throw new RuntimeException(
78                          "Couldn't find any WebSphere TransactionManager factory class, "
79                          + "neither for WebSphere version 5.1 nor 5.0 nor 4",
80                          ex);
81                  }
82              }
83          }
84          try
85          {
86              Method method = clazz.getMethod("getTransactionManager", null);
87              transactionManager = (TransactionManager) method.invoke(null, null);
88          }
89          catch (Exception ex)
90          {
91              throw new RuntimeException("Found WebSphere TransactionManager factory class [" + clazz.getName()
92                                         + "], but couldn't invoke its static 'getTransactionManager' method",
93                  ex);
94          }
95  
96          return transactionManager;
97      }
98  
99  }