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.portlet.PortletClassLoaderUtil;
024    import com.liferay.portal.kernel.util.InfrastructureUtil;
025    import com.liferay.portal.spring.hibernate.PortletHibernateConfiguration;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.sql.Connection;
029    
030    import java.util.HashMap;
031    import java.util.Map;
032    
033    import javax.sql.DataSource;
034    
035    import org.hibernate.SessionFactory;
036    
037    /**
038     * @author Shuyang Zhou
039     * @author Alexander Chow
040     */
041    public class PortletSessionFactoryImpl extends SessionFactoryImpl {
042    
043            public void afterPropertiesSet() {
044                    if (_dataSource == InfrastructureUtil.getDataSource()) {
045    
046                            // Register only if the current session factory is using the portal
047                            // data source
048    
049                            portletSessionFactories.add(this);
050                    }
051            }
052    
053            @Override
054            public void closeSession(Session session) throws ORMException {
055                    if (session != null) {
056                            session.flush();
057    
058                            if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
059                                    session.close();
060                            }
061                    }
062            }
063    
064            @Override
065            public void destroy() {
066                    portletSessionFactories.remove(this);
067            }
068    
069            public DataSource getDataSource() {
070                    ShardDataSourceTargetSource shardDataSourceTargetSource =
071                            (ShardDataSourceTargetSource)
072                                    InfrastructureUtil.getShardDataSourceTargetSource();
073    
074                    if (shardDataSourceTargetSource != null) {
075                            return shardDataSourceTargetSource.getDataSource();
076                    }
077                    else {
078                            return _dataSource;
079                    }
080            }
081    
082            @Override
083            public Session openSession() throws ORMException {
084                    SessionFactory sessionFactory = getSessionFactory();
085    
086                    org.hibernate.Session session = null;
087    
088                    if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
089                            Connection connection = CurrentConnectionUtil.getConnection(
090                                    getDataSource());
091    
092                            if (connection == null) {
093                                    session = sessionFactory.getCurrentSession();
094                            }
095                            else {
096                                    session = sessionFactory.openSession(connection);
097                            }
098                    }
099                    else {
100                            session = sessionFactory.openSession();
101                    }
102    
103                    if (_log.isDebugEnabled()) {
104                            org.hibernate.impl.SessionImpl sessionImpl =
105                                    (org.hibernate.impl.SessionImpl)session;
106    
107                            _log.debug(
108                                    "Session is using connection release mode " +
109                                            sessionImpl.getConnectionReleaseMode());
110                    }
111    
112                    return wrapSession(session);
113            }
114    
115            public void setDataSource(DataSource dataSource) {
116                    _dataSource = dataSource;
117            }
118    
119            protected SessionFactory getSessionFactory() {
120                    ShardDataSourceTargetSource shardDataSourceTargetSource =
121                            (ShardDataSourceTargetSource)
122                                    InfrastructureUtil.getShardDataSourceTargetSource();
123    
124                    if (shardDataSourceTargetSource == null) {
125                            return getSessionFactoryImplementor();
126                    }
127    
128                    DataSource dataSource = shardDataSourceTargetSource.getDataSource();
129    
130                    SessionFactory sessionFactory = _sessionFactories.get(dataSource);
131    
132                    if (sessionFactory != null) {
133                            return sessionFactory;
134                    }
135    
136                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
137    
138                    try {
139                            PortletClassLoaderUtil.setClassLoader(
140                                    getSessionFactoryClassLoader());
141    
142                            PortletHibernateConfiguration portletHibernateConfiguration =
143                                    new PortletHibernateConfiguration();
144    
145                            portletHibernateConfiguration.setDataSource(dataSource);
146    
147                            try {
148                                    sessionFactory =
149                                            portletHibernateConfiguration.buildSessionFactory();
150                            }
151                            catch (Exception e) {
152                                    _log.error(e, e);
153    
154                                    return null;
155                            }
156    
157                            _sessionFactories.put(dataSource, sessionFactory);
158    
159                            return sessionFactory;
160                    }
161                    finally {
162                            PortletClassLoaderUtil.setClassLoader(classLoader);
163                    }
164            }
165    
166            private static Log _log = LogFactoryUtil.getLog(
167                    PortletSessionFactoryImpl.class);
168    
169            private DataSource _dataSource;
170            private Map<DataSource, SessionFactory> _sessionFactories =
171                    new HashMap<DataSource, SessionFactory>();
172    
173    }