001
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.security.pacl.PACLClassLoaderUtil;
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
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 =
106 PACLClassLoaderUtil.getPortalClassLoader();
107
108 if (sessionFactoryClassLoader == portalClassLoader) {
109 _sessionFactoryClassLoader = sessionFactoryClassLoader;
110 }
111 else {
112 _sessionFactoryClassLoader = new PreloadClassLoader(
113 sessionFactoryClassLoader, getPreloadClassLoaderClasses());
114 }
115 }
116
117 public void setSessionFactoryImplementor(
118 SessionFactoryImplementor sessionFactoryImplementor) {
119
120 _sessionFactoryImplementor = sessionFactoryImplementor;
121 }
122
123 protected Map<String, Class<?>> getPreloadClassLoaderClasses() {
124 try {
125 Map<String, Class<?>> classes = new HashMap<String, Class<?>>();
126
127 for (String className : _PRELOAD_CLASS_NAMES) {
128 ClassLoader portalClassLoader =
129 PACLClassLoaderUtil.getPortalClassLoader();
130
131 Class<?> clazz = portalClassLoader.loadClass(className);
132
133 classes.put(className, clazz);
134 }
135
136 return classes;
137 }
138 catch (ClassNotFoundException cnfe) {
139 throw new RuntimeException(cnfe);
140 }
141 }
142
143 protected Session wrapSession(org.hibernate.Session session) {
144 Session liferaySession = new SessionImpl(session);
145
146 if (_sessionFactoryClassLoader != null) {
147
148
149
150 liferaySession = new ClassLoaderSession(
151 liferaySession, _sessionFactoryClassLoader);
152 }
153
154 return liferaySession;
155 }
156
157 private static final String[] _PRELOAD_CLASS_NAMES =
158 PropsValues.
159 SPRING_HIBERNATE_SESSION_FACTORY_PRELOAD_CLASSLOADER_CLASSES;
160
161 private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
162
163 protected static final List<PortletSessionFactoryImpl>
164 portletSessionFactories =
165 new CopyOnWriteArrayList<PortletSessionFactoryImpl>();
166
167 private ClassLoader _sessionFactoryClassLoader;
168 private SessionFactoryImplementor _sessionFactoryImplementor;
169
170 }