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.kernel.util.Validator;
26  import com.liferay.portal.util.PropsKeys;
27  import com.liferay.portal.util.PropsUtil;
28  import com.liferay.portal.util.PropsValues;
29  
30  import java.io.InputStream;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import org.hibernate.cfg.Configuration;
36  import org.hibernate.cfg.Environment;
37  
38  /**
39   * <a href="PortalHibernateConfiguration.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class PortalHibernateConfiguration
46      extends TransactionAwareConfiguration {
47  
48      protected String determineDialect() {
49          return DialectDetector.determineDialect(getDataSource());
50      }
51  
52      protected ClassLoader getConfigurationClassLoader() {
53          return getClass().getClassLoader();
54      }
55  
56      protected String[] getConfigurationResources() {
57          return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
58      }
59  
60      protected Configuration newConfiguration() {
61          Configuration configuration = new Configuration();
62  
63          try {
64              ClassLoader classLoader = getConfigurationClassLoader();
65  
66              String[] resources = getConfigurationResources();
67  
68              for (String resource : resources) {
69                  try {
70                      InputStream is = classLoader.getResourceAsStream(resource);
71  
72                      if (is != null) {
73                          configuration = configuration.addInputStream(is);
74  
75                          is.close();
76                      }
77                  }
78                  catch (Exception e1) {
79                      if (_log.isWarnEnabled()) {
80                          _log.warn(e1);
81                      }
82                  }
83              }
84  
85              if (Validator.isNull(PropsValues.HIBERNATE_DIALECT)) {
86                  String dialect = determineDialect();
87  
88                  configuration.setProperty("hibernate.dialect", dialect);
89              }
90  
91              configuration.setProperties(PropsUtil.getProperties());
92          }
93          catch (Exception e2) {
94              _log.error(e2, e2);
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     private static Log _log =
117         LogFactory.getLog(PortalHibernateConfiguration.class);
118 
119 }