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.interceptor;
018
019import java.io.Serializable;
020import java.lang.reflect.Method;
021
022import org.springframework.util.Assert;
023
024/**
025 * Composite {@link TransactionAttributeSource} implementation that iterates
026 * over a given array of {@link TransactionAttributeSource} instances.
027 *
028 * @author Juergen Hoeller
029 * @since 2.0
030 */
031@SuppressWarnings("serial")
032public class CompositeTransactionAttributeSource implements TransactionAttributeSource, Serializable {
033
034        private final TransactionAttributeSource[] transactionAttributeSources;
035
036
037        /**
038         * Create a new CompositeTransactionAttributeSource for the given sources.
039         * @param transactionAttributeSources the TransactionAttributeSource instances to combine
040         */
041        public CompositeTransactionAttributeSource(TransactionAttributeSource... transactionAttributeSources) {
042                Assert.notNull(transactionAttributeSources, "TransactionAttributeSource array must not be null");
043                this.transactionAttributeSources = transactionAttributeSources;
044        }
045
046        /**
047         * Return the TransactionAttributeSource instances that this
048         * CompositeTransactionAttributeSource combines.
049         */
050        public final TransactionAttributeSource[] getTransactionAttributeSources() {
051                return this.transactionAttributeSources;
052        }
053
054
055        @Override
056        public TransactionAttribute getTransactionAttribute(Method method, Class<?> targetClass) {
057                for (TransactionAttributeSource source : this.transactionAttributeSources) {
058                        TransactionAttribute attr = source.getTransactionAttribute(method, targetClass);
059                        if (attr != null) {
060                                return attr;
061                        }
062                }
063                return null;
064        }
065
066}