001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 org.hibernate.engine.SessionFactoryImplementor;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Shuyang Zhou
033     */
034    public class SessionFactoryImpl implements SessionFactory {
035    
036            public void closeSession(Session session) throws ORMException {
037                    if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
038                            session.close();
039                    }
040            }
041    
042            public Dialect getDialect() throws ORMException {
043                    return new DialectImpl(_sessionFactoryImplementor.getDialect());
044            }
045    
046            public SessionFactoryImplementor getSessionFactoryImplementor() {
047                    return _sessionFactoryImplementor;
048            }
049    
050            public Session openNewSession(Connection connection) throws ORMException {
051                    return wrapSession(_sessionFactoryImplementor.openSession(connection));
052            }
053    
054            public Session openSession() throws ORMException {
055                    org.hibernate.Session session = null;
056    
057                    if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
058                            session = _sessionFactoryImplementor.getCurrentSession();
059                    }
060                    else {
061                            session = _sessionFactoryImplementor.openSession();
062                    }
063    
064                    if (_log.isDebugEnabled()) {
065                            org.hibernate.impl.SessionImpl sessionImpl =
066                                    (org.hibernate.impl.SessionImpl)session;
067    
068                            _log.debug(
069                                    "Session is using connection release mode " +
070                                            sessionImpl.getConnectionReleaseMode());
071                    }
072    
073                    return wrapSession(session);
074            }
075    
076            public void setSessionFactoryClassLoader(
077                    ClassLoader sessionFactoryClassLoader) {
078    
079                    _sessionFactoryClassLoader = sessionFactoryClassLoader;
080            }
081    
082            public void setSessionFactoryImplementor(
083                    SessionFactoryImplementor sessionFactoryImplementor) {
084    
085                    _sessionFactoryImplementor = sessionFactoryImplementor;
086            }
087    
088            protected Session wrapSession(org.hibernate.Session session) {
089                    Session liferaySession = new SessionImpl(session);
090    
091                    if (_sessionFactoryClassLoader != null) {
092    
093                            // LPS-4190
094    
095                            liferaySession = new ClassLoaderSession(
096                                    liferaySession, _sessionFactoryClassLoader);
097                    }
098    
099                    return liferaySession;
100            }
101    
102            private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
103    
104            private ClassLoader _sessionFactoryClassLoader;
105            private SessionFactoryImplementor _sessionFactoryImplementor;
106    
107    }