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                    Class<?> clazz = classLoader.loadClass(
048                            PropsValues.TRANSACTION_MANAGER_IMPL);
049    
050                    AbstractPlatformTransactionManager abstractPlatformTransactionManager =
051                            (AbstractPlatformTransactionManager)clazz.newInstance();
052    
053                    Properties properties = PropsUtil.getProperties(
054                            "transaction.manager.property.", true);
055    
056                    Enumeration<String> enu =
057                            (Enumeration<String>)properties.propertyNames();
058    
059                    while (enu.hasMoreElements()) {
060                            String key = enu.nextElement();
061    
062                            String value = properties.getProperty(key);
063    
064                            BeanUtil.setProperty(
065                                    abstractPlatformTransactionManager, key, value);
066                    }
067    
068                    if (abstractPlatformTransactionManager instanceof
069                                    HibernateTransactionManager) {
070    
071                            HibernateTransactionManager hibernateTransactionManager =
072                                    (HibernateTransactionManager)abstractPlatformTransactionManager;
073    
074                            hibernateTransactionManager.setDataSource(dataSource);
075                            hibernateTransactionManager.setSessionFactory(sessionFactory);
076                    }
077    
078                    if (_log.isDebugEnabled()) {
079                            _log.debug(
080                                    "Created transaction manager " +
081                                            abstractPlatformTransactionManager.getClass().getName());
082    
083                            SortedProperties sortedProperties = new SortedProperties(
084                                    properties);
085    
086                            sortedProperties.list(System.out);
087                    }
088    
089                    return abstractPlatformTransactionManager;
090            }
091    
092            private static Log _log = LogFactoryUtil.getLog(
093                    TransactionManagerFactory.class);
094    
095    }