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.interceptor;
018
019import java.io.Serializable;
020import java.lang.reflect.Method;
021
022import org.springframework.lang.Nullable;
023import org.springframework.util.ClassUtils;
024import org.springframework.util.ObjectUtils;
025
026/**
027 * Very simple implementation of TransactionAttributeSource which will always return
028 * the same TransactionAttribute for all methods fed to it. The TransactionAttribute
029 * may be specified, but will otherwise default to PROPAGATION_REQUIRED. This may be
030 * used in the cases where you want to use the same transaction attribute with all
031 * methods being handled by a transaction interceptor.
032 *
033 * @author Colin Sampaleanu
034 * @since 15.10.2003
035 * @see org.springframework.transaction.interceptor.TransactionProxyFactoryBean
036 * @see org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
037 */
038@SuppressWarnings("serial")
039public class MatchAlwaysTransactionAttributeSource implements TransactionAttributeSource, Serializable {
040
041        private TransactionAttribute transactionAttribute = new DefaultTransactionAttribute();
042
043
044        /**
045         * Allows a transaction attribute to be specified, using the String form, for
046         * example, "PROPAGATION_REQUIRED".
047         * @param transactionAttribute the String form of the transactionAttribute to use.
048         * @see org.springframework.transaction.interceptor.TransactionAttributeEditor
049         */
050        public void setTransactionAttribute(TransactionAttribute transactionAttribute) {
051                this.transactionAttribute = transactionAttribute;
052        }
053
054
055        @Override
056        @Nullable
057        public TransactionAttribute getTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
058                return (ClassUtils.isUserLevelMethod(method) ? this.transactionAttribute : null);
059        }
060
061
062        @Override
063        public boolean equals(@Nullable Object other) {
064                if (this == other) {
065                        return true;
066                }
067                if (!(other instanceof MatchAlwaysTransactionAttributeSource)) {
068                        return false;
069                }
070                MatchAlwaysTransactionAttributeSource otherTas = (MatchAlwaysTransactionAttributeSource) other;
071                return ObjectUtils.nullSafeEquals(this.transactionAttribute, otherTas.transactionAttribute);
072        }
073
074        @Override
075        public int hashCode() {
076                return MatchAlwaysTransactionAttributeSource.class.hashCode();
077        }
078
079        @Override
080        public String toString() {
081                return getClass().getName() + ": " + this.transactionAttribute;
082        }
083
084}