1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.spring.annotation;
21  
22  import com.liferay.portal.kernel.annotation.Transactional;
23  
24  import java.io.Serializable;
25  
26  import java.lang.reflect.AnnotatedElement;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import org.springframework.transaction.annotation.TransactionAnnotationParser;
32  import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
33  import org.springframework.transaction.interceptor.RollbackRuleAttribute;
34  import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
35  import org.springframework.transaction.interceptor.TransactionAttribute;
36  
37  /**
38   * <a href="PortalTransactionAnnotationParser.java.html"><b><i>View Source</i>
39   * </b></a>
40   *
41   * @author Michael Young
42   *
43   */
44  public class PortalTransactionAnnotationParser
45      implements TransactionAnnotationParser, Serializable {
46  
47      public TransactionAttribute parseTransactionAnnotation(
48          AnnotatedElement annotatedElement) {
49  
50          Transactional annotation = annotatedElement.getAnnotation(
51              Transactional.class);
52  
53          if (annotation == null) {
54              return null;
55          }
56  
57          RuleBasedTransactionAttribute ruleBasedTransactionAttribute =
58              new RuleBasedTransactionAttribute();
59  
60          ruleBasedTransactionAttribute.setIsolationLevel(
61              annotation.isolation().value());
62          ruleBasedTransactionAttribute.setPropagationBehavior(
63              annotation.propagation().value());
64          ruleBasedTransactionAttribute.setReadOnly(annotation.readOnly());
65          ruleBasedTransactionAttribute.setTimeout(annotation.timeout());
66  
67          List<RollbackRuleAttribute> rollBackAttributes =
68              new ArrayList<RollbackRuleAttribute>();
69  
70          Class<?>[] rollbackFor = annotation.rollbackFor();
71  
72          for (int i = 0; i < rollbackFor.length; i++) {
73              RollbackRuleAttribute rollbackRuleAttribute =
74                  new RollbackRuleAttribute(rollbackFor[i]);
75  
76              rollBackAttributes.add(rollbackRuleAttribute);
77          }
78  
79          String[] rollbackForClassName = annotation.rollbackForClassName();
80  
81          for (int i = 0; i < rollbackForClassName.length; i++) {
82              RollbackRuleAttribute rollbackRuleAttribute =
83                  new RollbackRuleAttribute(rollbackForClassName[i]);
84  
85              rollBackAttributes.add(rollbackRuleAttribute);
86          }
87  
88          Class<?>[] noRollbackFor = annotation.noRollbackFor();
89  
90          for (int i = 0; i < noRollbackFor.length; ++i) {
91              NoRollbackRuleAttribute noRollbackRuleAttribute =
92                  new NoRollbackRuleAttribute(noRollbackFor[i]);
93  
94              rollBackAttributes.add(noRollbackRuleAttribute);
95          }
96  
97          String[] noRollbackForClassName = annotation.noRollbackForClassName();
98  
99          for (int i = 0; i < noRollbackForClassName.length; ++i) {
100             NoRollbackRuleAttribute noRollbackRuleAttribute =
101                 new NoRollbackRuleAttribute(noRollbackForClassName[i]);
102 
103             rollBackAttributes.add(noRollbackRuleAttribute);
104         }
105 
106         ruleBasedTransactionAttribute.getRollbackRules().addAll(
107             rollBackAttributes);
108 
109         return ruleBasedTransactionAttribute;
110     }
111 
112 }