001
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.ClassLoaderUtil;
027 import com.liferay.portal.util.PropsUtil;
028 import com.liferay.portal.util.PropsValues;
029
030 import java.io.InputStream;
031
032 import java.util.Map;
033 import java.util.Properties;
034
035 import javassist.util.proxy.ProxyFactory;
036
037 import org.hibernate.HibernateException;
038 import org.hibernate.SessionFactory;
039 import org.hibernate.cfg.Configuration;
040 import org.hibernate.cfg.Environment;
041 import org.hibernate.dialect.Dialect;
042
043 import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
044
045
050 public class PortalHibernateConfiguration extends LocalSessionFactoryBean {
051
052 @Override
053 public SessionFactory buildSessionFactory() throws Exception {
054 ProxyFactory.classLoaderProvider =
055 new ProxyFactory.ClassLoaderProvider() {
056
057 public ClassLoader get(ProxyFactory proxyFactory) {
058 return ClassLoaderUtil.getContextClassLoader();
059 }
060
061 };
062
063 setBeanClassLoader(getConfigurationClassLoader());
064
065 return super.buildSessionFactory();
066 }
067
068 @Override
069 public void destroy() throws HibernateException {
070 setBeanClassLoader(null);
071
072 super.destroy();
073 }
074
075 public void setHibernateConfigurationConverter(
076 Converter<String> hibernateConfigurationConverter) {
077
078 _hibernateConfigurationConverter = hibernateConfigurationConverter;
079 }
080
081 protected Dialect determineDialect() {
082 return DialectDetector.getDialect(getDataSource());
083 }
084
085 protected ClassLoader getConfigurationClassLoader() {
086 Class<?> clazz = getClass();
087
088 return clazz.getClassLoader();
089 }
090
091 protected String[] getConfigurationResources() {
092 return PropsUtil.getArray(PropsKeys.HIBERNATE_CONFIGS);
093 }
094
095 @Override
096 protected Configuration newConfiguration() {
097 Configuration configuration = new Configuration();
098
099 try {
100 String[] resources = getConfigurationResources();
101
102 for (String resource : resources) {
103 try {
104 readResource(configuration, resource);
105 }
106 catch (Exception e2) {
107 if (_log.isWarnEnabled()) {
108 _log.warn(e2, e2);
109 }
110 }
111 }
112
113 configuration.setProperties(PropsUtil.getProperties());
114
115 if (Validator.isNull(PropsValues.HIBERNATE_DIALECT)) {
116 Dialect dialect = determineDialect();
117
118 setDB(dialect);
119
120 Class<?> clazz = dialect.getClass();
121
122 configuration.setProperty("hibernate.dialect", clazz.getName());
123 }
124
125 DB db = DBFactoryUtil.getDB();
126
127 String dbType = db.getType();
128
129 if (dbType.equals(DB.TYPE_HYPERSONIC)) {
130
131 }
132 }
133 catch (Exception e1) {
134 _log.error(e1, e1);
135 }
136
137 Properties hibernateProperties = getHibernateProperties();
138
139 if (hibernateProperties != null) {
140 for (Map.Entry<Object, Object> entry :
141 hibernateProperties.entrySet()) {
142
143 String key = (String)entry.getKey();
144 String value = (String)entry.getValue();
145
146 configuration.setProperty(key, value);
147 }
148 }
149
150 return configuration;
151 }
152
153 @Override
154 protected void postProcessConfiguration(Configuration configuration) {
155
156
157
158
159
160
161 String connectionReleaseMode = PropsUtil.get(
162 Environment.RELEASE_CONNECTIONS);
163
164 if (Validator.isNotNull(connectionReleaseMode)) {
165 configuration.setProperty(
166 Environment.RELEASE_CONNECTIONS, connectionReleaseMode);
167 }
168 }
169
170 protected void readResource(Configuration configuration, String resource)
171 throws Exception {
172
173 ClassLoader classLoader = getConfigurationClassLoader();
174
175 InputStream is = classLoader.getResourceAsStream(resource);
176
177 if (is == null) {
178 return;
179 }
180
181 if (_hibernateConfigurationConverter != null) {
182 String configurationString = StringUtil.read(is);
183
184 is.close();
185
186 configurationString = _hibernateConfigurationConverter.convert(
187 configurationString);
188
189 is = new UnsyncByteArrayInputStream(configurationString.getBytes());
190 }
191
192 configuration = configuration.addInputStream(is);
193
194 is.close();
195 }
196
197 protected void setDB(Dialect dialect) {
198 DBFactoryUtil.setDB(dialect);
199 }
200
201 private static Log _log = LogFactoryUtil.getLog(
202 PortalHibernateConfiguration.class);
203
204 private Converter<String> _hibernateConfigurationConverter;
205
206 }