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.spring.hibernate;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.Converter;
23  import com.liferay.portal.kernel.util.PropsKeys;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.util.PropsUtil;
27  import com.liferay.portal.util.PropsValues;
28  
29  import java.io.InputStream;
30  
31  import org.hibernate.cfg.Configuration;
32  import org.hibernate.cfg.Environment;
33  
34  import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
35  
36  /**
37   * <a href="PortalHibernateConfiguration.java.html"><b><i>View Source</i></b>
38   * </a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class PortalHibernateConfiguration extends LocalSessionFactoryBean {
43  
44      public void setHibernateConfigurationConverter(
45          Converter<String> hibernateConfigurationConverter) {
46  
47          _hibernateConfigurationConverter = hibernateConfigurationConverter;
48      }
49  
50      protected String determineDialect() {
51          return DialectDetector.determineDialect(getDataSource());
52      }
53  
54      protected ClassLoader getConfigurationClassLoader() {
55          return getClass().getClassLoader();
56      }
57  
58      protected String[] getConfigurationResources() {
59          return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
60      }
61  
62      protected Configuration newConfiguration() {
63          Configuration configuration = new Configuration();
64  
65          try {
66              String[] resources = getConfigurationResources();
67  
68              for (String resource : resources) {
69                  try {
70                      readResource(configuration, resource);
71                  }
72                  catch (Exception e2) {
73                      if (_log.isWarnEnabled()) {
74                          _log.warn(e2, e2);
75                      }
76                  }
77              }
78  
79              configuration.setProperties(PropsUtil.getProperties());
80  
81              if (Validator.isNull(PropsValues.HIBERNATE_DIALECT)) {
82                  String dialect = determineDialect();
83  
84                  configuration.setProperty("hibernate.dialect", dialect);
85              }
86  
87              DB db = DBFactoryUtil.getDB();
88  
89              if (db.getType().equals(DB.TYPE_HYPERSONIC)) {
90                  //configuration.setProperty("hibernate.jdbc.batch_size", "0");
91              }
92          }
93          catch (Exception e1) {
94              _log.error(e1, e1);
95          }
96  
97          return configuration;
98      }
99  
100     protected void postProcessConfiguration(Configuration configuration) {
101 
102         // Make sure that the Hibernate settings from PropsUtil are set. See the
103         // buildSessionFactory implementation in the LocalSessionFactoryBean
104         // class to understand how Spring automates a lot of configuration for
105         // Hibernate.
106 
107         String connectionReleaseMode = PropsUtil.get(
108             Environment.RELEASE_CONNECTIONS);
109 
110         if (Validator.isNotNull(connectionReleaseMode)) {
111             configuration.setProperty(
112                 Environment.RELEASE_CONNECTIONS, connectionReleaseMode);
113         }
114     }
115 
116     protected void readResource(Configuration configuration, String resource)
117         throws Exception {
118 
119         ClassLoader classLoader = getConfigurationClassLoader();
120 
121         InputStream is = classLoader.getResourceAsStream(resource);
122 
123         if (is == null) {
124             return;
125         }
126 
127         if (_hibernateConfigurationConverter != null) {
128             String configurationString = StringUtil.read(is);
129 
130             is.close();
131 
132             configurationString = _hibernateConfigurationConverter.convert(
133                 configurationString);
134 
135             is = new UnsyncByteArrayInputStream(
136                 configurationString.getBytes());
137         }
138 
139         configuration = configuration.addInputStream(is);
140 
141         is.close();
142     }
143 
144     private static Log _log = LogFactoryUtil.getLog(
145         PortalHibernateConfiguration.class);
146 
147     private Converter<String> _hibernateConfigurationConverter;
148 
149 }