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