View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.kns.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 NameMatchTransactionAttributeSource {
30      private AnnotationTransactionAttributeSource annotationTransactionAttributeSource;
31      private Integer transactionTimeout;
32  
33      /**
34       * @see org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource#getTransactionAttribute(java.lang.reflect.Method,
35       *      java.lang.Class)
36       */
37      @Override
38      public TransactionAttribute getTransactionAttribute(Method method, Class targetClass) {
39          TransactionAttribute transactionAttribute = annotationTransactionAttributeSource.getTransactionAttribute(method, targetClass);
40          if (transactionAttribute != null) {
41              TransactionAttribute overridingTransactionAttribute = super.getTransactionAttribute(method, targetClass);
42              if (overridingTransactionAttribute != null) {
43                  transactionAttribute = overridingTransactionAttribute;
44              }
45          }
46          setTimeout(transactionAttribute);
47          return transactionAttribute;
48      }
49  
50      /**
51       * Sets the annotationTransactionAttributeSource attribute value.
52       *
53       * @param annotationTransactionAttributeSource The annotationTransactionAttributeSource to set.
54       */
55      public void setAnnotationTransactionAttributeSource(AnnotationTransactionAttributeSource annotationTransactionAttributeSource) {
56          this.annotationTransactionAttributeSource = annotationTransactionAttributeSource;
57      }
58  
59      public void setTransactionTimeout(Integer transactionTimeout) {
60          this.transactionTimeout = transactionTimeout;
61      }
62  
63      /**
64       * If the TransactionAttribute has a setTimeout() method, then this method will invoke it and pass the configured
65       * transaction timeout.  This is to allow for proper setting of the transaction timeout on a JTA transaction.
66       */
67      protected void setTimeout(TransactionAttribute transactionAttribute) {
68          try {
69              if (transactionTimeout != null) {
70                  PropertyUtils.setSimpleProperty(transactionAttribute, "timeout", new Integer(transactionTimeout));
71              }
72          } catch (Exception e) {
73              // failed to set the timeout
74          }
75      }
76  }