001    /**
002     * Copyright (c) 2000-2013 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            @Override
044            public void closeSession(Session session) throws ORMException {
045                    if ((session != null) &&
046                            !PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
047    
048                            session.close();
049                    }
050            }
051    
052            public void destroy() {
053                    portletSessionFactories.clear();
054            }
055    
056            @Override
057            public Dialect getDialect() throws ORMException {
058                    return new DialectImpl(_sessionFactoryImplementor.getDialect());
059            }
060    
061            public ClassLoader getSessionFactoryClassLoader() {
062                    return _sessionFactoryClassLoader;
063            }
064    
065            public SessionFactoryImplementor getSessionFactoryImplementor() {
066                    return _sessionFactoryImplementor;
067            }
068    
069            @Override
070            public Session openNewSession(Connection connection) throws ORMException {
071                    return wrapSession(_sessionFactoryImplementor.openSession(connection));
072            }
073    
074            @Override
075            public Session openSession() throws ORMException {
076                    org.hibernate.Session session = null;
077    
078                    if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
079                            session = _sessionFactoryImplementor.getCurrentSession();
080                    }
081                    else {
082                            session = _sessionFactoryImplementor.openSession();
083                    }
084    
085                    if (_log.isDebugEnabled()) {
086                            org.hibernate.impl.SessionImpl sessionImpl =
087                                    (org.hibernate.impl.SessionImpl)session;
088    
089                            _log.debug(
090                                    "Session is using connection release mode " +
091                                            sessionImpl.getConnectionReleaseMode());
092                    }
093    
094                    return wrapSession(session);
095            }
096    
097            public void setSessionFactoryClassLoader(
098                    ClassLoader sessionFactoryClassLoader) {
099    
100                    _sessionFactoryClassLoader = sessionFactoryClassLoader;
101            }
102    
103            public void setSessionFactoryImplementor(
104                    SessionFactoryImplementor sessionFactoryImplementor) {
105    
106                    _sessionFactoryImplementor = sessionFactoryImplementor;
107            }
108    
109            protected Session wrapSession(org.hibernate.Session session) {
110                    Session liferaySession = new SessionImpl(session);
111    
112                    if (_sessionFactoryClassLoader != null) {
113    
114                            // LPS-4190
115    
116                            liferaySession = new ClassLoaderSession(
117                                    liferaySession, _sessionFactoryClassLoader);
118                    }
119    
120                    return liferaySession;
121            }
122    
123            protected static final List<PortletSessionFactoryImpl>
124                    portletSessionFactories =
125                            new CopyOnWriteArrayList<PortletSessionFactoryImpl>();
126    
127            private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
128    
129            private ClassLoader _sessionFactoryClassLoader;
130            private SessionFactoryImplementor _sessionFactoryImplementor;
131    
132    }