View Javadoc

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