1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.orm.hibernate;
16  
17  import com.liferay.portal.kernel.bean.ContextClassLoaderBeanHandler;
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   */
38  public class SessionFactoryImpl implements SessionFactory {
39  
40      public void closeSession(Session session) throws ORMException {
41          if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
42              session.close();
43          }
44      }
45  
46      public Dialect getDialect() throws ORMException {
47          return new DialectImpl(_sessionFactoryImplementor.getDialect());
48      }
49  
50      public SessionFactoryImplementor getSessionFactoryImplementor() {
51          return _sessionFactoryImplementor;
52      }
53  
54      public Session openNewSession(Connection connection) throws ORMException {
55          return wrapSession(_sessionFactoryImplementor.openSession(connection));
56      }
57  
58      public Session openSession() throws ORMException {
59          org.hibernate.Session session = null;
60  
61          if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
62              session = _sessionFactoryImplementor.getCurrentSession();
63          }
64          else {
65              session = _sessionFactoryImplementor.openSession();
66          }
67  
68          if (_log.isDebugEnabled()) {
69              SessionInvocationHandler sessionInvocationHandler =
70                  (SessionInvocationHandler)Proxy.getInvocationHandler(session);
71  
72              org.hibernate.impl.SessionImpl sessionImpl =
73                  (org.hibernate.impl.SessionImpl)
74                      sessionInvocationHandler.getSession();
75  
76              _log.debug(
77                  "Session is using connection release mode " +
78                      sessionImpl.getConnectionReleaseMode());
79          }
80  
81          return wrapSession(session);
82      }
83  
84      public void setSessionFactoryClassLoader(
85          ClassLoader sessionFactoryClassLoader) {
86  
87          _sessionFactoryClassLoader = sessionFactoryClassLoader;
88      }
89  
90      public void setSessionFactoryImplementor(
91          SessionFactoryImplementor sessionFactoryImplementor) {
92  
93          _sessionFactoryImplementor = sessionFactoryImplementor;
94      }
95  
96      protected Session wrapSession(org.hibernate.Session session) {
97          Session liferaySession = new SessionImpl(session);
98  
99          if (_sessionFactoryClassLoader != null) {
100 
101             // LPS-4190
102 
103             liferaySession = (Session)Proxy.newProxyInstance(
104                 _sessionFactoryClassLoader,
105                 new Class[] {Session.class},
106                 new ContextClassLoaderBeanHandler(
107                     liferaySession, _sessionFactoryClassLoader));
108         }
109 
110         return liferaySession;
111     }
112 
113     private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
114 
115     private ClassLoader _sessionFactoryClassLoader;
116     private SessionFactoryImplementor _sessionFactoryImplementor;
117 
118 }