001    /**
002     * Copyright (c) 2000-2011 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.dao.jdbc.util;
016    
017    import com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl;
018    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataSourceFactoryUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.spring.hibernate.PortalHibernateConfiguration;
023    import com.liferay.portal.spring.jpa.LocalContainerEntityManagerFactoryBean;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.util.Properties;
027    
028    import javax.persistence.EntityManagerFactory;
029    
030    import javax.sql.DataSource;
031    
032    import org.hibernate.engine.SessionFactoryImplementor;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class DataSourceSwapper {
038    
039            public static void swapCounterDataSource(Properties properties)
040                    throws Exception {
041    
042                    if (_log.isInfoEnabled()) {
043                            _log.info("Create new counter data source");
044                    }
045    
046                    DataSource newDataSource = DataSourceFactoryUtil.initDataSource(
047                            properties);
048    
049                    DataSource oldDataSource =
050                            _counterDataSourceWrapper.getWrappedDataSource();
051    
052                    if (_log.isInfoEnabled()) {
053                            _log.info("Set new counter data source");
054                    }
055    
056                    _counterDataSourceWrapper.setWrappedDataSource(newDataSource);
057    
058                    if (_log.isInfoEnabled()) {
059                            _log.info("Destroy old counter data source");
060                    }
061    
062                    DataSourceFactoryUtil.destroyDataSource(oldDataSource);
063    
064                    if (PropsValues.PERSISTENCE_PROVIDER.equalsIgnoreCase("jpa")) {
065                            if (_log.isInfoEnabled()) {
066                                    _log.info("Reinitialize Hibernate for new counter data source");
067                            }
068    
069                            _reinitializeJPA("counterSessionFactory", newDataSource);
070                    }
071                    else {
072                            if (_log.isInfoEnabled()) {
073                                    _log.info("Reinitialize JPA for new counter data source");
074                            }
075    
076                            _reinitializeHibernate("counterSessionFactory", newDataSource);
077                    }
078            }
079    
080            public static void swapLiferayDataSource(Properties properties)
081                    throws Exception {
082    
083                    if (_log.isInfoEnabled()) {
084                            _log.info("Create new liferay data source");
085                    }
086    
087                    DataSource newDataSource = DataSourceFactoryUtil.initDataSource(
088                            properties);
089    
090                    DataSource oldDataSource =
091                            _liferayDataSourceWrapper.getWrappedDataSource();
092    
093                    if (_log.isInfoEnabled()) {
094                            _log.info("Set new liferay data source");
095                    }
096    
097                    _liferayDataSourceWrapper.setWrappedDataSource(newDataSource);
098    
099                    if (_log.isInfoEnabled()) {
100                            _log.info("Destroy old liferay data source");
101                    }
102    
103                    DataSourceFactoryUtil.destroyDataSource(oldDataSource);
104    
105                    if (PropsValues.PERSISTENCE_PROVIDER.equalsIgnoreCase("jpa")) {
106                            if (_log.isInfoEnabled()) {
107                                    _log.info("Reinitialize Hibernate for new liferay data source");
108                            }
109    
110                            _reinitializeJPA("liferaySessionFactory", newDataSource);
111                    }
112                    else {
113                            if (_log.isInfoEnabled()) {
114                                    _log.info("Reinitialize JPA for new liferay data source");
115                            }
116    
117                            _reinitializeHibernate("liferaySessionFactory", newDataSource);
118                    }
119            }
120    
121            public void setCounterDataSourceWrapper(
122                    DataSourceWrapper counterDataSourceWrapper) {
123    
124                    _counterDataSourceWrapper = counterDataSourceWrapper;
125            }
126    
127            public void setLiferayDataSourceWrapper(
128                    DataSourceWrapper liferayDataSourceWrapper) {
129    
130                    _liferayDataSourceWrapper = liferayDataSourceWrapper;
131            }
132    
133            private static void _reinitializeHibernate(
134                            String name, DataSource dataSource)
135                    throws Exception {
136    
137                    PortalHibernateConfiguration portalHibernateConfiguration =
138                            new PortalHibernateConfiguration();
139    
140                    portalHibernateConfiguration.setDataSource(dataSource);
141    
142                    portalHibernateConfiguration.afterPropertiesSet();
143    
144                    SessionFactoryImplementor sessionFactoryImplementor =
145                            (SessionFactoryImplementor)portalHibernateConfiguration.getObject();
146    
147                    SessionFactoryImpl sessionFactoryImpl =
148                            (SessionFactoryImpl)PortalBeanLocatorUtil.locate(name);
149    
150                    sessionFactoryImpl.setSessionFactoryImplementor(
151                            sessionFactoryImplementor);
152            }
153    
154            private static void _reinitializeJPA(String name, DataSource dataSource)
155                    throws Exception {
156    
157                    LocalContainerEntityManagerFactoryBean
158                            localContainerEntityManagerFactoryBean =
159                                    new LocalContainerEntityManagerFactoryBean();
160    
161                    localContainerEntityManagerFactoryBean.setDataSource(dataSource);
162    
163                    localContainerEntityManagerFactoryBean.afterPropertiesSet();
164    
165                    EntityManagerFactory entityManagerFactory =
166                            localContainerEntityManagerFactoryBean.getObject();
167    
168                    com.liferay.portal.dao.orm.jpa.SessionFactoryImpl sessionFactoryImpl =
169                            (com.liferay.portal.dao.orm.jpa.SessionFactoryImpl)
170                                    PortalBeanLocatorUtil.locate(name);
171    
172                    sessionFactoryImpl.setEntityManagerFactory(entityManagerFactory);
173            }
174    
175            private static Log _log = LogFactoryUtil.getLog(DataSourceSwapper.class);
176    
177            private static DataSourceWrapper _counterDataSourceWrapper;
178            private static DataSourceWrapper _liferayDataSourceWrapper;
179    
180    }