1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.spring.annotation;
24  
25  import com.liferay.portal.kernel.annotation.Transactional;
26  
27  import java.io.Serializable;
28  
29  import java.lang.reflect.AnnotatedElement;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import org.springframework.transaction.annotation.TransactionAnnotationParser;
35  import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
36  import org.springframework.transaction.interceptor.RollbackRuleAttribute;
37  import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
38  import org.springframework.transaction.interceptor.TransactionAttribute;
39  
40  /**
41   * <a href="PortalTransactionAnnotationParser.java.html"><b><i>View Source</i>
42   * </b></a>
43   *
44   * @author Michael Young
45   *
46   */
47  public class PortalTransactionAnnotationParser
48      implements TransactionAnnotationParser, Serializable {
49  
50      public TransactionAttribute parseTransactionAnnotation(
51          AnnotatedElement annotatedElement) {
52  
53          Transactional annotation = annotatedElement.getAnnotation(
54              Transactional.class);
55  
56          if (annotation == null) {
57              return null;
58          }
59  
60          RuleBasedTransactionAttribute ruleBasedTransactionAttribute =
61              new RuleBasedTransactionAttribute();
62  
63          ruleBasedTransactionAttribute.setIsolationLevel(
64              annotation.isolation().value());
65          ruleBasedTransactionAttribute.setPropagationBehavior(
66              annotation.propagation().value());
67          ruleBasedTransactionAttribute.setReadOnly(annotation.readOnly());
68          ruleBasedTransactionAttribute.setTimeout(annotation.timeout());
69  
70          List<RollbackRuleAttribute> rollBackAttributes =
71              new ArrayList<RollbackRuleAttribute>();
72  
73          Class<?>[] rollbackFor = annotation.rollbackFor();
74  
75          for (int i = 0; i < rollbackFor.length; i++) {
76              RollbackRuleAttribute rollbackRuleAttribute =
77                  new RollbackRuleAttribute(rollbackFor[i]);
78  
79              rollBackAttributes.add(rollbackRuleAttribute);
80          }
81  
82          String[] rollbackForClassName = annotation.rollbackForClassName();
83  
84          for (int i = 0; i < rollbackForClassName.length; i++) {
85              RollbackRuleAttribute rollbackRuleAttribute =
86                  new RollbackRuleAttribute(rollbackForClassName[i]);
87  
88              rollBackAttributes.add(rollbackRuleAttribute);
89          }
90  
91          Class<?>[] noRollbackFor = annotation.noRollbackFor();
92  
93          for (int i = 0; i < noRollbackFor.length; ++i) {
94              NoRollbackRuleAttribute noRollbackRuleAttribute =
95                  new NoRollbackRuleAttribute(noRollbackFor[i]);
96  
97              rollBackAttributes.add(noRollbackRuleAttribute);
98          }
99  
100         String[] noRollbackForClassName = annotation.noRollbackForClassName();
101 
102         for (int i = 0; i < noRollbackForClassName.length; ++i) {
103             NoRollbackRuleAttribute noRollbackRuleAttribute =
104                 new NoRollbackRuleAttribute(noRollbackForClassName[i]);
105 
106             rollBackAttributes.add(noRollbackRuleAttribute);
107         }
108 
109         ruleBasedTransactionAttribute.getRollbackRules().addAll(
110             rollBackAttributes);
111 
112         return ruleBasedTransactionAttribute;
113     }
114 
115 }