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.annotation;
018
019import java.io.Serializable;
020import java.lang.reflect.AnnotatedElement;
021import java.util.ArrayList;
022import java.util.List;
023
024import org.springframework.core.annotation.AnnotatedElementUtils;
025import org.springframework.core.annotation.AnnotationAttributes;
026import org.springframework.core.annotation.AnnotationUtils;
027import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
028import org.springframework.transaction.interceptor.RollbackRuleAttribute;
029import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
030import org.springframework.transaction.interceptor.TransactionAttribute;
031
032/**
033 * Strategy implementation for parsing JTA 1.2's {@link javax.transaction.Transactional} annotation.
034 *
035 * @author Juergen Hoeller
036 * @since 4.0
037 */
038@SuppressWarnings("serial")
039public class JtaTransactionAnnotationParser implements TransactionAnnotationParser, Serializable {
040
041        @Override
042        public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
043                AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(
044                                element, javax.transaction.Transactional.class);
045                if (attributes != null) {
046                        return parseTransactionAnnotation(attributes);
047                }
048                else {
049                        return null;
050                }
051        }
052
053        public TransactionAttribute parseTransactionAnnotation(javax.transaction.Transactional ann) {
054                return parseTransactionAnnotation(AnnotationUtils.getAnnotationAttributes(ann, false, false));
055        }
056
057        protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) {
058                RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
059
060                rbta.setPropagationBehaviorName(
061                                RuleBasedTransactionAttribute.PREFIX_PROPAGATION + attributes.getEnum("value").toString());
062
063                List<RollbackRuleAttribute> rollbackRules = new ArrayList<RollbackRuleAttribute>();
064                for (Class<?> rbRule : attributes.getClassArray("rollbackOn")) {
065                        rollbackRules.add(new RollbackRuleAttribute(rbRule));
066                }
067                for (Class<?> rbRule : attributes.getClassArray("dontRollbackOn")) {
068                        rollbackRules.add(new NoRollbackRuleAttribute(rbRule));
069                }
070                rbta.setRollbackRules(rollbackRules);
071
072                return rbta;
073        }
074
075
076        @Override
077        public boolean equals(Object other) {
078                return (this == other || other instanceof JtaTransactionAnnotationParser);
079        }
080
081        @Override
082        public int hashCode() {
083                return JtaTransactionAnnotationParser.class.hashCode();
084        }
085
086}