001: /*
002: * Copyright 2002-2007 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.lang.reflect.Method;
020: import java.lang.reflect.Modifier;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import org.springframework.util.ClassUtils;
032: import org.springframework.util.ObjectUtils;
033:
034: /**
035: * Abstract implementation of {@link TransactionAttributeSource} that caches
036: * attributes for methods and implements a fallback policy: 1. specific target
037: * method; 2. target class; 3. declaring method; 4. declaring class/interface.
038: *
039: * <p>Defaults to using the target class's transaction attribute if none is
040: * associated with the target method. Any transaction attribute associated with
041: * the target method completely overrides a class transaction attribute.
042: * If none found on the target class, the interface that the invoked method
043: * has been called through (in case of a JDK proxy) will be checked.
044: *
045: * <p>This implementation caches attributes by method after they are first used.
046: * If it is ever desirable to allow dynamic changing of transaction attributes
047: * (which is very unlikely), caching could be made configurable. Caching is
048: * desirable because of the cost of evaluating rollback rules.
049: *
050: * @author Rod Johnson
051: * @author Juergen Hoeller
052: * @since 1.1
053: */
054: public abstract class AbstractFallbackTransactionAttributeSource
055: implements TransactionAttributeSource {
056:
057: /**
058: * Canonical value held in cache to indicate no transaction attribute was
059: * found for this method, and we don't need to look again.
060: */
061: private final static Object NULL_TRANSACTION_ATTRIBUTE = new Object();
062:
063: /**
064: * Logger available to subclasses.
065: * <p>As this base class is not marked Serializable, the logger will be recreated
066: * after serialization - provided that the concrete subclass is Serializable.
067: */
068: protected final Log logger = LogFactory.getLog(getClass());
069:
070: /**
071: * Cache of TransactionAttributes, keyed by DefaultCacheKey (Method + target Class).
072: * <p>As this base class is not marked Serializable, the cache will be recreated
073: * after serialization - provided that the concrete subclass is Serializable.
074: */
075: final Map attributeCache = new HashMap();
076:
077: /**
078: * Determine the transaction attribute for this method invocation.
079: * <p>Defaults to the class's transaction attribute if no method attribute is found.
080: * @param method the method for the current invocation (never <code>null</code>)
081: * @param targetClass the target class for this invocation (may be <code>null</code>)
082: * @return TransactionAttribute for this method, or <code>null</code> if the method
083: * is not transactional
084: */
085: public TransactionAttribute getTransactionAttribute(Method method,
086: Class targetClass) {
087: // First, see if we have a cached value.
088: Object cacheKey = getCacheKey(method, targetClass);
089: synchronized (this .attributeCache) {
090: Object cached = this .attributeCache.get(cacheKey);
091: if (cached != null) {
092: // Value will either be canonical value indicating there is no transaction attribute,
093: // or an actual transaction attribute.
094: if (cached == NULL_TRANSACTION_ATTRIBUTE) {
095: return null;
096: } else {
097: return (TransactionAttribute) cached;
098: }
099: } else {
100: // We need to work it out.
101: TransactionAttribute txAtt = computeTransactionAttribute(
102: method, targetClass);
103: // Put it in the cache.
104: if (txAtt == null) {
105: this .attributeCache.put(cacheKey,
106: NULL_TRANSACTION_ATTRIBUTE);
107: } else {
108: if (logger.isDebugEnabled()) {
109: logger.debug("Adding transactional method ["
110: + method.getName()
111: + "] with attribute [" + txAtt + "]");
112: }
113: this .attributeCache.put(cacheKey, txAtt);
114: }
115: return txAtt;
116: }
117: }
118: }
119:
120: /**
121: * Determine a cache key for the given method and target class.
122: * <p>Must not produce same key for overloaded methods.
123: * Must produce same key for different instances of the same method.
124: * @param method the method (never <code>null</code>)
125: * @param targetClass the target class (may be <code>null</code>)
126: * @return the cache key (never <code>null</code>)
127: */
128: protected Object getCacheKey(Method method, Class targetClass) {
129: return new DefaultCacheKey(method, targetClass);
130: }
131:
132: /**
133: * Same signature as {@link #getTransactionAttribute}, but doesn't cache the result.
134: * {@link #getTransactionAttribute} is effectively a caching decorator for this method.
135: * @see #getTransactionAttribute
136: */
137: private TransactionAttribute computeTransactionAttribute(
138: Method method, Class targetClass) {
139: // Don't allow no-public methods as required.
140: if (allowPublicMethodsOnly()
141: && !Modifier.isPublic(method.getModifiers())) {
142: return null;
143: }
144:
145: // The method may be on an interface, but we need attributes from the target class.
146: // If the target class is null, the method will be unchanged.
147: Method specificMethod = ClassUtils.getMostSpecificMethod(
148: method, targetClass);
149:
150: // First try is the method in the target class.
151: TransactionAttribute txAtt = findTransactionAttribute(findAllAttributes(specificMethod));
152: if (txAtt != null) {
153: return txAtt;
154: }
155:
156: // Second try is the transaction attribute on the target class.
157: txAtt = findTransactionAttribute(findAllAttributes(specificMethod
158: .getDeclaringClass()));
159: if (txAtt != null) {
160: return txAtt;
161: }
162:
163: if (specificMethod != method) {
164: // Fallback is to look at the original method.
165: txAtt = findTransactionAttribute(findAllAttributes(method));
166: if (txAtt != null) {
167: return txAtt;
168: }
169: // Last fallback is the class of the original method.
170: return findTransactionAttribute(findAllAttributes(method
171: .getDeclaringClass()));
172: }
173: return null;
174: }
175:
176: /**
177: * Subclasses should implement this to return all attributes for this method.
178: * We need all because of the need to analyze rollback rules.
179: * @param method the method to retrieve attributes for
180: * @return all attributes associated with this method (may be <code>null</code>)
181: */
182: protected abstract Collection findAllAttributes(Method method);
183:
184: /**
185: * Subclasses should implement this to return all attributes for this class.
186: * @param clazz class to retrieve attributes for
187: * @return all attributes associated with this class (may be <code>null</code>)
188: */
189: protected abstract Collection findAllAttributes(Class clazz);
190:
191: /**
192: * Return the transaction attribute, given this set of attributes
193: * attached to a method or class.
194: * <p>Protected rather than private as subclasses may want to customize
195: * how this is done: for example, returning a TransactionAttribute
196: * affected by the values of other attributes.
197: * <p>This implementation takes into account RollbackRuleAttributes,
198: * if the TransactionAttribute is a RuleBasedTransactionAttribute.
199: * @param atts attributes attached to a method or class (may be <code>null</code>)
200: * @return TransactionAttribute the corresponding transaction attribute,
201: * or <code>null</code> if none was found
202: */
203: protected TransactionAttribute findTransactionAttribute(
204: Collection atts) {
205: if (atts == null) {
206: return null;
207: }
208:
209: TransactionAttribute txAttribute = null;
210:
211: // Check whether there is a transaction attribute.
212: for (Iterator itr = atts.iterator(); itr.hasNext()
213: && txAttribute == null;) {
214: Object att = itr.next();
215: if (att instanceof TransactionAttribute) {
216: txAttribute = (TransactionAttribute) att;
217: }
218: }
219:
220: // Check if we have a RuleBasedTransactionAttribute.
221: if (txAttribute instanceof RuleBasedTransactionAttribute) {
222: RuleBasedTransactionAttribute rbta = (RuleBasedTransactionAttribute) txAttribute;
223: // We really want value: bit of a hack.
224: List rollbackRules = new LinkedList();
225: for (Iterator it = atts.iterator(); it.hasNext();) {
226: Object att = it.next();
227: if (att instanceof RollbackRuleAttribute) {
228: if (logger.isDebugEnabled()) {
229: logger.debug("Found rollback rule: " + att);
230: }
231: rollbackRules.add(att);
232: }
233: }
234: // Repeatedly setting this isn't elegant, but it works.
235: rbta.setRollbackRules(rollbackRules);
236: }
237:
238: return txAttribute;
239: }
240:
241: /**
242: * Should only public methods be allowed to have transactional semantics?
243: * <p>The default implementation returns <code>false</code>.
244: */
245: protected boolean allowPublicMethodsOnly() {
246: return false;
247: }
248:
249: /**
250: * Default cache key for the TransactionAttribute cache.
251: */
252: private static class DefaultCacheKey {
253:
254: private final Method method;
255:
256: private final Class targetClass;
257:
258: public DefaultCacheKey(Method method, Class targetClass) {
259: this .method = method;
260: this .targetClass = targetClass;
261: }
262:
263: public boolean equals(Object other) {
264: if (this == other) {
265: return true;
266: }
267: if (!(other instanceof DefaultCacheKey)) {
268: return false;
269: }
270: DefaultCacheKey otherKey = (DefaultCacheKey) other;
271: return (this .method.equals(otherKey.method) && ObjectUtils
272: .nullSafeEquals(this .targetClass,
273: otherKey.targetClass));
274: }
275:
276: public int hashCode() {
277: return this .method.hashCode()
278: * 29
279: + (this .targetClass != null ? this .targetClass
280: .hashCode() : 0);
281: }
282: }
283:
284: }
|