001: /*
002: * Copyright 2002-2006 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: * http://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:
017: package org.springframework.transaction.interceptor;
018:
019: import java.io.Serializable;
020: import java.lang.reflect.Method;
021:
022: import org.aopalliance.aop.Advice;
023:
024: import org.springframework.aop.ClassFilter;
025: import org.springframework.aop.Pointcut;
026: import org.springframework.aop.support.AbstractPointcutAdvisor;
027: import org.springframework.aop.support.StaticMethodMatcherPointcut;
028: import org.springframework.util.ObjectUtils;
029:
030: /**
031: * Advisor driven by a TransactionAttributeSource, used to exclude
032: * a TransactionInterceptor from methods that are non-transactional.
033: *
034: * <p>Because the AOP framework caches advice calculations, this is normally
035: * faster than just letting the TransactionInterceptor run and find out
036: * itself that it has no work to do.
037: *
038: * @author Rod Johnson
039: * @author Juergen Hoeller
040: * @see TransactionInterceptor
041: * @see TransactionProxyFactoryBean
042: */
043: public class TransactionAttributeSourceAdvisor extends
044: AbstractPointcutAdvisor {
045:
046: private TransactionInterceptor transactionInterceptor;
047:
048: private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut();
049:
050: /**
051: * Create a new TransactionAttributeSourceAdvisor.
052: */
053: public TransactionAttributeSourceAdvisor() {
054: }
055:
056: /**
057: * Create a new TransactionAttributeSourceAdvisor.
058: * @param interceptor the transaction interceptor to use for this advisor
059: */
060: public TransactionAttributeSourceAdvisor(
061: TransactionInterceptor interceptor) {
062: setTransactionInterceptor(interceptor);
063: }
064:
065: /**
066: * Set the transaction interceptor to use for this advisor.
067: */
068: public void setTransactionInterceptor(
069: TransactionInterceptor interceptor) {
070: this .transactionInterceptor = interceptor;
071: }
072:
073: /**
074: * Set the {@link ClassFilter} to use for this pointcut.
075: * Default is {@link ClassFilter#TRUE}.
076: */
077: public void setClassFilter(ClassFilter classFilter) {
078: this .pointcut.setClassFilter(classFilter);
079: }
080:
081: public Advice getAdvice() {
082: return this .transactionInterceptor;
083: }
084:
085: public Pointcut getPointcut() {
086: return this .pointcut;
087: }
088:
089: /**
090: * Inner class that implements a Pointcut that matches if the underlying
091: * TransactionAttributeSource has an attribute for a given method.
092: */
093: private class TransactionAttributeSourcePointcut extends
094: StaticMethodMatcherPointcut implements Serializable {
095:
096: private TransactionAttributeSource getTransactionAttributeSource() {
097: return (transactionInterceptor != null ? transactionInterceptor
098: .getTransactionAttributeSource()
099: : null);
100: }
101:
102: public boolean matches(Method method, Class targetClass) {
103: TransactionAttributeSource tas = getTransactionAttributeSource();
104: return (tas != null && tas.getTransactionAttribute(method,
105: targetClass) != null);
106: }
107:
108: public boolean equals(Object other) {
109: if (this == other) {
110: return true;
111: }
112: if (!(other instanceof TransactionAttributeSourcePointcut)) {
113: return false;
114: }
115: TransactionAttributeSourcePointcut otherPc = (TransactionAttributeSourcePointcut) other;
116: return ObjectUtils.nullSafeEquals(
117: getTransactionAttributeSource(), otherPc
118: .getTransactionAttributeSource());
119: }
120:
121: public int hashCode() {
122: return TransactionAttributeSourcePointcut.class.hashCode();
123: }
124:
125: public String toString() {
126: return getClass().getName() + ": "
127: + getTransactionAttributeSource();
128: }
129: }
130:
131: }
|