001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.transaction.config;
018
019import org.springframework.beans.BeanUtils;
020import org.springframework.beans.factory.FactoryBean;
021import org.springframework.beans.factory.InitializingBean;
022import org.springframework.lang.Nullable;
023import org.springframework.transaction.TransactionSystemException;
024import org.springframework.transaction.jta.JtaTransactionManager;
025import org.springframework.util.ClassUtils;
026
027/**
028 * A {@link FactoryBean} equivalent to the <tx:jta-transaction-manager/> XML element,
029 * autodetecting WebLogic and WebSphere servers and exposing the corresponding
030 * {@link org.springframework.transaction.jta.JtaTransactionManager} subclass.
031 *
032 * @author Juergen Hoeller
033 * @since 4.1.1
034 * @see org.springframework.transaction.jta.WebLogicJtaTransactionManager
035 * @see org.springframework.transaction.jta.WebSphereUowTransactionManager
036 */
037public class JtaTransactionManagerFactoryBean implements FactoryBean<JtaTransactionManager>, InitializingBean {
038
039        private static final String WEBLOGIC_JTA_TRANSACTION_MANAGER_CLASS_NAME =
040                        "org.springframework.transaction.jta.WebLogicJtaTransactionManager";
041
042        private static final String WEBSPHERE_TRANSACTION_MANAGER_CLASS_NAME =
043                        "org.springframework.transaction.jta.WebSphereUowTransactionManager";
044
045        private static final String JTA_TRANSACTION_MANAGER_CLASS_NAME =
046                        "org.springframework.transaction.jta.JtaTransactionManager";
047
048
049        private static final boolean weblogicPresent;
050
051        private static final boolean webspherePresent;
052
053        static {
054                ClassLoader classLoader = JtaTransactionManagerFactoryBean.class.getClassLoader();
055                weblogicPresent = ClassUtils.isPresent("weblogic.transaction.UserTransaction", classLoader);
056                webspherePresent = ClassUtils.isPresent("com.ibm.wsspi.uow.UOWManager", classLoader);
057        }
058
059
060        private final JtaTransactionManager transactionManager;
061
062
063        @SuppressWarnings("unchecked")
064        public JtaTransactionManagerFactoryBean() {
065                String className = resolveJtaTransactionManagerClassName();
066                try {
067                        Class<? extends JtaTransactionManager> clazz = (Class<? extends JtaTransactionManager>)
068                                        ClassUtils.forName(className, JtaTransactionManagerFactoryBean.class.getClassLoader());
069                        this.transactionManager = BeanUtils.instantiateClass(clazz);
070                }
071                catch (ClassNotFoundException ex) {
072                        throw new IllegalStateException("Failed to load JtaTransactionManager class: " + className, ex);
073                }
074        }
075
076
077        @Override
078        public void afterPropertiesSet() throws TransactionSystemException {
079                this.transactionManager.afterPropertiesSet();
080        }
081
082        @Override
083        @Nullable
084        public JtaTransactionManager getObject() {
085                return this.transactionManager;
086        }
087
088        @Override
089        public Class<?> getObjectType() {
090                return this.transactionManager.getClass();
091        }
092
093        @Override
094        public boolean isSingleton() {
095                return true;
096        }
097
098
099        static String resolveJtaTransactionManagerClassName() {
100                if (weblogicPresent) {
101                        return WEBLOGIC_JTA_TRANSACTION_MANAGER_CLASS_NAME;
102                }
103                else if (webspherePresent) {
104                        return WEBSPHERE_TRANSACTION_MANAGER_CLASS_NAME;
105                }
106                else {
107                        return JTA_TRANSACTION_MANAGER_CLASS_NAME;
108                }
109        }
110
111}