001    /**
002     * Copyright (c) 2000-2012 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            public void afterPropertiesSet() {
043                    if (_dataSource == InfrastructureUtil.getDataSource()) {
044    
045                            // Register only if the current session factory is using the portal
046                            // data source
047    
048                            portletSessionFactories.add(this);
049                    }
050            }
051    
052            @Override
053            public void destroy() {
054                    portletSessionFactories.remove(this);
055            }
056    
057            public DataSource getDataSource() {
058                    ShardDataSourceTargetSource shardDataSourceTargetSource =
059                            (ShardDataSourceTargetSource)
060                                    InfrastructureUtil.getShardDataSourceTargetSource();
061    
062                    if (shardDataSourceTargetSource != null) {
063                            return shardDataSourceTargetSource.getDataSource();
064                    }
065                    else {
066                            return _dataSource;
067                    }
068            }
069    
070            @Override
071            public Session openSession() throws ORMException {
072                    SessionFactory sessionFactory = getSessionFactory();
073    
074                    org.hibernate.Session session = null;
075    
076                    if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
077                            Connection connection = CurrentConnectionUtil.getConnection(
078                                    getDataSource());
079    
080                            if (connection == null) {
081                                    session = sessionFactory.getCurrentSession();
082                            }
083                            else {
084                                    session = sessionFactory.openSession(connection);
085                            }
086                    }
087                    else {
088                            session = sessionFactory.openSession();
089                    }
090    
091                    if (_log.isDebugEnabled()) {
092                            org.hibernate.impl.SessionImpl sessionImpl =
093                                    (org.hibernate.impl.SessionImpl)session;
094    
095                            _log.debug(
096                                    "Session is using connection release mode " +
097                                            sessionImpl.getConnectionReleaseMode());
098                    }
099    
100                    return wrapSession(session);
101            }
102    
103            public void setDataSource(DataSource dataSource) {
104                    _dataSource = dataSource;
105            }
106    
107            protected SessionFactory getSessionFactory() {
108                    ShardDataSourceTargetSource shardDataSourceTargetSource =
109                            (ShardDataSourceTargetSource)
110                                    InfrastructureUtil.getShardDataSourceTargetSource();
111    
112                    if (shardDataSourceTargetSource == null) {
113                            return getSessionFactoryImplementor();
114                    }
115    
116                    DataSource dataSource = shardDataSourceTargetSource.getDataSource();
117    
118                    SessionFactory sessionFactory = _sessionFactories.get(dataSource);
119    
120                    if (sessionFactory != null) {
121                            return sessionFactory;
122                    }
123    
124                    PortletHibernateConfiguration portletHibernateConfiguration =
125                            new PortletHibernateConfiguration();
126    
127                    portletHibernateConfiguration.setDataSource(dataSource);
128    
129                    try {
130                            sessionFactory =
131                                    portletHibernateConfiguration.buildSessionFactory();
132                    }
133                    catch (Exception e) {
134                            _log.error(e, e);
135    
136                            return null;
137                    }
138    
139                    _sessionFactories.put(dataSource, sessionFactory);
140    
141                    return sessionFactory;
142            }
143    
144            private static Log _log = LogFactoryUtil.getLog(
145                    PortletSessionFactoryImpl.class);
146    
147            private DataSource _dataSource;
148            private Map<DataSource, SessionFactory> _sessionFactories =
149                    new HashMap<DataSource, SessionFactory>();
150    
151    }