001: /*
002: * $Id: WebsphereTransactionManagerLookupFactory.java 10802 2008-02-14 11:39:16Z holger $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transaction.lookup;
012:
013: import org.mule.api.transaction.TransactionManagerFactory;
014: import org.mule.util.ClassUtils;
015:
016: import java.lang.reflect.Method;
017:
018: import javax.transaction.TransactionManager;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: /**
024: * The code borrowed from Spring's
025: * org.springframework.transaction.jta.WebSphereTransactionManagerFactoryBean. See
026: * the apache-2.0.license file in Mule's licenses folder for details.
027: *
028: * @see com.ibm.ws.Transaction.TransactionManagerFactory#getTransactionManager
029: * @see com.ibm.ejs.jts.jta.JTSXA#getTransactionManager
030: * @see com.ibm.ejs.jts.jta.TransactionManagerFactory#getTransactionManager
031: */
032: public class WebsphereTransactionManagerLookupFactory implements
033: TransactionManagerFactory {
034: private static final String FACTORY_CLASS_5_1_AND_ABOVE = "com.ibm.ws.Transaction.TransactionManagerFactory";
035:
036: private static final String FACTORY_CLASS_5_0 = "com.ibm.ejs.jts.jta.TransactionManagerFactory";
037:
038: private static final String FACTORY_CLASS_4 = "com.ibm.ejs.jts.jta.JTSXA";
039:
040: private final Log logger = LogFactory.getLog(getClass());
041:
042: /**
043: * This constructor retrieves the WebSphere TransactionManager factory class, so
044: * we can get access to the JTA TransactionManager.
045: */
046: public TransactionManager create() {
047: Class clazz;
048: TransactionManager transactionManager;
049: try {
050: logger.debug("Trying WebSphere 5.1+: "
051: + FACTORY_CLASS_5_1_AND_ABOVE);
052: clazz = ClassUtils.loadClass(FACTORY_CLASS_5_1_AND_ABOVE,
053: this .getClass());
054: logger.info("Found WebSphere 5.1+: "
055: + FACTORY_CLASS_5_1_AND_ABOVE);
056: } catch (ClassNotFoundException ex) {
057: logger
058: .debug(
059: "Could not find WebSphere 5.1+ TransactionManager factory class",
060: ex);
061: try {
062: logger.debug("Trying WebSphere 5.0: "
063: + FACTORY_CLASS_5_0);
064: clazz = ClassUtils.loadClass(FACTORY_CLASS_5_0, this
065: .getClass());
066: logger
067: .info("Found WebSphere 5.0: "
068: + FACTORY_CLASS_5_0);
069: } catch (ClassNotFoundException ex2) {
070: logger
071: .debug(
072: "Could not find WebSphere 5.0 TransactionManager factory class",
073: ex2);
074: try {
075: logger.debug("Trying WebSphere 4: "
076: + FACTORY_CLASS_4);
077: clazz = ClassUtils.loadClass(FACTORY_CLASS_4, this
078: .getClass());
079: logger
080: .info("Found WebSphere 4: "
081: + FACTORY_CLASS_4);
082: } catch (ClassNotFoundException ex3) {
083: logger
084: .debug(
085: "Could not find WebSphere 4 TransactionManager factory class",
086: ex3);
087: throw new RuntimeException(
088: "Couldn't find any WebSphere TransactionManager factory class, "
089: + "neither for WebSphere version 5.1 nor 5.0 nor 4",
090: ex);
091: }
092: }
093: }
094: try {
095: Method method = clazz.getMethod("getTransactionManager",
096: (Class[]) null);
097: transactionManager = (TransactionManager) method.invoke(
098: null, (Object[]) null);
099: } catch (Exception ex) {
100: throw new RuntimeException(
101: "Found WebSphere TransactionManager factory class ["
102: + clazz.getName()
103: + "], but couldn't invoke its static 'getTransactionManager' method",
104: ex);
105: }
106:
107: return transactionManager;
108: }
109:
110: }
|