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.kernel.dao.orm.ClassLoaderSession;
018    import com.liferay.portal.kernel.dao.orm.Dialect;
019    import com.liferay.portal.kernel.dao.orm.ORMException;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.SessionFactory;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.sql.Connection;
027    
028    import java.util.List;
029    import java.util.concurrent.CopyOnWriteArrayList;
030    
031    import org.hibernate.engine.SessionFactoryImplementor;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Shuyang Zhou
036     */
037    public class SessionFactoryImpl implements SessionFactory {
038    
039            public static List<PortletSessionFactoryImpl> getPortletSessionFactories() {
040                    return portletSessionFactories;
041            }
042    
043            public void closeSession(Session session) throws ORMException {
044                    if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
045                            session.close();
046                    }
047            }
048    
049            public void destroy() {
050                    portletSessionFactories.clear();
051            }
052    
053            public Dialect getDialect() throws ORMException {
054                    return new DialectImpl(_sessionFactoryImplementor.getDialect());
055            }
056    
057            public ClassLoader getSessionFactoryClassLoader() {
058                    return _sessionFactoryClassLoader;
059            }
060    
061            public SessionFactoryImplementor getSessionFactoryImplementor() {
062                    return _sessionFactoryImplementor;
063            }
064    
065            public Session openNewSession(Connection connection) throws ORMException {
066                    return wrapSession(_sessionFactoryImplementor.openSession(connection));
067            }
068    
069            public Session openSession() throws ORMException {
070                    org.hibernate.Session session = null;
071    
072                    if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
073                            session = _sessionFactoryImplementor.getCurrentSession();
074                    }
075                    else {
076                            session = _sessionFactoryImplementor.openSession();
077                    }
078    
079                    if (_log.isDebugEnabled()) {
080                            org.hibernate.impl.SessionImpl sessionImpl =
081                                    (org.hibernate.impl.SessionImpl)session;
082    
083                            _log.debug(
084                                    "Session is using connection release mode " +
085                                            sessionImpl.getConnectionReleaseMode());
086                    }
087    
088                    return wrapSession(session);
089            }
090    
091            public void setSessionFactoryClassLoader(
092                    ClassLoader sessionFactoryClassLoader) {
093    
094                    _sessionFactoryClassLoader = sessionFactoryClassLoader;
095            }
096    
097            public void setSessionFactoryImplementor(
098                    SessionFactoryImplementor sessionFactoryImplementor) {
099    
100                    _sessionFactoryImplementor = sessionFactoryImplementor;
101            }
102    
103            protected Session wrapSession(org.hibernate.Session session) {
104                    Session liferaySession = new SessionImpl(session);
105    
106                    if (_sessionFactoryClassLoader != null) {
107    
108                            // LPS-4190
109    
110                            liferaySession = new ClassLoaderSession(
111                                    liferaySession, _sessionFactoryClassLoader);
112                    }
113    
114                    return liferaySession;
115            }
116    
117            private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
118    
119            protected static final List<PortletSessionFactoryImpl>
120                    portletSessionFactories =
121                            new CopyOnWriteArrayList<PortletSessionFactoryImpl>();
122    
123            private ClassLoader _sessionFactoryClassLoader;
124            private SessionFactoryImplementor _sessionFactoryImplementor;
125    
126    }