1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortalHibernateConfiguration
44      extends TransactionAwareConfiguration {
45  
46      protected String determineDialect() {
47          return DialectDetector.determineDialect(getDataSource());
48      }
49  
50      protected ClassLoader getConfigurationClassLoader() {
51          return getClass().getClassLoader();
52      }
53  
54      protected String[] getConfigurationResources() {
55          return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
56      }
57  
58      protected Configuration newConfiguration() {
59          Configuration configuration = new Configuration();
60  
61          try {
62              ClassLoader classLoader = getConfigurationClassLoader();
63  
64              String[] resources = getConfigurationResources();
65  
66              for (String resource : resources) {
67                  try {
68                      InputStream is = classLoader.getResourceAsStream(resource);
69  
70                      if (is != null) {
71                          configuration = configuration.addInputStream(is);
72  
73                          is.close();
74                      }
75                  }
76                  catch (Exception e1) {
77                      if (_log.isWarnEnabled()) {
78                          _log.warn(e1);
79                      }
80                  }
81              }
82  
83              if (Validator.isNull(PropsValues.HIBERNATE_DIALECT)) {
84                  String dialect = determineDialect();
85  
86                  configuration.setProperty("hibernate.dialect", dialect);
87              }
88  
89              configuration.setProperties(PropsUtil.getProperties());
90          }
91          catch (Exception e2) {
92              _log.error(e2, e2);
93          }
94  
95          return configuration;
96      }
97  
98      protected void postProcessConfiguration(Configuration configuration) {
99  
100         // Make sure that the Hibernate settings from PropsUtil are set. See the
101         // buildSessionFactory implementation in the LocalSessionFactoryBean
102         // class to understand how Spring automates a lot of configuration for
103         // Hibernate.
104 
105         String connectionReleaseMode = PropsUtil.get(
106             Environment.RELEASE_CONNECTIONS);
107 
108         if (Validator.isNotNull(connectionReleaseMode)) {
109             configuration.setProperty(
110                 Environment.RELEASE_CONNECTIONS, connectionReleaseMode);
111         }
112     }
113 
114     private static Log _log =
115         LogFactoryUtil.getLog(PortalHibernateConfiguration.class);
116 
117 }