KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > interceptor > TransactionAttributeSourceAdvisor


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.interceptor;
18
19 import java.io.Serializable JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import org.aopalliance.aop.Advice;
23
24 import org.springframework.aop.ClassFilter;
25 import org.springframework.aop.Pointcut;
26 import org.springframework.aop.support.AbstractPointcutAdvisor;
27 import org.springframework.aop.support.StaticMethodMatcherPointcut;
28 import org.springframework.util.ObjectUtils;
29
30 /**
31  * Advisor driven by a TransactionAttributeSource, used to exclude
32  * a TransactionInterceptor from methods that are non-transactional.
33  *
34  * <p>Because the AOP framework caches advice calculations, this is normally
35  * faster than just letting the TransactionInterceptor run and find out
36  * itself that it has no work to do.
37  *
38  * @author Rod Johnson
39  * @author Juergen Hoeller
40  * @see TransactionInterceptor
41  * @see TransactionProxyFactoryBean
42  */

43 public class TransactionAttributeSourceAdvisor extends AbstractPointcutAdvisor {
44     
45     private TransactionInterceptor transactionInterceptor;
46
47     private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut();
48
49
50     /**
51      * Create a new TransactionAttributeSourceAdvisor.
52      */

53     public TransactionAttributeSourceAdvisor() {
54     }
55
56     /**
57      * Create a new TransactionAttributeSourceAdvisor.
58      * @param interceptor the transaction interceptor to use for this advisor
59      */

60     public TransactionAttributeSourceAdvisor(TransactionInterceptor interceptor) {
61         setTransactionInterceptor(interceptor);
62     }
63
64
65     /**
66      * Set the transaction interceptor to use for this advisor.
67      */

68     public void setTransactionInterceptor(TransactionInterceptor interceptor) {
69         this.transactionInterceptor = interceptor;
70     }
71
72     /**
73      * Set the {@link ClassFilter} to use for this pointcut.
74      * Default is {@link ClassFilter#TRUE}.
75      */

76     public void setClassFilter(ClassFilter classFilter) {
77         this.pointcut.setClassFilter(classFilter);
78     }
79
80
81     public Advice getAdvice() {
82         return this.transactionInterceptor;
83     }
84
85     public Pointcut getPointcut() {
86         return this.pointcut;
87     }
88
89
90     /**
91      * Inner class that implements a Pointcut that matches if the underlying
92      * TransactionAttributeSource has an attribute for a given method.
93      */

94     private class TransactionAttributeSourcePointcut extends StaticMethodMatcherPointcut implements Serializable JavaDoc {
95
96         private TransactionAttributeSource getTransactionAttributeSource() {
97             return (transactionInterceptor != null ? transactionInterceptor.getTransactionAttributeSource() : null);
98         }
99
100         public boolean matches(Method JavaDoc method, Class JavaDoc targetClass) {
101             TransactionAttributeSource tas = getTransactionAttributeSource();
102             return (tas != null && tas.getTransactionAttribute(method, targetClass) != null);
103         }
104
105         public boolean equals(Object JavaDoc other) {
106             if (this == other) {
107                 return true;
108             }
109             if (!(other instanceof TransactionAttributeSourcePointcut)) {
110                 return false;
111             }
112             TransactionAttributeSourcePointcut otherPc = (TransactionAttributeSourcePointcut) other;
113             return ObjectUtils.nullSafeEquals(getTransactionAttributeSource(), otherPc.getTransactionAttributeSource());
114         }
115
116         public int hashCode() {
117             return TransactionAttributeSourcePointcut.class.hashCode();
118         }
119
120         public String JavaDoc toString() {
121             return getClass().getName() + ": " + getTransactionAttributeSource();
122         }
123     }
124
125 }
126
Popular Tags