001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.spring.hibernate;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.bean.BeanLocatorUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.util.PropsUtil;
027: import com.liferay.util.CollectionFactory;
028: import com.liferay.util.dao.hibernate.LiferayClassicSession;
029:
030: import java.sql.Connection;
031: import java.sql.SQLException;
032:
033: import java.util.Map;
034:
035: import javax.sql.DataSource;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: import org.hibernate.HibernateException;
041: import org.hibernate.Session;
042: import org.hibernate.SessionFactory;
043: import org.hibernate.dialect.Dialect;
044: import org.hibernate.engine.SessionFactoryImplementor;
045: import org.hibernate.impl.SessionImpl;
046:
047: import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
048:
049: /**
050: * <a href="HibernateUtil.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Brian Wing Shun Chan
053: *
054: */
055: public class HibernateUtil {
056:
057: public static final String COUNT_COLUMN_NAME = "COUNT_VALUE";
058:
059: public static final String SPRING_HIBERNATE_DATA_SOURCE = PropsUtil
060: .get(PropsUtil.SPRING_HIBERNATE_DATA_SOURCE);
061:
062: public static final String SPRING_HIBERNATE_SESSION_FACTORY = PropsUtil
063: .get(PropsUtil.SPRING_HIBERNATE_SESSION_FACTORY);
064:
065: public static void closeSession(Session session) {
066: try {
067: if (session != null) {
068:
069: // Let Spring manage sessions
070:
071: //session.close();
072: }
073: } catch (HibernateException he) {
074: _log.error(he.getMessage());
075: }
076: }
077:
078: public static Connection getConnection() throws SQLException {
079: return getDataSource().getConnection();
080: }
081:
082: public static String getCountColumnName() {
083: return COUNT_COLUMN_NAME;
084: }
085:
086: public static DataSource getDataSource() {
087: if (_dataSource == null) {
088: _dataSource = (DataSource) BeanLocatorUtil
089: .locate(SPRING_HIBERNATE_DATA_SOURCE);
090: }
091:
092: return _dataSource;
093: }
094:
095: public static Dialect getDialect() {
096: return getDialect(SPRING_HIBERNATE_SESSION_FACTORY);
097: }
098:
099: public static Dialect getDialect(String sessionFactoryName) {
100: return getSessionFactory(sessionFactoryName).getDialect();
101: }
102:
103: public static SessionFactoryImplementor getSessionFactory() {
104: return getSessionFactory(SPRING_HIBERNATE_SESSION_FACTORY);
105: }
106:
107: public static SessionFactoryImplementor getSessionFactory(
108: String sessionFactoryName) {
109:
110: if (sessionFactoryName.equals(SPRING_HIBERNATE_SESSION_FACTORY)) {
111: if (_sessionFactory == null) {
112: LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) BeanLocatorUtil
113: .locate(sessionFactoryName);
114:
115: _sessionFactory = (SessionFactoryImplementor) lsfb
116: .getObject();
117: }
118:
119: return _sessionFactory;
120: } else {
121: SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) _sessionFactories
122: .get(sessionFactoryName);
123:
124: if (sessionFactory == null) {
125: LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) BeanLocatorUtil
126: .locate(sessionFactoryName);
127:
128: sessionFactory = (SessionFactoryImplementor) lsfb
129: .getObject();
130:
131: _sessionFactories.put(sessionFactoryName,
132: sessionFactory);
133: }
134:
135: return sessionFactory;
136: }
137: }
138:
139: public static Dialect getWrappedDialect() {
140: return getWrappedDialect(SPRING_HIBERNATE_SESSION_FACTORY);
141: }
142:
143: public static Dialect getWrappedDialect(String sessionFactoryName) {
144: Dialect dialect = getSessionFactory(sessionFactoryName)
145: .getDialect();
146:
147: if (dialect instanceof DynamicDialect) {
148: DynamicDialect dynamicDialect = (DynamicDialect) dialect;
149:
150: return dynamicDialect.getWrappedDialect();
151: } else {
152: return dialect;
153: }
154: }
155:
156: public static Session openSession() throws HibernateException {
157: return openSession(SPRING_HIBERNATE_SESSION_FACTORY);
158: }
159:
160: public static Session openSession(String sessionFactoryName)
161: throws HibernateException {
162:
163: if (Validator.isNull(sessionFactoryName)) {
164: return openSession();
165: }
166:
167: SessionFactoryImplementor sessionFactory = getSessionFactory(sessionFactoryName);
168:
169: return openSession(sessionFactory);
170: }
171:
172: public static Session openSession(SessionFactory sessionFactory)
173: throws HibernateException {
174:
175: if (sessionFactory == null) {
176: return openSession();
177: }
178:
179: // Let Spring manage sessions
180:
181: Session session = sessionFactory.getCurrentSession();
182:
183: if (_log.isDebugEnabled()) {
184: LiferayClassicSession classicSession = (LiferayClassicSession) session;
185:
186: SessionImpl sessionImpl = (SessionImpl) classicSession
187: .getHibernateClassicSession();
188:
189: _log.debug("Session is using connection release mode "
190: + sessionImpl.getConnectionReleaseMode());
191: }
192:
193: return session;
194: }
195:
196: public static SystemException processException(Exception e) {
197: if (e instanceof HibernateException) {
198: _log.error("Caught HibernateException");
199: } else {
200: _log.error("Caught unexpected exception "
201: + e.getClass().getName());
202: }
203:
204: _log.error(e, e);
205:
206: return new SystemException(e);
207: }
208:
209: private static Log _log = LogFactory.getLog(HibernateUtil.class);
210:
211: private static DataSource _dataSource;
212: private static SessionFactoryImplementor _sessionFactory;
213: private static Map _sessionFactories = CollectionFactory
214: .getHashMap();
215:
216: }
|