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