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