01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.transaction.annotation;
18:
19: import java.io.Serializable;
20: import java.lang.reflect.AnnotatedElement;
21:
22: import javax.ejb.ApplicationException;
23: import javax.ejb.TransactionAttributeType;
24:
25: import org.springframework.transaction.interceptor.TransactionAttribute;
26: import org.springframework.transaction.support.DefaultTransactionDefinition;
27:
28: /**
29: * Strategy implementation for parsing EJB3's {@link javax.ejb.TransactionAttribute}
30: * annotation.
31: *
32: * @author Juergen Hoeller
33: * @since 2.5
34: */
35: public class Ejb3TransactionAnnotationParser implements
36: TransactionAnnotationParser, Serializable {
37:
38: public TransactionAttribute parseTransactionAnnotation(
39: AnnotatedElement ae) {
40: javax.ejb.TransactionAttribute ann = ae
41: .getAnnotation(javax.ejb.TransactionAttribute.class);
42: if (ann != null) {
43: return new Ejb3TransactionAttribute(ann.value());
44: } else {
45: return null;
46: }
47: }
48:
49: /**
50: * EJB3-specific TransactionAttribute, implementing EJB3's rollback rules
51: * which are based on annotated exceptions.
52: */
53: private static class Ejb3TransactionAttribute extends
54: DefaultTransactionDefinition implements
55: TransactionAttribute {
56:
57: public Ejb3TransactionAttribute(TransactionAttributeType type) {
58: setPropagationBehaviorName(PREFIX_PROPAGATION + type.name());
59: }
60:
61: public boolean rollbackOn(Throwable ex) {
62: ApplicationException ann = ex.getClass().getAnnotation(
63: ApplicationException.class);
64: return (ann != null ? ann.rollback()
65: : (ex instanceof RuntimeException || ex instanceof Error));
66: }
67: }
68:
69: }
|