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.kernel.util.PreloadClassLoader;
025    import com.liferay.portal.util.ClassLoaderUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.sql.Connection;
029    
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    import java.util.concurrent.CopyOnWriteArrayList;
034    
035    import org.hibernate.engine.SessionFactoryImplementor;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Shuyang Zhou
040     */
041    public class SessionFactoryImpl implements SessionFactory {
042    
043            public static List<PortletSessionFactoryImpl> getPortletSessionFactories() {
044                    return portletSessionFactories;
045            }
046    
047            public void closeSession(Session session) throws ORMException {
048                    if ((session != null) &&
049                            !PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
050    
051                            session.flush();
052                            session.close();
053                    }
054            }
055    
056            public void destroy() {
057                    portletSessionFactories.clear();
058            }
059    
060            public Session getCurrentSession() throws ORMException {
061                    return wrapSession(_sessionFactoryImplementor.getCurrentSession());
062            }
063    
064            public Dialect getDialect() throws ORMException {
065                    return new DialectImpl(_sessionFactoryImplementor.getDialect());
066            }
067    
068            public ClassLoader getSessionFactoryClassLoader() {
069                    return _sessionFactoryClassLoader;
070            }
071    
072            public SessionFactoryImplementor getSessionFactoryImplementor() {
073                    return _sessionFactoryImplementor;
074            }
075    
076            public Session openNewSession(Connection connection) throws ORMException {
077                    return wrapSession(_sessionFactoryImplementor.openSession(connection));
078            }
079    
080            public Session openSession() throws ORMException {
081                    org.hibernate.Session session = null;
082    
083                    if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
084                            session = _sessionFactoryImplementor.getCurrentSession();
085                    }
086                    else {
087                            session = _sessionFactoryImplementor.openSession();
088                    }
089    
090                    if (_log.isDebugEnabled()) {
091                            org.hibernate.impl.SessionImpl sessionImpl =
092                                    (org.hibernate.impl.SessionImpl)session;
093    
094                            _log.debug(
095                                    "Session is using connection release mode " +
096                                            sessionImpl.getConnectionReleaseMode());
097                    }
098    
099                    return wrapSession(session);
100            }
101    
102            public void setSessionFactoryClassLoader(
103                    ClassLoader sessionFactoryClassLoader) {
104    
105                    ClassLoader portalClassLoader = ClassLoaderUtil.getPortalClassLoader();
106    
107                    if (sessionFactoryClassLoader == portalClassLoader) {
108                            _sessionFactoryClassLoader = sessionFactoryClassLoader;
109                    }
110                    else {
111                            _sessionFactoryClassLoader = new PreloadClassLoader(
112                                    sessionFactoryClassLoader, getPreloadClassLoaderClasses());
113                    }
114            }
115    
116            public void setSessionFactoryImplementor(
117                    SessionFactoryImplementor sessionFactoryImplementor) {
118    
119                    _sessionFactoryImplementor = sessionFactoryImplementor;
120            }
121    
122            protected Map<String, Class<?>> getPreloadClassLoaderClasses() {
123                    try {
124                            Map<String, Class<?>> classes = new HashMap<String, Class<?>>();
125    
126                            for (String className : _PRELOAD_CLASS_NAMES) {
127                                    ClassLoader portalClassLoader =
128                                            ClassLoaderUtil.getPortalClassLoader();
129    
130                                    Class<?> clazz = portalClassLoader.loadClass(className);
131    
132                                    classes.put(className, clazz);
133                            }
134    
135                            return classes;
136                    }
137                    catch (ClassNotFoundException cnfe) {
138                            throw new RuntimeException(cnfe);
139                    }
140            }
141    
142            protected Session wrapSession(org.hibernate.Session session) {
143                    Session liferaySession = new SessionImpl(session);
144    
145                    if (_sessionFactoryClassLoader != null) {
146    
147                            // LPS-4190
148    
149                            liferaySession = new ClassLoaderSession(
150                                    liferaySession, _sessionFactoryClassLoader);
151                    }
152    
153                    return liferaySession;
154            }
155    
156            private static final String[] _PRELOAD_CLASS_NAMES =
157                    PropsValues.
158                            SPRING_HIBERNATE_SESSION_FACTORY_PRELOAD_CLASSLOADER_CLASSES;
159    
160            private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
161    
162            protected static final List<PortletSessionFactoryImpl>
163                    portletSessionFactories =
164                            new CopyOnWriteArrayList<PortletSessionFactoryImpl>();
165    
166            private ClassLoader _sessionFactoryClassLoader;
167            private SessionFactoryImplementor _sessionFactoryImplementor;
168    
169    }