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  
29  import java.io.InputStream;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
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 ClassLoader getConfigurationClassLoader() {
48          return getClass().getClassLoader();
49      }
50  
51      protected String[] getConfigurationResources() {
52          return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
53      }
54  
55      protected Configuration newConfiguration() {
56          Configuration configuration = new Configuration();
57  
58          try {
59              ClassLoader classLoader = getConfigurationClassLoader();
60  
61              String[] resources = getConfigurationResources();
62  
63              for (String resource : resources) {
64                  try {
65                      InputStream is = classLoader.getResourceAsStream(resource);
66  
67                      if (is != null) {
68                          configuration = configuration.addInputStream(is);
69  
70                          is.close();
71                      }
72                  }
73                  catch (Exception e1) {
74                      if (_log.isWarnEnabled()) {
75                          _log.warn(e1);
76                      }
77                  }
78              }
79  
80              configuration.setProperties(PropsUtil.getProperties());
81          }
82          catch (Exception e2) {
83              _log.error(e2, e2);
84          }
85  
86          return configuration;
87      }
88  
89      protected void postProcessConfiguration(Configuration configuration) {
90  
91          // Make sure that the Hibernate settings from PropsUtil are set. See the
92          // buildSessionFactory implementation in the LocalSessionFactoryBean
93          // class to understand how Spring automates a lot of configuration for
94          // Hibernate.
95  
96          String connectionReleaseMode = PropsUtil.get(
97              Environment.RELEASE_CONNECTIONS);
98  
99          if (Validator.isNotNull(connectionReleaseMode)) {
100             configuration.setProperty(
101                 Environment.RELEASE_CONNECTIONS, connectionReleaseMode);
102         }
103     }
104 
105     private static Log _log =
106         LogFactory.getLog(PortalHibernateConfiguration.class);
107 
108 }