1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.dao.orm.hibernate;
16  
17  import com.liferay.portal.kernel.dao.orm.ClassLoaderSession;
18  import com.liferay.portal.kernel.dao.orm.Dialect;
19  import com.liferay.portal.kernel.dao.orm.ORMException;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.SessionFactory;
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.spring.hibernate.SessionInvocationHandler;
25  import com.liferay.portal.util.PropsValues;
26  
27  import java.lang.reflect.Proxy;
28  
29  import java.sql.Connection;
30  
31  import org.hibernate.engine.SessionFactoryImplementor;
32  
33  /**
34   * <a href="SessionFactoryImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Shuyang Zhou
38   */
39  public class SessionFactoryImpl implements SessionFactory {
40  
41      public void closeSession(Session session) throws ORMException {
42          if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
43              session.close();
44          }
45      }
46  
47      public Dialect getDialect() throws ORMException {
48          return new DialectImpl(_sessionFactoryImplementor.getDialect());
49      }
50  
51      public SessionFactoryImplementor getSessionFactoryImplementor() {
52          return _sessionFactoryImplementor;
53      }
54  
55      public Session openNewSession(Connection connection) throws ORMException {
56          return wrapSession(_sessionFactoryImplementor.openSession(connection));
57      }
58  
59      public Session openSession() throws ORMException {
60          org.hibernate.Session session = null;
61  
62          if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
63              session = _sessionFactoryImplementor.getCurrentSession();
64          }
65          else {
66              session = _sessionFactoryImplementor.openSession();
67          }
68  
69          if (_log.isDebugEnabled()) {
70              SessionInvocationHandler sessionInvocationHandler =
71                  (SessionInvocationHandler)Proxy.getInvocationHandler(session);
72  
73              org.hibernate.impl.SessionImpl sessionImpl =
74                  (org.hibernate.impl.SessionImpl)
75                      sessionInvocationHandler.getSession();
76  
77              _log.debug(
78                  "Session is using connection release mode " +
79                      sessionImpl.getConnectionReleaseMode());
80          }
81  
82          return wrapSession(session);
83      }
84  
85      public void setSessionFactoryClassLoader(
86          ClassLoader sessionFactoryClassLoader) {
87  
88          _sessionFactoryClassLoader = sessionFactoryClassLoader;
89      }
90  
91      public void setSessionFactoryImplementor(
92          SessionFactoryImplementor sessionFactoryImplementor) {
93  
94          _sessionFactoryImplementor = sessionFactoryImplementor;
95      }
96  
97      protected Session wrapSession(org.hibernate.Session session) {
98          Session liferaySession = new SessionImpl(session);
99  
100         if (_sessionFactoryClassLoader != null) {
101 
102             // LPS-4190
103 
104             liferaySession = new ClassLoaderSession(
105                 liferaySession, _sessionFactoryClassLoader);
106         }
107 
108         return liferaySession;
109     }
110 
111     private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
112 
113     private ClassLoader _sessionFactoryClassLoader;
114     private SessionFactoryImplementor _sessionFactoryImplementor;
115 
116 }