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.orm.hibernate;
016    
017    import com.liferay.portal.dao.shard.ShardDataSourceTargetSource;
018    import com.liferay.portal.kernel.dao.jdbc.CurrentConnectionUtil;
019    import com.liferay.portal.kernel.dao.orm.ORMException;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.InfrastructureUtil;
024    import com.liferay.portal.spring.hibernate.PortletHibernateConfiguration;
025    import com.liferay.portal.util.PropsValues;
026    
027    import java.sql.Connection;
028    
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    import javax.sql.DataSource;
033    
034    import org.hibernate.SessionFactory;
035    
036    /**
037     * @author Shuyang Zhou
038     * @author Alexander Chow
039     */
040    public class PortletSessionFactoryImpl extends SessionFactoryImpl {
041    
042            @Override
043            public Session openSession() throws ORMException {
044                    SessionFactory sessionFactory = getSessionFactory();
045    
046                    org.hibernate.Session session = null;
047    
048                    if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
049                            Connection connection = CurrentConnectionUtil.getConnection(
050                                    getDataSource());
051    
052                            if (connection == null) {
053                                    session = sessionFactory.getCurrentSession();
054                            }
055                            else {
056                                    session = sessionFactory.openSession(connection);
057                            }
058                    }
059                    else {
060                            session = sessionFactory.openSession();
061                    }
062    
063                    if (_log.isDebugEnabled()) {
064                            org.hibernate.impl.SessionImpl sessionImpl =
065                                    (org.hibernate.impl.SessionImpl)session;
066    
067                            _log.debug(
068                                    "Session is using connection release mode " +
069                                            sessionImpl.getConnectionReleaseMode());
070                    }
071    
072                    return wrapSession(session);
073            }
074    
075            public void setDataSource(DataSource dataSource) {
076                    _dataSource = dataSource;
077            }
078    
079            protected DataSource getDataSource() {
080                    ShardDataSourceTargetSource shardDataSourceTargetSource =
081                            (ShardDataSourceTargetSource)
082                                    InfrastructureUtil.getShardDataSourceTargetSource();
083    
084                    if (shardDataSourceTargetSource != null) {
085                            return shardDataSourceTargetSource.getDataSource();
086                    }
087                    else {
088                            return _dataSource;
089                    }
090            }
091    
092            protected SessionFactory getSessionFactory() {
093                    ShardDataSourceTargetSource shardDataSourceTargetSource =
094                            (ShardDataSourceTargetSource)
095                                    InfrastructureUtil.getShardDataSourceTargetSource();
096    
097                    if (shardDataSourceTargetSource == null) {
098                            return getSessionFactoryImplementor();
099                    }
100    
101                    DataSource dataSource = shardDataSourceTargetSource.getDataSource();
102    
103                    SessionFactory sessionFactory = _sessionFactories.get(dataSource);
104    
105                    if (sessionFactory != null) {
106                            return sessionFactory;
107                    }
108    
109                    PortletHibernateConfiguration portletHibernateConfiguration =
110                            new PortletHibernateConfiguration();
111    
112                    portletHibernateConfiguration.setDataSource(dataSource);
113    
114                    try {
115                            sessionFactory =
116                                    portletHibernateConfiguration.buildSessionFactory();
117                    }
118                    catch (Exception e) {
119                            _log.error(e, e);
120    
121                            return null;
122                    }
123    
124                    _sessionFactories.put(dataSource, sessionFactory);
125    
126                    return sessionFactory;
127            }
128    
129            private static Log _log = LogFactoryUtil.getLog(
130                    PortletSessionFactoryImpl.class);
131    
132            private DataSource _dataSource;
133            private Map<DataSource, SessionFactory> _sessionFactories =
134                    new HashMap<DataSource, SessionFactory>();
135    
136    }