View Javadoc

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