001/*
002 * Copyright 2012-2017 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 *      http://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.boot.autoconfigure.transaction;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.List;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025
026import org.springframework.core.ResolvableType;
027import org.springframework.transaction.PlatformTransactionManager;
028
029/**
030 * A collection of {@link PlatformTransactionManagerCustomizer}.
031 *
032 * @author Phillip Webb
033 * @since 1.5.0
034 */
035public class TransactionManagerCustomizers {
036
037        private static final Log logger = LogFactory
038                        .getLog(TransactionManagerCustomizers.class);
039
040        private final List<PlatformTransactionManagerCustomizer<?>> customizers;
041
042        public TransactionManagerCustomizers(
043                        Collection<? extends PlatformTransactionManagerCustomizer<?>> customizers) {
044                this.customizers = (customizers == null ? null
045                                : new ArrayList<PlatformTransactionManagerCustomizer<?>>(customizers));
046        }
047
048        public void customize(PlatformTransactionManager transactionManager) {
049                if (this.customizers != null) {
050                        for (PlatformTransactionManagerCustomizer<?> customizer : this.customizers) {
051                                Class<?> generic = ResolvableType
052                                                .forClass(PlatformTransactionManagerCustomizer.class,
053                                                                customizer.getClass())
054                                                .resolveGeneric();
055                                if (generic.isInstance(transactionManager)) {
056                                        customize(transactionManager, customizer);
057                                }
058                        }
059                }
060        }
061
062        @SuppressWarnings({ "unchecked", "rawtypes" })
063        private void customize(PlatformTransactionManager transactionManager,
064                        PlatformTransactionManagerCustomizer customizer) {
065                try {
066                        customizer.customize(transactionManager);
067                }
068                catch (ClassCastException ex) {
069                        // Possibly a lambda-defined customizer which we could not resolve the generic
070                        // transaction manager type for
071                        if (logger.isDebugEnabled()) {
072                                logger.debug("Non-matching transaction manager type for customizer: "
073                                                + customizer, ex);
074                        }
075                }
076        }
077
078}