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