001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
070                                    "counterSessionFactory", newDataSource);
071                    }
072                    else {
073                            if (_log.isInfoEnabled()) {
074                                    _log.info("Reinitialize JPA for new counter data source");
075                            }
076    
077                            _reinitializeHibernate(
078                                    "counterSessionFactory", newDataSource);
079                    }
080            }
081    
082            public static void swapLiferayDataSource(Properties properties)
083                    throws Exception {
084    
085                    if (_log.isInfoEnabled()) {
086                            _log.info("Create new liferay data source");
087                    }
088    
089                    DataSource newDataSource = DataSourceFactoryUtil.initDataSource(
090                            properties);
091    
092                    DataSource oldDataSource =
093                            _liferayDataSourceWrapper.getWrappedDataSource();
094    
095                    if (_log.isInfoEnabled()) {
096                            _log.info("Set new liferay data source");
097                    }
098    
099                    _liferayDataSourceWrapper.setWrappedDataSource(newDataSource);
100    
101                    if (_log.isInfoEnabled()) {
102                            _log.info("Destroy old liferay data source");
103                    }
104    
105                    DataSourceFactoryUtil.destroyDataSource(oldDataSource);
106    
107                    if (PropsValues.PERSISTENCE_PROVIDER.equalsIgnoreCase("jpa")) {
108                            if (_log.isInfoEnabled()) {
109                                    _log.info("Reinitialize Hibernate for new liferay data source");
110                            }
111    
112                            _reinitializeJPA(
113                                    "liferaySessionFactory", newDataSource);
114                    }
115                    else {
116                            if (_log.isInfoEnabled()) {
117                                    _log.info("Reinitialize JPA for new liferay data source");
118                            }
119    
120                            _reinitializeHibernate(
121                                    "liferaySessionFactory", newDataSource);
122                    }
123            }
124    
125            public void setCounterDataSourceWrapper(
126                    DataSourceWrapper counterDataSourceWrapper) {
127    
128                    _counterDataSourceWrapper = counterDataSourceWrapper;
129            }
130    
131            public void setLiferayDataSourceWrapper(
132                    DataSourceWrapper liferayDataSourceWrapper) {
133    
134                    _liferayDataSourceWrapper = liferayDataSourceWrapper;
135            }
136    
137            private static void _reinitializeHibernate(
138                            String name, DataSource dataSource)
139                    throws Exception {
140    
141                    PortalHibernateConfiguration portalHibernateConfiguration =
142                            new PortalHibernateConfiguration();
143    
144                    portalHibernateConfiguration.setDataSource(dataSource);
145    
146                    portalHibernateConfiguration.afterPropertiesSet();
147    
148                    SessionFactoryImplementor sessionFactoryImplementor =
149                            (SessionFactoryImplementor)portalHibernateConfiguration.getObject();
150    
151                    SessionFactoryImpl sessionFactoryImpl =
152                            (SessionFactoryImpl)PortalBeanLocatorUtil.locate(name);
153    
154                    sessionFactoryImpl.setSessionFactoryImplementor(
155                            sessionFactoryImplementor);
156            }
157    
158            private static void _reinitializeJPA(
159                            String name, DataSource dataSource)
160                    throws Exception {
161    
162                    LocalContainerEntityManagerFactoryBean
163                            localContainerEntityManagerFactoryBean =
164                                    new LocalContainerEntityManagerFactoryBean();
165    
166                    localContainerEntityManagerFactoryBean.setDataSource(dataSource);
167    
168                    localContainerEntityManagerFactoryBean.afterPropertiesSet();
169    
170                    EntityManagerFactory entityManagerFactory =
171                            localContainerEntityManagerFactoryBean.getObject();
172    
173                    com.liferay.portal.dao.orm.jpa.SessionFactoryImpl sessionFactoryImpl =
174                            (com.liferay.portal.dao.orm.jpa.SessionFactoryImpl)
175                                    PortalBeanLocatorUtil.locate(name);
176    
177                    sessionFactoryImpl.setEntityManagerFactory(entityManagerFactory);
178            }
179    
180            private static Log _log = LogFactoryUtil.getLog(DataSourceSwapper.class);
181    
182            private static DataSourceWrapper _counterDataSourceWrapper;
183            private static DataSourceWrapper _liferayDataSourceWrapper;
184    
185    }