001    /**
002     * Copyright (c) 2000-2012 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 javassist.util.proxy.ProxyFactory;
035    
036    import org.hibernate.SessionFactory;
037    import org.hibernate.cfg.Configuration;
038    import org.hibernate.cfg.Environment;
039    
040    import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Marcellus Tavares
045     * @author Shuyang Zhou
046     */
047    public class PortalHibernateConfiguration extends LocalSessionFactoryBean {
048    
049            @Override
050            public SessionFactory buildSessionFactory() throws Exception {
051                    ProxyFactory.classLoaderProvider =
052                            new ProxyFactory.ClassLoaderProvider() {
053    
054                                    public ClassLoader get(ProxyFactory proxyFactory) {
055                                            return Thread.currentThread().getContextClassLoader();
056                                    }
057    
058                            };
059    
060                    return super.buildSessionFactory();
061            }
062    
063            public void setHibernateConfigurationConverter(
064                    Converter<String> hibernateConfigurationConverter) {
065    
066                    _hibernateConfigurationConverter = hibernateConfigurationConverter;
067            }
068    
069            protected String determineDialect() {
070                    return DialectDetector.determineDialect(getDataSource());
071            }
072    
073            protected ClassLoader getConfigurationClassLoader() {
074                    return getClass().getClassLoader();
075            }
076    
077            protected String[] getConfigurationResources() {
078                    return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
079            }
080    
081            @Override
082            protected Configuration newConfiguration() {
083                    Configuration configuration = new Configuration();
084    
085                    try {
086                            String[] resources = getConfigurationResources();
087    
088                            for (String resource : resources) {
089                                    try {
090                                            readResource(configuration, resource);
091                                    }
092                                    catch (Exception e2) {
093                                            if (_log.isWarnEnabled()) {
094                                                    _log.warn(e2, e2);
095                                            }
096                                    }
097                            }
098    
099                            configuration.setProperties(PropsUtil.getProperties());
100    
101                            if (Validator.isNull(PropsValues.HIBERNATE_DIALECT)) {
102                                    String dialect = determineDialect();
103    
104                                    configuration.setProperty("hibernate.dialect", dialect);
105                            }
106    
107                            DB db = DBFactoryUtil.getDB();
108    
109                            String dbType = db.getType();
110    
111                            if (dbType.equals(DB.TYPE_HYPERSONIC)) {
112                                    //configuration.setProperty("hibernate.jdbc.batch_size", "0");
113                            }
114                    }
115                    catch (Exception e1) {
116                            _log.error(e1, e1);
117                    }
118    
119                    Properties hibernateProperties = getHibernateProperties();
120    
121                    if (hibernateProperties != null) {
122                            for (Map.Entry<Object, Object> entry :
123                                            hibernateProperties.entrySet()) {
124    
125                                    String key = (String)entry.getKey();
126                                    String value = (String)entry.getValue();
127    
128                                    configuration.setProperty(key, value);
129                            }
130                    }
131    
132                    return configuration;
133            }
134    
135            @Override
136            protected void postProcessConfiguration(Configuration configuration) {
137    
138                    // Make sure that the Hibernate settings from PropsUtil are set. See the
139                    // buildSessionFactory implementation in the LocalSessionFactoryBean
140                    // class to understand how Spring automates a lot of configuration for
141                    // Hibernate.
142    
143                    String connectionReleaseMode = PropsUtil.get(
144                            Environment.RELEASE_CONNECTIONS);
145    
146                    if (Validator.isNotNull(connectionReleaseMode)) {
147                            configuration.setProperty(
148                                    Environment.RELEASE_CONNECTIONS, connectionReleaseMode);
149                    }
150            }
151    
152            protected void readResource(Configuration configuration, String resource)
153                    throws Exception {
154    
155                    ClassLoader classLoader = getConfigurationClassLoader();
156    
157                    InputStream is = classLoader.getResourceAsStream(resource);
158    
159                    if (is == null) {
160                            return;
161                    }
162    
163                    if (_hibernateConfigurationConverter != null) {
164                            String configurationString = StringUtil.read(is);
165    
166                            is.close();
167    
168                            configurationString = _hibernateConfigurationConverter.convert(
169                                    configurationString);
170    
171                            is = new UnsyncByteArrayInputStream(configurationString.getBytes());
172                    }
173    
174                    configuration = configuration.addInputStream(is);
175    
176                    is.close();
177            }
178    
179            private static Log _log = LogFactoryUtil.getLog(
180                    PortalHibernateConfiguration.class);
181    
182            private Converter<String> _hibernateConfigurationConverter;
183    
184    }