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