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; 021 022import javax.ejb.ApplicationException; 023import javax.ejb.TransactionAttributeType; 024 025import org.springframework.core.annotation.AnnotationUtils; 026import org.springframework.lang.Nullable; 027import org.springframework.transaction.interceptor.DefaultTransactionAttribute; 028import org.springframework.transaction.interceptor.TransactionAttribute; 029 030/** 031 * Strategy implementation for parsing EJB3's {@link javax.ejb.TransactionAttribute} 032 * annotation. 033 * 034 * @author Juergen Hoeller 035 * @since 2.5 036 */ 037@SuppressWarnings("serial") 038public class Ejb3TransactionAnnotationParser implements TransactionAnnotationParser, Serializable { 039 040 @Override 041 public boolean isCandidateClass(Class<?> targetClass) { 042 return AnnotationUtils.isCandidateClass(targetClass, javax.ejb.TransactionAttribute.class); 043 } 044 045 @Override 046 @Nullable 047 public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) { 048 javax.ejb.TransactionAttribute ann = element.getAnnotation(javax.ejb.TransactionAttribute.class); 049 if (ann != null) { 050 return parseTransactionAnnotation(ann); 051 } 052 else { 053 return null; 054 } 055 } 056 057 public TransactionAttribute parseTransactionAnnotation(javax.ejb.TransactionAttribute ann) { 058 return new Ejb3TransactionAttribute(ann.value()); 059 } 060 061 062 @Override 063 public boolean equals(@Nullable Object other) { 064 return (this == other || other instanceof Ejb3TransactionAnnotationParser); 065 } 066 067 @Override 068 public int hashCode() { 069 return Ejb3TransactionAnnotationParser.class.hashCode(); 070 } 071 072 073 /** 074 * EJB3-specific TransactionAttribute, implementing EJB3's rollback rules 075 * which are based on annotated exceptions. 076 */ 077 private static class Ejb3TransactionAttribute extends DefaultTransactionAttribute { 078 079 public Ejb3TransactionAttribute(TransactionAttributeType type) { 080 setPropagationBehaviorName(PREFIX_PROPAGATION + type.name()); 081 } 082 083 @Override 084 public boolean rollbackOn(Throwable ex) { 085 ApplicationException ann = ex.getClass().getAnnotation(ApplicationException.class); 086 return (ann != null ? ann.rollback() : super.rollbackOn(ex)); 087 } 088 } 089 090}