1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.spring.transaction;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
20  import com.liferay.portal.kernel.util.SortedProperties;
21  import com.liferay.portal.util.PropsUtil;
22  import com.liferay.portal.util.PropsValues;
23  
24  import java.util.Enumeration;
25  import java.util.Properties;
26  
27  import javax.sql.DataSource;
28  
29  import jodd.bean.BeanUtil;
30  
31  import org.hibernate.SessionFactory;
32  
33  import org.springframework.orm.hibernate3.HibernateTransactionManager;
34  import org.springframework.transaction.support.AbstractPlatformTransactionManager;
35  
36  /**
37   * <a href="TransactionManagerFactory.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class TransactionManagerFactory {
42  
43      public static AbstractPlatformTransactionManager createTransactionManager(
44              DataSource dataSource, SessionFactory sessionFactory)
45          throws Exception {
46  
47          ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
48  
49          AbstractPlatformTransactionManager abstractPlatformTransactionManager =
50              (AbstractPlatformTransactionManager)classLoader.loadClass(
51                  PropsValues.TRANSACTION_MANAGER_IMPL).newInstance();
52  
53          Properties properties = PropsUtil.getProperties(
54              "transaction.manager.property.", true);
55  
56          Enumeration<String> enu =
57              (Enumeration<String>)properties.propertyNames();
58  
59          while (enu.hasMoreElements()) {
60              String key = enu.nextElement();
61  
62              String value = properties.getProperty(key);
63  
64              BeanUtil.setProperty(
65                  abstractPlatformTransactionManager, key, value);
66          }
67  
68          if (abstractPlatformTransactionManager instanceof
69                  HibernateTransactionManager) {
70  
71              HibernateTransactionManager hibernateTransactionManager =
72                  (HibernateTransactionManager)abstractPlatformTransactionManager;
73  
74              hibernateTransactionManager.setDataSource(dataSource);
75              hibernateTransactionManager.setSessionFactory(sessionFactory);
76          }
77  
78          if (_log.isDebugEnabled()) {
79              _log.debug(
80                  "Created transaction manager " +
81                      abstractPlatformTransactionManager.getClass().getName());
82  
83              SortedProperties sortedProperties = new SortedProperties(
84                  properties);
85  
86              sortedProperties.list(System.out);
87          }
88  
89          return abstractPlatformTransactionManager;
90      }
91  
92      private static Log _log = LogFactoryUtil.getLog(
93          TransactionManagerFactory.class);
94  
95  }