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