View Javadoc

1   /*
2    * $Id: WebsphereTransactionManagerLookupFactory.java 20813 2010-12-21 11:37:48Z dirk.olmes $
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       */
49      public TransactionManager create(MuleConfiguration config)
50      {
51          Class<?> clazz;
52          TransactionManager transactionManager;
53          try
54          {
55              logger.debug("Trying WebSphere 5.1+: " + FACTORY_CLASS_5_1_AND_ABOVE);
56              clazz = ClassUtils.loadClass(FACTORY_CLASS_5_1_AND_ABOVE, this.getClass());
57              logger.info("Found WebSphere 5.1+: " + FACTORY_CLASS_5_1_AND_ABOVE);
58          }
59          catch (ClassNotFoundException ex)
60          {
61              logger.debug("Could not find WebSphere 5.1+ TransactionManager factory class", ex);
62              try
63              {
64                  logger.debug("Trying WebSphere 5.0: " + FACTORY_CLASS_5_0);
65                  clazz = ClassUtils.loadClass(FACTORY_CLASS_5_0, this.getClass());
66                  logger.info("Found WebSphere 5.0: " + FACTORY_CLASS_5_0);
67              }
68              catch (ClassNotFoundException ex2)
69              {
70                  logger.debug("Could not find WebSphere 5.0 TransactionManager factory class", ex2);
71                  try
72                  {
73                      logger.debug("Trying WebSphere 4: " + FACTORY_CLASS_4);
74                      clazz = ClassUtils.loadClass(FACTORY_CLASS_4, this.getClass());
75                      logger.info("Found WebSphere 4: " + FACTORY_CLASS_4);
76                  }
77                  catch (ClassNotFoundException ex3)
78                  {
79                      logger.debug("Could not find WebSphere 4 TransactionManager factory class", ex3);
80                      throw new MuleRuntimeException(
81                          CoreMessages.createStaticMessage("Couldn't find any WebSphere TransactionManager factory class, "
82                                                           + "neither for WebSphere version 5.1 nor 5.0 nor 4"),
83                          ex);
84                  }
85              }
86          }
87          try
88          {
89              Method method = clazz.getMethod("getTransactionManager", (Class[])null);
90              transactionManager = (TransactionManager) method.invoke(null, (Object[])null);
91          }
92          catch (Exception ex)
93          {
94              throw new MuleRuntimeException(
95                  CoreMessages.createStaticMessage("Found WebSphere TransactionManager factory class ["
96                                                   + clazz.getName()
97                                                   + "], but couldn't invoke its static 'getTransactionManager' method"),
98                  ex);
99          }
100 
101         return transactionManager;
102     }
103 }