KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > annotation > AnnotationTransactionAttributeSource


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 import org.springframework.core.annotation.AnnotationUtils;
26 import org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource;
27 import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
28 import org.springframework.transaction.interceptor.RollbackRuleAttribute;
29 import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
30 import org.springframework.transaction.interceptor.TransactionAttribute;
31
32 /**
33  * Implementation of the
34  * {@link org.springframework.transaction.interceptor.TransactionAttributeSource}
35  * interface for working with transaction metadata in JDK 1.5+ annotation format.
36  *
37  * <p>This class reads Spring's JDK 1.5+ {@link Transactional} annotation and
38  * exposes corresponding transaction attributes to Spring's transaction infrastructure.
39  * Can also be used as base class for a custom annotation-based TransactionAttributeSource.
40  *
41  * <p>This is a direct alternative to
42  * {@link org.springframework.transaction.interceptor.AttributesTransactionAttributeSource},
43  * which is able to read in source-level attributes via Commons Attributes.
44  *
45  * @author Colin Sampaleanu
46  * @author Juergen Hoeller
47  * @since 1.2
48  * @see Transactional
49  * @see #findTransactionAttribute
50  * @see org.springframework.transaction.interceptor.TransactionInterceptor#setTransactionAttributeSource
51  * @see org.springframework.transaction.interceptor.TransactionProxyFactoryBean#setTransactionAttributeSource
52  * @see org.springframework.transaction.interceptor.AttributesTransactionAttributeSource
53  * @see org.springframework.metadata.commons.CommonsAttributes
54  */

55 public class AnnotationTransactionAttributeSource
56         extends AbstractFallbackTransactionAttributeSource implements Serializable JavaDoc {
57
58     private final boolean publicMethodsOnly;
59
60
61     /**
62      * Create a default AnnotationTransactionAttributeSource, supporting
63      * public methods that carry the <code>Transactional</code> annotation.
64      */

65     public AnnotationTransactionAttributeSource() {
66         this.publicMethodsOnly = true;
67     }
68
69     /**
70      * Create a custom AnnotationTransactionAttributeSource.
71      * @param publicMethodsOnly whether to support public methods that carry
72      * the <code>Transactional</code> annotation only (typically for use
73      * with proxy-based AOP), or protected/private methods as well
74      * (typically used with AspectJ class weaving)
75      */

76     public AnnotationTransactionAttributeSource(boolean publicMethodsOnly) {
77         this.publicMethodsOnly = publicMethodsOnly;
78     }
79
80
81     /**
82      * Returns all JDK 1.5+ annotations found for the given method.
83      */

84     protected Collection JavaDoc findAllAttributes(Method JavaDoc method) {
85         return Arrays.asList(AnnotationUtils.getAnnotations(method));
86     }
87
88     /**
89      * Returns all JDK 1.5+ annotations found for the given class.
90      */

91     protected Collection JavaDoc findAllAttributes(Class JavaDoc clazz) {
92         return Arrays.asList(clazz.getAnnotations());
93     }
94
95     /**
96      * Return the transaction attribute, given this set of attributes
97      * attached to a method or class. Overrides method from parent class.
98      * <p>This implementation converts Spring's <code>Transactional</code> annotation
99      * to the Spring metadata classes. Returns <code>null</code> if it's not transactional.
100      * <p>Can be overridden to support custom annotations that carry transaction metadata.
101      * @param atts attributes attached to a method or class. May be <code>null</code>,
102      * in which case a <code>null</code> TransactionAttribute will be returned.
103      * @return TransactionAttribute the configured transaction attribute,
104      * or <code>null</code> if none was found
105      * @see Transactional
106      */

107     protected TransactionAttribute findTransactionAttribute(Collection JavaDoc atts) {
108         if (atts == null) {
109             return null;
110         }
111
112         // See if there is a transaction annotation.
113
for (Object JavaDoc att : atts) {
114             if (att instanceof Transactional) {
115                 Transactional ruleBasedTx = (Transactional) att;
116
117                 RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
118                 rbta.setPropagationBehavior(ruleBasedTx.propagation().value());
119                 rbta.setIsolationLevel(ruleBasedTx.isolation().value());
120                 rbta.setTimeout(ruleBasedTx.timeout());
121                 rbta.setReadOnly(ruleBasedTx.readOnly());
122
123                 ArrayList JavaDoc<RollbackRuleAttribute> rollBackRules = new ArrayList JavaDoc<RollbackRuleAttribute>();
124
125                 Class JavaDoc[] rbf = ruleBasedTx.rollbackFor();
126                 for (int i = 0; i < rbf.length; ++i) {
127                     RollbackRuleAttribute rule = new RollbackRuleAttribute(rbf[i]);
128                     rollBackRules.add(rule);
129                 }
130
131                 String JavaDoc[] rbfc = ruleBasedTx.rollbackForClassName();
132                 for (int i = 0; i < rbfc.length; ++i) {
133                     RollbackRuleAttribute rule = new RollbackRuleAttribute(rbfc[i]);
134                     rollBackRules.add(rule);
135                 }
136
137                 Class JavaDoc[] nrbf = ruleBasedTx.noRollbackFor();
138                 for (int i = 0; i < nrbf.length; ++i) {
139                     NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(nrbf[i]);
140                     rollBackRules.add(rule);
141                 }
142
143                 String JavaDoc[] nrbfc = ruleBasedTx.noRollbackForClassName();
144                 for (int i = 0; i < nrbfc.length; ++i) {
145                     NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(nrbfc[i]);
146                     rollBackRules.add(rule);
147                 }
148
149                 rbta.getRollbackRules().addAll(rollBackRules);
150
151                 return rbta;
152             }
153         }
154
155         return null;
156     }
157
158     /**
159      * By default, only public methods can be made transactional using
160      * {@link Transactional}.
161      */

162     protected boolean allowPublicMethodsOnly() {
163         return this.publicMethodsOnly;
164     }
165
166
167     public boolean equals(Object JavaDoc other) {
168         return (this == other || other instanceof AnnotationTransactionAttributeSource);
169     }
170
171     public int hashCode() {
172         return AnnotationTransactionAttributeSource.class.hashCode();
173     }
174
175 }
176
Popular Tags