001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }