001    /**
002     * Copyright (c) 2000-present 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.shard.ShardLastSessionRecorderHibernateTransactionManager;
018    import com.liferay.portal.dao.shard.ShardSessionFactoryTargetSource;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.SortedProperties;
022    import com.liferay.portal.spring.hibernate.LastSessionRecorderHibernateTransactionManager;
023    import com.liferay.portal.util.ClassLoaderUtil;
024    import com.liferay.portal.util.PropsUtil;
025    import com.liferay.portal.util.PropsValues;
026    
027    import java.util.Enumeration;
028    import java.util.Properties;
029    
030    import javax.sql.DataSource;
031    
032    import jodd.bean.BeanUtil;
033    
034    import org.hibernate.SessionFactory;
035    
036    import org.springframework.orm.hibernate3.HibernateTransactionManager;
037    import org.springframework.transaction.support.AbstractPlatformTransactionManager;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class TransactionManagerFactory {
043    
044            public static AbstractPlatformTransactionManager createTransactionManager(
045                            DataSource dataSource, SessionFactory sessionFactory,
046                            ShardSessionFactoryTargetSource shardSessionFactoryTargetSource)
047                    throws Exception {
048    
049                    ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
050    
051                    Class<?> clazz = classLoader.loadClass(
052                            PropsValues.TRANSACTION_MANAGER_IMPL);
053    
054                    AbstractPlatformTransactionManager abstractPlatformTransactionManager =
055                            null;
056    
057                    if ((shardSessionFactoryTargetSource != null) &&
058                            (clazz == LastSessionRecorderHibernateTransactionManager.class)) {
059    
060                            abstractPlatformTransactionManager =
061                                    new ShardLastSessionRecorderHibernateTransactionManager(
062                                            shardSessionFactoryTargetSource);
063                    }
064                    else {
065                            abstractPlatformTransactionManager =
066                                    (AbstractPlatformTransactionManager)clazz.newInstance();
067                    }
068    
069                    Properties properties = PropsUtil.getProperties(
070                            "transaction.manager.property.", true);
071    
072                    Enumeration<String> enu =
073                            (Enumeration<String>)properties.propertyNames();
074    
075                    while (enu.hasMoreElements()) {
076                            String key = enu.nextElement();
077    
078                            String value = properties.getProperty(key);
079    
080                            BeanUtil.setProperty(
081                                    abstractPlatformTransactionManager, key, value);
082                    }
083    
084                    if (abstractPlatformTransactionManager instanceof
085                                    HibernateTransactionManager) {
086    
087                            HibernateTransactionManager hibernateTransactionManager =
088                                    (HibernateTransactionManager)abstractPlatformTransactionManager;
089    
090                            hibernateTransactionManager.setDataSource(dataSource);
091                            hibernateTransactionManager.setSessionFactory(sessionFactory);
092                    }
093    
094                    if (_log.isDebugEnabled()) {
095                            _log.debug(
096                                    "Created transaction manager " +
097                                            abstractPlatformTransactionManager.getClass().getName());
098    
099                            SortedProperties sortedProperties = new SortedProperties(
100                                    properties);
101    
102                            sortedProperties.list(System.out);
103                    }
104    
105                    return abstractPlatformTransactionManager;
106            }
107    
108            private static final Log _log = LogFactoryUtil.getLog(
109                    TransactionManagerFactory.class);
110    
111    }