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