| PortalHibernateConfiguration.java |
1 /**
2 * Copyright (c) 2000-2009 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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 package com.liferay.portal.spring.hibernate;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portal.util.PropsKeys;
26 import com.liferay.portal.util.PropsUtil;
27 import com.liferay.portal.util.PropsValues;
28
29 import java.io.InputStream;
30
31 import org.hibernate.cfg.Configuration;
32 import org.hibernate.cfg.Environment;
33
34 /**
35 * <a href="PortalHibernateConfiguration.java.html"><b><i>View Source</i></b>
36 * </a>
37 *
38 * @author Brian Wing Shun Chan
39 *
40 */
41 public class PortalHibernateConfiguration
42 extends TransactionAwareConfiguration {
43
44 protected String determineDialect() {
45 return DialectDetector.determineDialect(getDataSource());
46 }
47
48 protected ClassLoader getConfigurationClassLoader() {
49 return getClass().getClassLoader();
50 }
51
52 protected String[] getConfigurationResources() {
53 return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
54 }
55
56 protected Configuration newConfiguration() {
57 Configuration configuration = new Configuration();
58
59 try {
60 ClassLoader classLoader = getConfigurationClassLoader();
61
62 String[] resources = getConfigurationResources();
63
64 for (String resource : resources) {
65 try {
66 InputStream is = classLoader.getResourceAsStream(resource);
67
68 if (is != null) {
69 configuration = configuration.addInputStream(is);
70
71 is.close();
72 }
73 }
74 catch (Exception e1) {
75 if (_log.isWarnEnabled()) {
76 _log.warn(e1);
77 }
78 }
79 }
80
81 if (Validator.isNull(PropsValues.HIBERNATE_DIALECT)) {
82 String dialect = determineDialect();
83
84 configuration.setProperty("hibernate.dialect", dialect);
85 }
86
87 configuration.setProperties(PropsUtil.getProperties());
88 }
89 catch (Exception e2) {
90 _log.error(e2, e2);
91 }
92
93 return configuration;
94 }
95
96 protected void postProcessConfiguration(Configuration configuration) {
97
98 // Make sure that the Hibernate settings from PropsUtil are set. See the
99 // buildSessionFactory implementation in the LocalSessionFactoryBean
100 // class to understand how Spring automates a lot of configuration for
101 // Hibernate.
102
103 String connectionReleaseMode = PropsUtil.get(
104 Environment.RELEASE_CONNECTIONS);
105
106 if (Validator.isNotNull(connectionReleaseMode)) {
107 configuration.setProperty(
108 Environment.RELEASE_CONNECTIONS, connectionReleaseMode);
109 }
110 }
111
112 private static Log _log =
113 LogFactoryUtil.getLog(PortalHibernateConfiguration.class);
114
115 }