1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.database;
|
11
|
|
12
|
import java.util.Enumeration;
|
13
|
import java.util.Properties;
|
14
|
|
15
|
import org.apache.commons.lang.StringUtils;
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.hibernate.cache.internal.NoCachingRegionFactory;
|
18
|
import org.hibernate.cache.spi.RegionFactory;
|
19
|
import org.springframework.beans.MutablePropertyValues;
|
20
|
import org.springframework.beans.factory.config.BeanDefinition;
|
21
|
import org.springframework.beans.factory.config.PropertiesFactoryBean;
|
22
|
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
23
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
24
|
|
25
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
26
|
|
27
|
/**
|
28
|
* @author a.mueller
|
29
|
*
|
30
|
*/
|
31
|
public class CdmDataSource extends CdmDataSourceBase {
|
32
|
@SuppressWarnings("unused")
|
33
|
private static final Logger logger = Logger.getLogger(CdmDataSource.class);
|
34
|
|
35
|
private DatabaseTypeEnum dbType;
|
36
|
private String database;
|
37
|
private int port = -1;
|
38
|
private String username;
|
39
|
private String password;
|
40
|
|
41
|
private String filePath;
|
42
|
private H2Mode mode;
|
43
|
|
44
|
private boolean isLazy = true;
|
45
|
private String initMethodName = null;
|
46
|
private String destroyMethodName = null;
|
47
|
private DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;
|
48
|
private boolean showSql = false;
|
49
|
private boolean formatSql = false;
|
50
|
private boolean registerSearchListener = false;
|
51
|
private Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;
|
52
|
|
53
|
public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, String username, String password){
|
54
|
return new CdmDataSource(dbType, server, database, -1, username, password, null, null);
|
55
|
}
|
56
|
|
57
|
public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password){
|
58
|
return new CdmDataSource(dbType, server, database, port, username, password, null, null);
|
59
|
}
|
60
|
|
61
|
|
62
|
static public CdmDataSource NewMySqlInstance(String server, String database, String username, String password ){
|
63
|
return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, -1, username, password, null, null);
|
64
|
}
|
65
|
|
66
|
static public CdmDataSource NewMySqlInstance(String server, String database, int port, String username, String password){
|
67
|
return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, port, username, password, null, null);
|
68
|
}
|
69
|
|
70
|
static public CdmDataSource NewPostgreSQLInstance(String server, String database, int port, String username, String password){
|
71
|
return new CdmDataSource(DatabaseTypeEnum.PostgreSQL, server, database, port, username, password, null, null);
|
72
|
}
|
73
|
|
74
|
static public CdmDataSource NewSqlServer2005Instance(String server, String database, int port, String username, String password){
|
75
|
return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, port, username, password, null, null);
|
76
|
}
|
77
|
|
78
|
|
79
|
/** in work
|
80
|
* @param code TODO*/
|
81
|
static public CdmDataSource NewH2EmbeddedInstance(String database, String username, String password){
|
82
|
return NewH2EmbeddedInstance(database, username, password, null);
|
83
|
}
|
84
|
|
85
|
/** in work
|
86
|
* @param code TODO*/
|
87
|
static public CdmDataSource NewH2EmbeddedInstance(String database, String username, String password, String filePath){
|
88
|
//FIXME in work
|
89
|
int port = -1;
|
90
|
H2Mode mode = H2Mode.EMBEDDED;
|
91
|
CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, database, port, username, password, filePath, mode);
|
92
|
return dataSource;
|
93
|
}
|
94
|
|
95
|
/** in work */
|
96
|
static public CdmDataSource NewH2InMemoryInstance(){
|
97
|
//FIXME in work
|
98
|
int port = -1;
|
99
|
H2Mode mode = H2Mode.IN_MEMORY;
|
100
|
String username = "sa";
|
101
|
String password = "";
|
102
|
CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, null, port, username, password, null, mode);
|
103
|
return dataSource;
|
104
|
}
|
105
|
|
106
|
|
107
|
public static CdmDataSource NewInstance(ICdmDataSource dataSource) {
|
108
|
return new CdmDataSource(dataSource.getDatabaseType(),
|
109
|
dataSource.getServer(),
|
110
|
dataSource.getDatabase(),
|
111
|
dataSource.getPort(),
|
112
|
dataSource.getUsername(),
|
113
|
dataSource.getPassword(),
|
114
|
dataSource.getFilePath(),
|
115
|
dataSource.getMode());
|
116
|
}
|
117
|
/**
|
118
|
* @param server
|
119
|
* @param database
|
120
|
* @param port
|
121
|
*/
|
122
|
protected CdmDataSource(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password, String filePath, H2Mode mode) {
|
123
|
super();
|
124
|
this.dbType = dbType;
|
125
|
this.server = server;
|
126
|
this.database = database;
|
127
|
this.port = port;
|
128
|
this.username = username;
|
129
|
this.password = password;
|
130
|
this.initMethodName = dbType.getInitMethod();
|
131
|
this.destroyMethodName = dbType.getDestroyMethod();
|
132
|
this.filePath = filePath;
|
133
|
this.mode = mode;
|
134
|
}
|
135
|
|
136
|
@Override
|
137
|
public String getName() {
|
138
|
return database;
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public String getServer() {
|
143
|
return server;
|
144
|
}
|
145
|
|
146
|
@Override
|
147
|
public int getPort() {
|
148
|
return port;
|
149
|
}
|
150
|
|
151
|
@Override
|
152
|
public BeanDefinition getDatasourceBean(){
|
153
|
AbstractBeanDefinition bd = new RootBeanDefinition(dbType.getDataSourceClass());
|
154
|
//attributes
|
155
|
bd.setLazyInit(isLazy);
|
156
|
if (! CdmUtils.Nz(initMethodName).trim().equals("") ){
|
157
|
bd.setInitMethodName(initMethodName);
|
158
|
}
|
159
|
if (! CdmUtils.Nz(destroyMethodName).trim().equals("") ){
|
160
|
bd.setInitMethodName(destroyMethodName);
|
161
|
}
|
162
|
|
163
|
//properties
|
164
|
MutablePropertyValues props = new MutablePropertyValues();
|
165
|
Properties persistentProperties = getDatasourceProperties();
|
166
|
Enumeration<Object> keys = persistentProperties.keys();
|
167
|
while (keys.hasMoreElements()){
|
168
|
String key = (String)keys.nextElement();
|
169
|
props.addPropertyValue(key, persistentProperties.getProperty(key));
|
170
|
// Properties a = Environment.getProperties();
|
171
|
}
|
172
|
|
173
|
bd.setPropertyValues(props);
|
174
|
return bd;
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* Returns the list of properties that are defined in the datasource
|
179
|
* @return
|
180
|
*/
|
181
|
private Properties getDatasourceProperties(){
|
182
|
Properties result = new Properties();
|
183
|
result.put("driverClassName", dbType.getDriverClassName());
|
184
|
String connectionString = dbType.getConnectionString(this);
|
185
|
result.put("url", connectionString);
|
186
|
result.put("username", username);
|
187
|
result.put("password", password);
|
188
|
return result;
|
189
|
}
|
190
|
|
191
|
|
192
|
@Override
|
193
|
public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll){
|
194
|
boolean showSql = false;
|
195
|
boolean formatSql = false;
|
196
|
boolean registerSearchListener = false;
|
197
|
Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;
|
198
|
return getHibernatePropertiesBean(hbm2dll, showSql, formatSql, registerSearchListener, cacheProviderClass);
|
199
|
}
|
200
|
|
201
|
@Override
|
202
|
public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll, Boolean showSql, Boolean formatSql, Boolean registerSearchListener, Class<? extends RegionFactory> cacheProviderClass){
|
203
|
//Hibernate default values
|
204
|
if (hbm2dll == null){
|
205
|
hbm2dll = this.hbm2dll;
|
206
|
}
|
207
|
if (showSql == null){
|
208
|
showSql = this.showSql;
|
209
|
}
|
210
|
if (formatSql == null){
|
211
|
formatSql = this.formatSql;
|
212
|
}
|
213
|
if (cacheProviderClass == null){
|
214
|
cacheProviderClass = this.cacheProviderClass;
|
215
|
}
|
216
|
if(registerSearchListener == null){
|
217
|
registerSearchListener = this.registerSearchListener;
|
218
|
}
|
219
|
|
220
|
DatabaseTypeEnum dbtype = dbType;
|
221
|
AbstractBeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);
|
222
|
MutablePropertyValues hibernateProps = new MutablePropertyValues();
|
223
|
|
224
|
Properties props = new Properties();
|
225
|
props.setProperty("hibernate.hbm2ddl.auto", hbm2dll.toString());
|
226
|
props.setProperty("hibernate.dialect", dbtype.getHibernateDialectCanonicalName());
|
227
|
// OLD:props.setProperty("hibernate.cache.provider_class", cacheProviderClass.getName());
|
228
|
props.setProperty("hibernate.cache.region.factory_class", cacheProviderClass.getName());
|
229
|
props.setProperty("hibernate.show_sql", String.valueOf(showSql));
|
230
|
props.setProperty("hibernate.format_sql", String.valueOf(formatSql));
|
231
|
props.setProperty("hibernate.search.autoregister_listeners", String.valueOf(registerSearchListener));
|
232
|
|
233
|
hibernateProps.addPropertyValue("properties",props);
|
234
|
bd.setPropertyValues(hibernateProps);
|
235
|
return bd;
|
236
|
}
|
237
|
|
238
|
public String getInitMethodName() {
|
239
|
return initMethodName;
|
240
|
}
|
241
|
|
242
|
public void setInitMethodName(String initMethodName) {
|
243
|
this.initMethodName = initMethodName;
|
244
|
}
|
245
|
|
246
|
public String getDestroyMethodName() {
|
247
|
return destroyMethodName;
|
248
|
}
|
249
|
|
250
|
public void setDestroyMethodName(String destroyMethodName) {
|
251
|
this.destroyMethodName = destroyMethodName;
|
252
|
}
|
253
|
|
254
|
@Override
|
255
|
public String getDatabase() {
|
256
|
return database;
|
257
|
}
|
258
|
|
259
|
@Override
|
260
|
public void setDatabase(String database) {
|
261
|
this.database = database;
|
262
|
}
|
263
|
|
264
|
@Override
|
265
|
public DatabaseTypeEnum getDatabaseType() {
|
266
|
return dbType;
|
267
|
}
|
268
|
|
269
|
@Override
|
270
|
public String getFilePath() {
|
271
|
return filePath;
|
272
|
}
|
273
|
|
274
|
@Override
|
275
|
public H2Mode getMode() {
|
276
|
return mode;
|
277
|
}
|
278
|
|
279
|
@Override
|
280
|
public void setMode(H2Mode h2Mode) {
|
281
|
this.mode = h2Mode;
|
282
|
|
283
|
}
|
284
|
|
285
|
@Override
|
286
|
public String getPassword() {
|
287
|
return password;
|
288
|
}
|
289
|
|
290
|
@Override
|
291
|
public void setPassword(String password) {
|
292
|
this.password = password;
|
293
|
|
294
|
}
|
295
|
|
296
|
@Override
|
297
|
public String getUsername() {
|
298
|
return username;
|
299
|
}
|
300
|
|
301
|
@Override
|
302
|
public void setUsername(String username) {
|
303
|
this.username = username;
|
304
|
|
305
|
}
|
306
|
|
307
|
//********************* TO STRING() **********************/
|
308
|
|
309
|
@Override
|
310
|
public String toString() {
|
311
|
if (StringUtils.isBlank(this.database)){
|
312
|
return super.toString();
|
313
|
}else{
|
314
|
String result = "DataSource<" + dbType.getConnectionString(this).replace(CdmUtils.Nz(password), "") + ">";
|
315
|
return result;
|
316
|
}
|
317
|
}
|
318
|
|
319
|
}
|
320
|
|