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.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    }