1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 java.util.Map;
32  import java.util.Properties;
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   * @author Marcellus Tavares
43   */
44  public class PortalHibernateConfiguration
45      extends TransactionAwareConfiguration {
46  
47      public void setHibernateConfigurationConverter(
48          Converter<String> hibernateConfigurationConverter) {
49  
50          _hibernateConfigurationConverter = hibernateConfigurationConverter;
51      }
52  
53      protected String determineDialect() {
54          return DialectDetector.determineDialect(getDataSource());
55      }
56  
57      protected ClassLoader getConfigurationClassLoader() {
58          return getClass().getClassLoader();
59      }
60  
61      protected String[] getConfigurationResources() {
62          return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
63      }
64  
65      protected Configuration newConfiguration() {
66          Configuration configuration = new Configuration();
67  
68          try {
69              String[] resources = getConfigurationResources();
70  
71              for (String resource : resources) {
72                  try {
73                      readResource(configuration, resource);
74                  }
75                  catch (Exception e2) {
76                      if (_log.isWarnEnabled()) {
77                          _log.warn(e2, e2);
78                      }
79                  }
80              }
81  
82              configuration.setProperties(PropsUtil.getProperties());
83  
84              if (Validator.isNull(PropsValues.HIBERNATE_DIALECT)) {
85                  String dialect = determineDialect();
86  
87                  configuration.setProperty("hibernate.dialect", dialect);
88              }
89  
90              DB db = DBFactoryUtil.getDB();
91  
92              if (db.getType().equals(DB.TYPE_HYPERSONIC)) {
93                  //configuration.setProperty("hibernate.jdbc.batch_size", "0");
94              }
95          }
96          catch (Exception e1) {
97              _log.error(e1, e1);
98          }
99  
100         Properties hibernateProperties = getHibernateProperties();
101 
102         if (hibernateProperties != null) {
103             for (Map.Entry<Object, Object> entry :
104                     hibernateProperties.entrySet()) {
105 
106                 String key = (String)entry.getKey();
107                 String value = (String)entry.getValue();
108 
109                 configuration.setProperty(key, value);
110             }
111         }
112 
113         return configuration;
114     }
115 
116     protected void postProcessConfiguration(Configuration configuration) {
117 
118         // Make sure that the Hibernate settings from PropsUtil are set. See the
119         // buildSessionFactory implementation in the LocalSessionFactoryBean
120         // class to understand how Spring automates a lot of configuration for
121         // Hibernate.
122 
123         String connectionReleaseMode = PropsUtil.get(
124             Environment.RELEASE_CONNECTIONS);
125 
126         if (Validator.isNotNull(connectionReleaseMode)) {
127             configuration.setProperty(
128                 Environment.RELEASE_CONNECTIONS, connectionReleaseMode);
129         }
130     }
131 
132     protected void readResource(Configuration configuration, String resource)
133         throws Exception {
134 
135         ClassLoader classLoader = getConfigurationClassLoader();
136 
137         InputStream is = classLoader.getResourceAsStream(resource);
138 
139         if (is == null) {
140             return;
141         }
142 
143         if (_hibernateConfigurationConverter != null) {
144             String configurationString = StringUtil.read(is);
145 
146             is.close();
147 
148             configurationString = _hibernateConfigurationConverter.convert(
149                 configurationString);
150 
151             is = new UnsyncByteArrayInputStream(
152                 configurationString.getBytes());
153         }
154 
155         configuration = configuration.addInputStream(is);
156 
157         is.close();
158     }
159 
160     private static Log _log = LogFactoryUtil.getLog(
161         PortalHibernateConfiguration.class);
162 
163     private Converter<String> _hibernateConfigurationConverter;
164 
165 }