001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.spring.hibernate;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.Converter;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.util.PropsUtil;
027    import com.liferay.portal.util.PropsValues;
028    
029    import java.io.InputStream;
030    
031    import java.util.Map;
032    import java.util.Properties;
033    
034    import org.hibernate.cfg.Configuration;
035    import org.hibernate.cfg.Environment;
036    
037    import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Marcellus Tavares
042     */
043    public class PortalHibernateConfiguration extends LocalSessionFactoryBean {
044    
045            public void setHibernateConfigurationConverter(
046                    Converter<String> hibernateConfigurationConverter) {
047    
048                    _hibernateConfigurationConverter = hibernateConfigurationConverter;
049            }
050    
051            protected String determineDialect() {
052                    return DialectDetector.determineDialect(getDataSource());
053            }
054    
055            protected ClassLoader getConfigurationClassLoader() {
056                    return getClass().getClassLoader();
057            }
058    
059            protected String[] getConfigurationResources() {
060                    return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
061            }
062    
063            protected Configuration newConfiguration() {
064                    Configuration configuration = new Configuration();
065    
066                    try {
067                            String[] resources = getConfigurationResources();
068    
069                            for (String resource : resources) {
070                                    try {
071                                            readResource(configuration, resource);
072                                    }
073                                    catch (Exception e2) {
074                                            if (_log.isWarnEnabled()) {
075                                                    _log.warn(e2, e2);
076                                            }
077                                    }
078                            }
079    
080                            configuration.setProperties(PropsUtil.getProperties());
081    
082                            if (Validator.isNull(PropsValues.HIBERNATE_DIALECT)) {
083                                    String dialect = determineDialect();
084    
085                                    configuration.setProperty("hibernate.dialect", dialect);
086                            }
087    
088                            DB db = DBFactoryUtil.getDB();
089    
090                            if (db.getType().equals(DB.TYPE_HYPERSONIC)) {
091                                    //configuration.setProperty("hibernate.jdbc.batch_size", "0");
092                            }
093                    }
094                    catch (Exception e1) {
095                            _log.error(e1, e1);
096                    }
097    
098                    Properties hibernateProperties = getHibernateProperties();
099    
100                    if (hibernateProperties != null) {
101                            for (Map.Entry<Object, Object> entry :
102                                            hibernateProperties.entrySet()) {
103    
104                                    String key = (String)entry.getKey();
105                                    String value = (String)entry.getValue();
106    
107                                    configuration.setProperty(key, value);
108                            }
109                    }
110    
111                    return configuration;
112            }
113    
114            protected void postProcessConfiguration(Configuration configuration) {
115    
116                    // Make sure that the Hibernate settings from PropsUtil are set. See the
117                    // buildSessionFactory implementation in the LocalSessionFactoryBean
118                    // class to understand how Spring automates a lot of configuration for
119                    // Hibernate.
120    
121                    String connectionReleaseMode = PropsUtil.get(
122                            Environment.RELEASE_CONNECTIONS);
123    
124                    if (Validator.isNotNull(connectionReleaseMode)) {
125                            configuration.setProperty(
126                                    Environment.RELEASE_CONNECTIONS, connectionReleaseMode);
127                    }
128            }
129    
130            protected void readResource(Configuration configuration, String resource)
131                    throws Exception {
132    
133                    ClassLoader classLoader = getConfigurationClassLoader();
134    
135                    InputStream is = classLoader.getResourceAsStream(resource);
136    
137                    if (is == null) {
138                            return;
139                    }
140    
141                    if (_hibernateConfigurationConverter != null) {
142                            String configurationString = StringUtil.read(is);
143    
144                            is.close();
145    
146                            configurationString = _hibernateConfigurationConverter.convert(
147                                    configurationString);
148    
149                            is = new UnsyncByteArrayInputStream(
150                                    configurationString.getBytes());
151                    }
152    
153                    configuration = configuration.addInputStream(is);
154    
155                    is.close();
156            }
157    
158            private static Log _log = LogFactoryUtil.getLog(
159                    PortalHibernateConfiguration.class);
160    
161            private Converter<String> _hibernateConfigurationConverter;
162    
163    }