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.io.unsync.UnsyncByteArrayInputStream;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.Converter;
29  import com.liferay.portal.kernel.util.PropsKeys;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.util.PropsUtil;
33  import com.liferay.portal.util.PropsValues;
34  
35  import java.io.InputStream;
36  
37  import org.hibernate.cfg.Configuration;
38  import org.hibernate.cfg.Environment;
39  
40  /**
41   * <a href="PortalHibernateConfiguration.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class PortalHibernateConfiguration
47      extends TransactionAwareConfiguration {
48  
49      public void setHibernateConfigurationConverter(
50          Converter<String> hibernateConfigurationConverter) {
51  
52          _hibernateConfigurationConverter = hibernateConfigurationConverter;
53      }
54  
55      protected String determineDialect() {
56          return DialectDetector.determineDialect(getDataSource());
57      }
58  
59      protected ClassLoader getConfigurationClassLoader() {
60          return getClass().getClassLoader();
61      }
62  
63      protected String[] getConfigurationResources() {
64          return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
65      }
66  
67      protected Configuration newConfiguration() {
68          Configuration configuration = new Configuration();
69  
70          try {
71              String[] resources = getConfigurationResources();
72  
73              for (String resource : resources) {
74                  try {
75                      readResource(configuration, resource);
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     protected void readResource(Configuration configuration, String resource)
116         throws Exception {
117 
118         ClassLoader classLoader = getConfigurationClassLoader();
119 
120         InputStream is = classLoader.getResourceAsStream(resource);
121 
122         if (is == null) {
123             return;
124         }
125 
126         if (_hibernateConfigurationConverter != null) {
127             String configurationString = StringUtil.read(is);
128 
129             is.close();
130 
131             configurationString = _hibernateConfigurationConverter.convert(
132                 configurationString);
133 
134             is = new UnsyncByteArrayInputStream(
135                 configurationString.getBytes());
136         }
137 
138         configuration = configuration.addInputStream(is);
139 
140         is.close();
141     }
142 
143     private static Log _log =
144         LogFactoryUtil.getLog(PortalHibernateConfiguration.class);
145 
146     private Converter<String> _hibernateConfigurationConverter;
147 
148 }