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.dao.jdbc.aop;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  
24  import java.lang.reflect.Method;
25  
26  import org.aopalliance.intercept.MethodInvocation;
27  
28  import org.springframework.transaction.interceptor.TransactionAttribute;
29  import org.springframework.transaction.interceptor.TransactionAttributeSource;
30  import org.springframework.transaction.interceptor.TransactionInterceptor;
31  
32  /**
33   * <a href="DynamicDataSourceTransactionInterceptor.java.html"><b><i>View Source
34   * </i></b></a>
35   *
36   * @author Michael Young
37   *
38   */
39  public class DynamicDataSourceTransactionInterceptor
40      extends TransactionInterceptor {
41  
42      public Object invoke(MethodInvocation methodInvocation) throws Throwable {
43          Class<?> targetClass = null;
44  
45          if (methodInvocation.getThis() != null) {
46              targetClass = methodInvocation.getThis().getClass();
47          }
48  
49          Method targetMethod = methodInvocation.getMethod();
50  
51          TransactionAttributeSource transactionAttributeSource =
52              getTransactionAttributeSource();
53  
54          TransactionAttribute transactionAttribute =
55              transactionAttributeSource.getTransactionAttribute(
56                  targetMethod, targetClass);
57  
58          if ((transactionAttribute != null) &&
59              (transactionAttribute.isReadOnly())) {
60  
61              _dynamicDataSourceTargetSource.setOperation(Operation.READ);
62          }
63          else {
64              _dynamicDataSourceTargetSource.setOperation(Operation.WRITE);
65          }
66  
67          StringBuilder sb = new StringBuilder();
68  
69          sb.append(targetClass.getName());
70          sb.append(StringPool.PERIOD);
71          sb.append(targetMethod.getName());
72  
73          _dynamicDataSourceTargetSource.pushMethod(sb.toString());
74  
75          Object returnValue = null;
76  
77          try {
78              returnValue = super.invoke(methodInvocation);
79          }
80          finally {
81              _dynamicDataSourceTargetSource.popMethod();
82          }
83  
84          return returnValue;
85      }
86  
87      public void setDynamicDataSourceTargetSource(
88          DynamicDataSourceTargetSource dynamicDataSourceTargetSource) {
89  
90          _dynamicDataSourceTargetSource = dynamicDataSourceTargetSource;
91      }
92  
93      private DynamicDataSourceTargetSource _dynamicDataSourceTargetSource;
94  
95  }