1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.jdbc.aop;
16  
17  import com.liferay.portal.kernel.util.InfrastructureUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  
20  import java.lang.reflect.Method;
21  
22  import org.aopalliance.intercept.MethodInvocation;
23  
24  import org.springframework.transaction.interceptor.TransactionAttribute;
25  import org.springframework.transaction.interceptor.TransactionAttributeSource;
26  import org.springframework.transaction.interceptor.TransactionInterceptor;
27  
28  /**
29   * <a href="DynamicDataSourceTransactionInterceptor.java.html"><b><i>View Source
30   * </i></b></a>
31   *
32   * @author Michael Young
33   */
34  public class DynamicDataSourceTransactionInterceptor
35      extends TransactionInterceptor {
36  
37      public void afterPropertiesSet() {
38          if (_dynamicDataSourceTargetSource == null) {
39              _dynamicDataSourceTargetSource =
40                  (DynamicDataSourceTargetSource)InfrastructureUtil.
41                      getDynamicDataSourceTargetSource();
42          }
43      }
44  
45      public Object invoke(MethodInvocation methodInvocation) throws Throwable {
46          if (_dynamicDataSourceTargetSource == null) {
47              return super.invoke(methodInvocation);
48          }
49  
50          Class<?> targetClass = null;
51  
52          if (methodInvocation.getThis() != null) {
53              targetClass = methodInvocation.getThis().getClass();
54          }
55  
56          Method targetMethod = methodInvocation.getMethod();
57  
58          TransactionAttributeSource transactionAttributeSource =
59              getTransactionAttributeSource();
60  
61          TransactionAttribute transactionAttribute =
62              transactionAttributeSource.getTransactionAttribute(
63                  targetMethod, targetClass);
64  
65          if ((transactionAttribute != null) &&
66              (transactionAttribute.isReadOnly())) {
67  
68              _dynamicDataSourceTargetSource.setOperation(Operation.READ);
69          }
70          else {
71              _dynamicDataSourceTargetSource.setOperation(Operation.WRITE);
72          }
73  
74          _dynamicDataSourceTargetSource.pushMethod(
75              targetClass.getName().concat(StringPool.PERIOD).concat(
76                  targetMethod.getName()));
77  
78          Object returnValue = null;
79  
80          try {
81              returnValue = super.invoke(methodInvocation);
82          }
83          finally {
84              _dynamicDataSourceTargetSource.popMethod();
85          }
86  
87          return returnValue;
88      }
89  
90      public void setDynamicDataSourceTargetSource(
91          DynamicDataSourceTargetSource dynamicDataSourceTargetSource) {
92  
93          _dynamicDataSourceTargetSource = dynamicDataSourceTargetSource;
94      }
95  
96      private DynamicDataSourceTargetSource _dynamicDataSourceTargetSource;
97  
98  }