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