001/*
002 * Copyright 2002-2018 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.annotation;
018
019import org.springframework.context.annotation.AdviceMode;
020import org.springframework.context.annotation.AdviceModeImportSelector;
021import org.springframework.context.annotation.AutoProxyRegistrar;
022import org.springframework.transaction.config.TransactionManagementConfigUtils;
023import org.springframework.util.ClassUtils;
024
025/**
026 * Selects which implementation of {@link AbstractTransactionManagementConfiguration}
027 * should be used based on the value of {@link EnableTransactionManagement#mode} on the
028 * importing {@code @Configuration} class.
029 *
030 * @author Chris Beams
031 * @author Juergen Hoeller
032 * @since 3.1
033 * @see EnableTransactionManagement
034 * @see ProxyTransactionManagementConfiguration
035 * @see TransactionManagementConfigUtils#TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME
036 * @see TransactionManagementConfigUtils#JTA_TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME
037 */
038public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> {
039
040        /**
041         * Returns {@link ProxyTransactionManagementConfiguration} or
042         * {@code AspectJ(Jta)TransactionManagementConfiguration} for {@code PROXY}
043         * and {@code ASPECTJ} values of {@link EnableTransactionManagement#mode()},
044         * respectively.
045         */
046        @Override
047        protected String[] selectImports(AdviceMode adviceMode) {
048                switch (adviceMode) {
049                        case PROXY:
050                                return new String[] {AutoProxyRegistrar.class.getName(),
051                                                ProxyTransactionManagementConfiguration.class.getName()};
052                        case ASPECTJ:
053                                return new String[] {determineTransactionAspectClass()};
054                        default:
055                                return null;
056                }
057        }
058
059        private String determineTransactionAspectClass() {
060                return (ClassUtils.isPresent("javax.transaction.Transactional", getClass().getClassLoader()) ?
061                                TransactionManagementConfigUtils.JTA_TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME :
062                                TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME);
063        }
064
065}