1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.orm.hibernate;
24  
25  import com.liferay.portal.kernel.bean.ContextClassLoaderBeanHandler;
26  import com.liferay.portal.kernel.dao.orm.Dialect;
27  import com.liferay.portal.kernel.dao.orm.ORMException;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.dao.orm.SessionFactory;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.util.PropsValues;
33  
34  import java.lang.reflect.Proxy;
35  
36  import java.sql.Connection;
37  
38  import org.hibernate.engine.SessionFactoryImplementor;
39  
40  /**
41   * <a href="SessionFactoryImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class SessionFactoryImpl implements SessionFactory {
46  
47      public void closeSession(Session session) throws ORMException {
48          if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
49              session.close();
50          }
51      }
52  
53      public Dialect getDialect() throws ORMException {
54          return new DialectImpl(_sessionFactoryImplementor.getDialect());
55      }
56  
57      public SessionFactoryImplementor getSessionFactoryImplementor() {
58          return _sessionFactoryImplementor;
59      }
60  
61      public Session openNewSession(Connection connection) throws ORMException {
62          return transformSession(
63              _sessionFactoryImplementor.openSession(connection));
64      }
65  
66      public Session openSession() throws ORMException {
67          org.hibernate.Session session = null;
68  
69          if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
70              session = _sessionFactoryImplementor.getCurrentSession();
71          }
72          else {
73              session = _sessionFactoryImplementor.openSession();
74          }
75  
76          if (_log.isDebugEnabled()) {
77              LiferayClassicSession classicSession =
78                  (LiferayClassicSession)session;
79  
80              org.hibernate.impl.SessionImpl sessionImpl =
81                  (org.hibernate.impl.SessionImpl)
82                      classicSession.getHibernateClassicSession();
83  
84              _log.debug(
85                  "Session is using connection release mode " +
86                      sessionImpl.getConnectionReleaseMode());
87          }
88  
89          return transformSession(session);
90      }
91  
92      public void setSessionFactoryClassLoader(
93          ClassLoader sessionFactoryClassLoader) {
94  
95          _sessionFactoryClassLoader = sessionFactoryClassLoader;
96      }
97  
98      public void setSessionFactoryImplementor(
99          SessionFactoryImplementor sessionFactoryImplementor) {
100 
101         _sessionFactoryImplementor = sessionFactoryImplementor;
102     }
103 
104     protected Session transformSession(org.hibernate.Session session) {
105         Session liferaySession = new SessionImpl(session);
106 
107         if (_sessionFactoryClassLoader != null) {
108 
109             // LPS-4190
110 
111             liferaySession = (Session)Proxy.newProxyInstance(
112                 _sessionFactoryClassLoader,
113                 new Class[] {Session.class},
114                 new ContextClassLoaderBeanHandler(
115                     liferaySession, _sessionFactoryClassLoader));
116         }
117 
118         return liferaySession;
119     }
120 
121     private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
122 
123     private ClassLoader _sessionFactoryClassLoader;
124     private SessionFactoryImplementor _sessionFactoryImplementor;
125 
126 }