1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.transaction.jta;
  17. import java.lang.reflect.Method;
  18. import javax.transaction.TransactionManager;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.springframework.beans.factory.FactoryBean;
  22. import org.springframework.transaction.TransactionSystemException;
  23. /**
  24. * FactoryBean that retrieves the JTA TransactionManager for IBM's
  25. * WebSphere application servers (versions 5.1, 5.0 and 4).
  26. *
  27. * <p>Uses WebSphere's static access methods to obtain the JTA
  28. * TransactionManager (different for WebSphere 5.1, 5.0 and 4).
  29. *
  30. * <p>The strategy has been kindly borrowed from Hibernate's
  31. * WebSphereTransactionManagerLookup class.
  32. *
  33. * @author Juergen Hoeller
  34. * @since 21.01.2004
  35. * @see JtaTransactionManager#setTransactionManager
  36. * @see net.sf.hibernate.transaction.WebSphereTransactionManagerLookup
  37. * @see com.ibm.ws.Transaction.TransactionManagerFactory#getTransactionManager
  38. * @see com.ibm.ejs.jts.jta.JTSXA#getTransactionManager
  39. * @see com.ibm.ejs.jts.jta.TransactionManagerFactory#getTransactionManager
  40. */
  41. public class WebSphereTransactionManagerFactoryBean implements FactoryBean {
  42. public static final String FACTORY_CLASS_5_1 = "com.ibm.ws.Transaction.TransactionManagerFactory";
  43. public static final String FACTORY_CLASS_5_0 = "com.ibm.ejs.jts.jta.TransactionManagerFactory";
  44. public static final String FACTORY_CLASS_4 = "com.ibm.ejs.jts.jta.JTSXA";
  45. private static final Log logger = LogFactory.getLog(WebSphereTransactionManagerFactoryBean.class);
  46. private final TransactionManager transactionManager;
  47. public WebSphereTransactionManagerFactoryBean() throws TransactionSystemException {
  48. Class clazz;
  49. try {
  50. logger.debug("Trying WebSphere 5.1: " + FACTORY_CLASS_5_1);
  51. clazz = Class.forName(FACTORY_CLASS_5_1);
  52. logger.info("Found WebSphere 5.1: " + FACTORY_CLASS_5_1);
  53. }
  54. catch (ClassNotFoundException ex) {
  55. try {
  56. logger.debug("Trying WebSphere 5.0: " + FACTORY_CLASS_5_0);
  57. clazz = Class.forName(FACTORY_CLASS_5_0);
  58. logger.info("Found WebSphere 5.0: " + FACTORY_CLASS_5_0);
  59. }
  60. catch (ClassNotFoundException ex2) {
  61. try {
  62. logger.debug("Trying WebSphere 4: " + FACTORY_CLASS_4);
  63. clazz = Class.forName(FACTORY_CLASS_4);
  64. logger.info("Found WebSphere 4: " + FACTORY_CLASS_4);
  65. }
  66. catch (ClassNotFoundException ex3) {
  67. throw new TransactionSystemException("Couldn't find any WebSphere TransactionManager factory class, " +
  68. "neither for WebSphere version 5.1 nor 5.0 nor 4");
  69. }
  70. }
  71. }
  72. try {
  73. Method method = clazz.getMethod("getTransactionManager", null);
  74. this.transactionManager = (TransactionManager) method.invoke(null, null);
  75. }
  76. catch (Exception ex) {
  77. throw new TransactionSystemException("Found WebSphere TransactionManager factory class [" + clazz.getName() +
  78. "], but couldn't invoke its static 'getTransactionManager' method", ex);
  79. }
  80. }
  81. public Object getObject() {
  82. return this.transactionManager;
  83. }
  84. public Class getObjectType() {
  85. return this.transactionManager.getClass();
  86. }
  87. public boolean isSingleton() {
  88. return true;
  89. }
  90. }