001    /**
002     * Copyright (c) 2000-2011 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.bean.ClassLoaderBeanHandler;
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.lang.reflect.Proxy;
027    
028    import java.sql.Connection;
029    
030    import org.hibernate.engine.SessionFactoryImplementor;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class SessionFactoryImpl implements SessionFactory {
036    
037            public void closeSession(Session session) throws ORMException {
038                    if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
039                            session.close();
040                    }
041            }
042    
043            public Dialect getDialect() throws ORMException {
044                    return new DialectImpl(_sessionFactoryImplementor.getDialect());
045            }
046    
047            public SessionFactoryImplementor getSessionFactoryImplementor() {
048                    return _sessionFactoryImplementor;
049            }
050    
051            public Session openNewSession(Connection connection) throws ORMException {
052                    return wrapSession(_sessionFactoryImplementor.openSession(connection));
053            }
054    
055            public Session openSession() throws ORMException {
056                    org.hibernate.Session session = null;
057    
058                    if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
059                            session = _sessionFactoryImplementor.getCurrentSession();
060                    }
061                    else {
062                            session = _sessionFactoryImplementor.openSession();
063                    }
064    
065                    if (_log.isDebugEnabled()) {
066                            org.hibernate.impl.SessionImpl sessionImpl =
067                                    (org.hibernate.impl.SessionImpl)session;
068    
069                            _log.debug(
070                                    "Session is using connection release mode " +
071                                            sessionImpl.getConnectionReleaseMode());
072                    }
073    
074                    return wrapSession(session);
075            }
076    
077            public void setSessionFactoryClassLoader(
078                    ClassLoader sessionFactoryClassLoader) {
079    
080                    _sessionFactoryClassLoader = sessionFactoryClassLoader;
081            }
082    
083            public void setSessionFactoryImplementor(
084                    SessionFactoryImplementor sessionFactoryImplementor) {
085    
086                    _sessionFactoryImplementor = sessionFactoryImplementor;
087            }
088    
089            protected Session wrapSession(org.hibernate.Session session) {
090                    Session liferaySession = new SessionImpl(session);
091    
092                    if (_sessionFactoryClassLoader != null) {
093    
094                            // LPS-4190
095    
096                            liferaySession = (Session)Proxy.newProxyInstance(
097                                    _sessionFactoryClassLoader,
098                                    new Class[] {Session.class},
099                                    new ClassLoaderBeanHandler(
100                                            liferaySession, _sessionFactoryClassLoader));
101                    }
102    
103                    return liferaySession;
104            }
105    
106            private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
107    
108            private ClassLoader _sessionFactoryClassLoader;
109            private SessionFactoryImplementor _sessionFactoryImplementor;
110    
111    }