1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.spring.hibernate;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.bean.BeanLocatorUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.util.PropsUtil;
29  import com.liferay.util.dao.hibernate.LiferayClassicSession;
30  
31  import java.sql.Connection;
32  import java.sql.SQLException;
33  
34  import java.util.HashMap;
35  import java.util.Map;
36  
37  import javax.sql.DataSource;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  import org.hibernate.HibernateException;
43  import org.hibernate.Session;
44  import org.hibernate.SessionFactory;
45  import org.hibernate.dialect.Dialect;
46  import org.hibernate.engine.SessionFactoryImplementor;
47  import org.hibernate.impl.SessionImpl;
48  
49  import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
50  
51  /**
52   * <a href="HibernateUtil.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class HibernateUtil {
58  
59      public static final String COUNT_COLUMN_NAME = "COUNT_VALUE";
60  
61      public static final String SPRING_HIBERNATE_DATA_SOURCE =
62          PropsUtil.get(PropsUtil.SPRING_HIBERNATE_DATA_SOURCE);
63  
64      public static final String SPRING_HIBERNATE_SESSION_FACTORY =
65          PropsUtil.get(PropsUtil.SPRING_HIBERNATE_SESSION_FACTORY);
66  
67      public static void closeSession(Session session) {
68          try {
69              if (session != null) {
70  
71                  // Let Spring manage sessions
72  
73                  //session.close();
74              }
75          }
76          catch (HibernateException he) {
77              _log.error(he.getMessage());
78          }
79      }
80  
81      public static Connection getConnection() throws SQLException {
82          return getDataSource().getConnection();
83      }
84  
85      public static String getCountColumnName() {
86          return COUNT_COLUMN_NAME;
87      }
88  
89      public static DataSource getDataSource() {
90          if (_dataSource == null) {
91              _dataSource = (DataSource)BeanLocatorUtil.locate(
92                  SPRING_HIBERNATE_DATA_SOURCE);
93          }
94  
95          return _dataSource;
96      }
97  
98      public static Dialect getDialect() {
99          return getDialect(SPRING_HIBERNATE_SESSION_FACTORY);
100     }
101 
102     public static Dialect getDialect(String sessionFactoryName) {
103         return getSessionFactory(sessionFactoryName).getDialect();
104     }
105 
106     public static SessionFactoryImplementor getSessionFactory() {
107         return getSessionFactory(SPRING_HIBERNATE_SESSION_FACTORY);
108     }
109 
110     public static SessionFactoryImplementor getSessionFactory(
111         String sessionFactoryName) {
112 
113         if (sessionFactoryName.equals(SPRING_HIBERNATE_SESSION_FACTORY)) {
114             if (_sessionFactory == null) {
115                 LocalSessionFactoryBean lsfb =
116                     (LocalSessionFactoryBean)BeanLocatorUtil.locate(
117                         sessionFactoryName);
118 
119                 _sessionFactory = (SessionFactoryImplementor)lsfb.getObject();
120             }
121 
122             return _sessionFactory;
123         }
124         else {
125             SessionFactoryImplementor sessionFactory = _sessionFactories.get(
126                 sessionFactoryName);
127 
128             if (sessionFactory == null) {
129                 LocalSessionFactoryBean lsfb =
130                     (LocalSessionFactoryBean)BeanLocatorUtil.locate(
131                         sessionFactoryName);
132 
133                 sessionFactory = (SessionFactoryImplementor)lsfb.getObject();
134 
135                 _sessionFactories.put(sessionFactoryName, sessionFactory);
136             }
137 
138             return sessionFactory;
139         }
140     }
141 
142     public static Dialect getWrappedDialect() {
143         return getWrappedDialect(SPRING_HIBERNATE_SESSION_FACTORY);
144     }
145 
146     public static Dialect getWrappedDialect(String sessionFactoryName) {
147         Dialect dialect = getSessionFactory(sessionFactoryName).getDialect();
148 
149         if (dialect instanceof DynamicDialect) {
150             DynamicDialect dynamicDialect = (DynamicDialect)dialect;
151 
152             return dynamicDialect.getWrappedDialect();
153         }
154         else {
155             return dialect;
156         }
157     }
158 
159     public static Session openSession() throws HibernateException {
160         return openSession(SPRING_HIBERNATE_SESSION_FACTORY);
161     }
162 
163     public static Session openSession(String sessionFactoryName)
164         throws HibernateException {
165 
166         if (Validator.isNull(sessionFactoryName)) {
167             return openSession();
168         }
169 
170         SessionFactoryImplementor sessionFactory =
171             getSessionFactory(sessionFactoryName);
172 
173         return openSession(sessionFactory);
174     }
175 
176     public static Session openSession(SessionFactory sessionFactory)
177         throws HibernateException {
178 
179         if (sessionFactory == null) {
180             return openSession();
181         }
182 
183         // Let Spring manage sessions
184 
185         Session session = sessionFactory.getCurrentSession();
186 
187         if (_log.isDebugEnabled()) {
188             LiferayClassicSession classicSession =
189                 (LiferayClassicSession)session;
190 
191             SessionImpl sessionImpl =
192                 (SessionImpl)classicSession.getHibernateClassicSession();
193 
194             _log.debug(
195                 "Session is using connection release mode " +
196                     sessionImpl.getConnectionReleaseMode());
197         }
198 
199         return session;
200     }
201 
202     public static SystemException processException(Exception e) {
203         if (e instanceof HibernateException) {
204             _log.error("Caught HibernateException");
205         }
206         else {
207             _log.error("Caught unexpected exception " + e.getClass().getName());
208         }
209 
210         _log.error(e, e);
211 
212         return new SystemException(e);
213     }
214 
215     private static Log _log = LogFactory.getLog(HibernateUtil.class);
216 
217     private static DataSource _dataSource;
218     private static SessionFactoryImplementor _sessionFactory;
219     private static Map<String, SessionFactoryImplementor> _sessionFactories =
220         new HashMap<String, SessionFactoryImplementor>();
221 
222 }