001    /**
002     * Copyright (c) 2000-2013 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.spring.transaction;
016    
017    import com.liferay.portal.dao.jdbc.aop.DynamicDataSourceAdvice;
018    import com.liferay.portal.dao.jdbc.aop.DynamicDataSourceTargetSource;
019    import com.liferay.portal.kernel.spring.util.FactoryBean;
020    import com.liferay.portal.kernel.util.InfrastructureUtil;
021    
022    import org.aopalliance.intercept.MethodInterceptor;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class TransactionInterceptorFactoryBean
028            implements FactoryBean<MethodInterceptor> {
029    
030            @Override
031            public MethodInterceptor create() {
032                    return new TransactionInterceptor();
033            }
034    
035            @Override
036            public MethodInterceptor postProcessing(
037                    MethodInterceptor methodInterceptor) {
038    
039                    TransactionInterceptor transactionInterceptor =
040                            (TransactionInterceptor)methodInterceptor;
041    
042                    TransactionExecutor transactionExecutor =
043                            TransactionExecutorFactory.createTransactionExecutor(
044                                    transactionInterceptor.platformTransactionManager, false);
045    
046                    transactionInterceptor.setTransactionExecutor(transactionExecutor);
047    
048                    DynamicDataSourceTargetSource dynamicDataSourceTargetSource =
049                            (DynamicDataSourceTargetSource)
050                                    InfrastructureUtil.getDynamicDataSourceTargetSource();
051    
052                    if (dynamicDataSourceTargetSource == null) {
053                            return methodInterceptor;
054                    }
055    
056                    DynamicDataSourceAdvice dynamicDataSourceAdvice =
057                            new DynamicDataSourceAdvice();
058    
059                    dynamicDataSourceAdvice.setDynamicDataSourceTargetSource(
060                            dynamicDataSourceTargetSource);
061                    dynamicDataSourceAdvice.setNextMethodInterceptor(methodInterceptor);
062    
063                    dynamicDataSourceAdvice.setTransactionAttributeSource(
064                            transactionInterceptor.transactionAttributeSource);
065    
066                    return dynamicDataSourceAdvice;
067            }
068    
069    }