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.dao.orm.hibernate.DB2Dialect;
26  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.tools.sql.DBUtil;
30  import com.liferay.portal.util.PropsKeys;
31  import com.liferay.portal.util.PropsUtil;
32  import com.liferay.portal.util.PropsValues;
33  
34  import java.io.InputStream;
35  
36  import java.sql.Connection;
37  import java.sql.DatabaseMetaData;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  import org.hibernate.cfg.Configuration;
43  import org.hibernate.cfg.Environment;
44  import org.hibernate.dialect.DB2400Dialect;
45  import org.hibernate.dialect.Dialect;
46  import org.hibernate.dialect.DialectFactory;
47  import org.hibernate.dialect.SybaseDialect;
48  
49  /**
50   * <a href="PortalHibernateConfiguration.java.html"><b><i>View Source</i></b>
51   * </a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class PortalHibernateConfiguration
57      extends TransactionAwareConfiguration {
58  
59      protected String determineDialect() {
60          Dialect dialect = null;
61  
62          Connection con = null;
63  
64          try {
65              con = getDataSource().getConnection();
66  
67              DatabaseMetaData metaData = con.getMetaData();
68  
69              String dbName = metaData.getDatabaseProductName();
70              int dbMajorVersion = metaData.getDatabaseMajorVersion();
71  
72              if (_log.isInfoEnabled()) {
73                  _log.info(
74                      "Determining dialect for " + dbName + " " + dbMajorVersion);
75              }
76  
77              if (dbName.startsWith("HSQL")) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn(
80                          "Liferay is configured to use Hypersonic as its " +
81                              "database. Do NOT use Hypersonic in production. " +
82                                  "Hypersonic is an embedded database useful " +
83                                      "for development and demo'ing purposes.");
84                  }
85              }
86  
87              if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
88                  dialect = new SybaseDialect();
89              }
90              else if (dbName.startsWith("DB2") && (dbMajorVersion == 9)) {
91                  dialect = new DB2Dialect();
92              }
93              else {
94                  dialect = DialectFactory.determineDialect(
95                      dbName, dbMajorVersion);
96              }
97  
98              DBUtil.setInstance(dialect);
99  
100             if (_log.isInfoEnabled()) {
101                 _log.info("Using dialect " + dialect.getClass().getName());
102             }
103         }
104         catch (Exception e) {
105             String msg = GetterUtil.getString(e.getMessage());
106 
107             if (msg.indexOf("explicitly set for database: DB2") != -1) {
108                 dialect = new DB2400Dialect();
109 
110                 if (_log.isWarnEnabled()) {
111                     _log.warn(
112                         "DB2400Dialect was dynamically chosen as the " +
113                             "Hibernate dialect for DB2. This can be " +
114                                 "overriden in portal.properties");
115                 }
116             }
117             else {
118                 _log.error(e, e);
119             }
120         }
121         finally {
122             DataAccess.cleanUp(con);
123         }
124 
125         if (dialect == null) {
126             throw new RuntimeException("No dialect found");
127         }
128 
129         return dialect.getClass().getName();
130     }
131 
132     protected ClassLoader getConfigurationClassLoader() {
133         return getClass().getClassLoader();
134     }
135 
136     protected String[] getConfigurationResources() {
137         return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
138     }
139 
140     protected Configuration newConfiguration() {
141         Configuration configuration = new Configuration();
142 
143         try {
144             ClassLoader classLoader = getConfigurationClassLoader();
145 
146             String[] resources = getConfigurationResources();
147 
148             for (String resource : resources) {
149                 try {
150                     InputStream is = classLoader.getResourceAsStream(resource);
151 
152                     if (is != null) {
153                         configuration = configuration.addInputStream(is);
154 
155                         is.close();
156                     }
157                 }
158                 catch (Exception e1) {
159                     if (_log.isWarnEnabled()) {
160                         _log.warn(e1);
161                     }
162                 }
163             }
164 
165             if (Validator.isNull(PropsValues.HIBERNATE_DIALECT)) {
166                 String dialect = determineDialect();
167 
168                 configuration.setProperty("hibernate.dialect", dialect);
169             }
170 
171             configuration.setProperties(PropsUtil.getProperties());
172         }
173         catch (Exception e2) {
174             _log.error(e2, e2);
175         }
176 
177         return configuration;
178     }
179 
180     protected void postProcessConfiguration(Configuration configuration) {
181 
182         // Make sure that the Hibernate settings from PropsUtil are set. See the
183         // buildSessionFactory implementation in the LocalSessionFactoryBean
184         // class to understand how Spring automates a lot of configuration for
185         // Hibernate.
186 
187         String connectionReleaseMode = PropsUtil.get(
188             Environment.RELEASE_CONNECTIONS);
189 
190         if (Validator.isNotNull(connectionReleaseMode)) {
191             configuration.setProperty(
192                 Environment.RELEASE_CONNECTIONS, connectionReleaseMode);
193         }
194     }
195 
196     private static Log _log =
197         LogFactory.getLog(PortalHibernateConfiguration.class);
198 
199 }