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