01: /*
02: * Copyright 2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.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.opensource.org/licenses/ecl1.php
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: package org.kuali.core.util.spring;
17:
18: import java.lang.reflect.Method;
19:
20: import org.apache.commons.beanutils.PropertyUtils;
21: import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
22: import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
23: import org.springframework.transaction.interceptor.TransactionAttribute;
24:
25: /**
26: * Classes are not considered for name matching, if they do not have the specified annotation on the class or method. However, the
27: * name matching takes precendence, if they do.
28: */
29: public class AnnotationAndNameMatchingTransactionAttributeSource extends
30: NameMatchTransactionAttributeSource {
31: private AnnotationTransactionAttributeSource annotationTransactionAttributeSource;
32: private Integer transactionTimeout;
33:
34: /**
35: * @see org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource#getTransactionAttribute(java.lang.reflect.Method,
36: * java.lang.Class)
37: */
38: @Override
39: public TransactionAttribute getTransactionAttribute(Method method,
40: Class targetClass) {
41: TransactionAttribute transactionAttribute = annotationTransactionAttributeSource
42: .getTransactionAttribute(method, targetClass);
43: if (transactionAttribute != null) {
44: TransactionAttribute overridingTransactionAttribute = super
45: .getTransactionAttribute(method, targetClass);
46: if (overridingTransactionAttribute != null) {
47: transactionAttribute = overridingTransactionAttribute;
48: }
49: }
50: setTimeout(transactionAttribute);
51: return transactionAttribute;
52: }
53:
54: /**
55: * Sets the annotationTransactionAttributeSource attribute value.
56: *
57: * @param annotationTransactionAttributeSource The annotationTransactionAttributeSource to set.
58: */
59: public void setAnnotationTransactionAttributeSource(
60: AnnotationTransactionAttributeSource annotationTransactionAttributeSource) {
61: this .annotationTransactionAttributeSource = annotationTransactionAttributeSource;
62: }
63:
64: public void setTransactionTimeout(Integer transactionTimeout) {
65: this .transactionTimeout = transactionTimeout;
66: }
67:
68: /**
69: * If the TransactionAttribute has a setTimeout() method, then this method will invoke it and pass the configured
70: * transaction timeout. This is to allow for proper setting of the transaction timeout on a JTA transaction.
71: */
72: protected void setTimeout(TransactionAttribute transactionAttribute) {
73: try {
74: if (transactionTimeout != null) {
75: PropertyUtils.setSimpleProperty(transactionAttribute,
76: "timeout", new Integer(transactionTimeout));
77: }
78: } catch (Exception e) {
79: // failed to set the timeout
80: }
81: }
82: }
|