001    /**
002     * Copyright (c) 2000-2010 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    
020    import java.lang.reflect.Method;
021    
022    import org.aopalliance.intercept.MethodInvocation;
023    
024    import org.springframework.transaction.interceptor.TransactionAttribute;
025    import org.springframework.transaction.interceptor.TransactionAttributeSource;
026    import org.springframework.transaction.interceptor.TransactionInterceptor;
027    
028    /**
029     * @author Michael Young
030     */
031    public class DynamicDataSourceTransactionInterceptor
032            extends TransactionInterceptor {
033    
034            public void afterPropertiesSet() {
035                    if (_dynamicDataSourceTargetSource == null) {
036                            _dynamicDataSourceTargetSource =
037                                    (DynamicDataSourceTargetSource)InfrastructureUtil.
038                                            getDynamicDataSourceTargetSource();
039                    }
040            }
041    
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                            targetClass = methodInvocation.getThis().getClass();
051                    }
052    
053                    Method targetMethod = methodInvocation.getMethod();
054    
055                    TransactionAttributeSource transactionAttributeSource =
056                            getTransactionAttributeSource();
057    
058                    TransactionAttribute transactionAttribute =
059                            transactionAttributeSource.getTransactionAttribute(
060                                    targetMethod, targetClass);
061    
062                    if ((transactionAttribute != null) &&
063                            (transactionAttribute.isReadOnly())) {
064    
065                            _dynamicDataSourceTargetSource.setOperation(Operation.READ);
066                    }
067                    else {
068                            _dynamicDataSourceTargetSource.setOperation(Operation.WRITE);
069                    }
070    
071                    _dynamicDataSourceTargetSource.pushMethod(
072                            targetClass.getName().concat(StringPool.PERIOD).concat(
073                                    targetMethod.getName()));
074    
075                    Object returnValue = null;
076    
077                    try {
078                            returnValue = super.invoke(methodInvocation);
079                    }
080                    finally {
081                            _dynamicDataSourceTargetSource.popMethod();
082                    }
083    
084                    return returnValue;
085            }
086    
087            public void setDynamicDataSourceTargetSource(
088                    DynamicDataSourceTargetSource dynamicDataSourceTargetSource) {
089    
090                    _dynamicDataSourceTargetSource = dynamicDataSourceTargetSource;
091            }
092    
093            private DynamicDataSourceTargetSource _dynamicDataSourceTargetSource;
094    
095    }