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.ClassLoaderBeanHandler;
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.util.PropsValues;
25  
26  import java.lang.reflect.Proxy;
27  
28  import java.sql.Connection;
29  
30  import org.hibernate.engine.SessionFactoryImplementor;
31  
32  /**
33   * <a href="SessionFactoryImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class SessionFactoryImpl implements SessionFactory {
38  
39      public void closeSession(Session session) throws ORMException {
40          if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
41              session.close();
42          }
43      }
44  
45      public Dialect getDialect() throws ORMException {
46          return new DialectImpl(_sessionFactoryImplementor.getDialect());
47      }
48  
49      public SessionFactoryImplementor getSessionFactoryImplementor() {
50          return _sessionFactoryImplementor;
51      }
52  
53      public Session openNewSession(Connection connection) throws ORMException {
54          return wrapSession(_sessionFactoryImplementor.openSession(connection));
55      }
56  
57      public Session openSession() throws ORMException {
58          org.hibernate.Session session = null;
59  
60          if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
61              session = _sessionFactoryImplementor.getCurrentSession();
62          }
63          else {
64              session = _sessionFactoryImplementor.openSession();
65          }
66  
67          if (_log.isDebugEnabled()) {
68              org.hibernate.impl.SessionImpl sessionImpl =
69                  (org.hibernate.impl.SessionImpl)session;
70  
71              _log.debug(
72                  "Session is using connection release mode " +
73                      sessionImpl.getConnectionReleaseMode());
74          }
75  
76          return wrapSession(session);
77      }
78  
79      public void setSessionFactoryClassLoader(
80          ClassLoader sessionFactoryClassLoader) {
81  
82          _sessionFactoryClassLoader = sessionFactoryClassLoader;
83      }
84  
85      public void setSessionFactoryImplementor(
86          SessionFactoryImplementor sessionFactoryImplementor) {
87  
88          _sessionFactoryImplementor = sessionFactoryImplementor;
89      }
90  
91      protected Session wrapSession(org.hibernate.Session session) {
92          Session liferaySession = new SessionImpl(session);
93  
94          if (_sessionFactoryClassLoader != null) {
95  
96              // LPS-4190
97  
98              liferaySession = (Session)Proxy.newProxyInstance(
99                  _sessionFactoryClassLoader,
100                 new Class[] {Session.class},
101                 new ClassLoaderBeanHandler(
102                     liferaySession, _sessionFactoryClassLoader));
103         }
104 
105         return liferaySession;
106     }
107 
108     private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
109 
110     private ClassLoader _sessionFactoryClassLoader;
111     private SessionFactoryImplementor _sessionFactoryImplementor;
112 
113 }