001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.jdbc.aop;
016    
017    import com.liferay.portal.kernel.util.InfrastructureUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.spring.transaction.TransactionInterceptor;
020    
021    import java.lang.reflect.Method;
022    
023    import org.aopalliance.intercept.MethodInvocation;
024    
025    import org.springframework.transaction.interceptor.TransactionAttribute;
026    
027    /**
028     * @author Michael Young
029     */
030    public class DynamicDataSourceTransactionInterceptor
031            extends TransactionInterceptor {
032    
033            public void afterPropertiesSet() {
034                    if (_dynamicDataSourceTargetSource == null) {
035                            _dynamicDataSourceTargetSource =
036                                    (DynamicDataSourceTargetSource)InfrastructureUtil.
037                                            getDynamicDataSourceTargetSource();
038                    }
039            }
040    
041            @Override
042            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
043                    if (_dynamicDataSourceTargetSource == null) {
044                            return super.invoke(methodInvocation);
045                    }
046    
047                    Class<?> targetClass = null;
048    
049                    if (methodInvocation.getThis() != null) {
050                            Object thisObject = methodInvocation.getThis();
051    
052                            targetClass = thisObject.getClass();
053                    }
054    
055                    Method targetMethod = methodInvocation.getMethod();
056    
057                    TransactionAttribute transactionAttribute =
058                            transactionAttributeSource.getTransactionAttribute(
059                                    targetMethod, targetClass);
060    
061                    if ((transactionAttribute != null) &&
062                            transactionAttribute.isReadOnly()) {
063    
064                            _dynamicDataSourceTargetSource.setOperation(Operation.READ);
065                    }
066                    else {
067                            _dynamicDataSourceTargetSource.setOperation(Operation.WRITE);
068                    }
069    
070                    _dynamicDataSourceTargetSource.pushMethod(
071                            targetClass.getName().concat(StringPool.PERIOD).concat(
072                                    targetMethod.getName()));
073    
074                    Object returnValue = null;
075    
076                    try {
077                            returnValue = super.invoke(methodInvocation);
078                    }
079                    finally {
080                            _dynamicDataSourceTargetSource.popMethod();
081                    }
082    
083                    return returnValue;
084            }
085    
086            public void setDynamicDataSourceTargetSource(
087                    DynamicDataSourceTargetSource dynamicDataSourceTargetSource) {
088    
089                    _dynamicDataSourceTargetSource = dynamicDataSourceTargetSource;
090            }
091    
092            private DynamicDataSourceTargetSource _dynamicDataSourceTargetSource;
093    
094    }