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