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.hibernate.cfg.Environment;
|
20
|
import org.springframework.beans.MutablePropertyValues;
|
21
|
import org.springframework.beans.factory.config.BeanDefinition;
|
22
|
import org.springframework.beans.factory.config.PropertiesFactoryBean;
|
23
|
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
24
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
25
|
|
26
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
27
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
28
|
|
29
|
/**
|
30
|
* @author a.mueller
|
31
|
*
|
32
|
*/
|
33
|
public class CdmDataSource extends CdmDataSourceBase {
|
34
|
@SuppressWarnings("unused")
|
35
|
private static final Logger logger = Logger.getLogger(CdmDataSource.class);
|
36
|
|
37
|
private DatabaseTypeEnum dbType;
|
38
|
private String server;
|
39
|
private String database;
|
40
|
private int port = -1;
|
41
|
private String username;
|
42
|
private String password;
|
43
|
private NomenclaturalCode nomenclaturalCode;
|
44
|
|
45
|
private String filePath;
|
46
|
private H2Mode mode;
|
47
|
|
48
|
private boolean isLazy = true;
|
49
|
private String initMethodName = null;
|
50
|
private String destroyMethodName = null;
|
51
|
private DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;
|
52
|
private boolean showSql = false;
|
53
|
private boolean formatSql = false;
|
54
|
private boolean registerSearchListener = false;
|
55
|
private Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;
|
56
|
|
57
|
public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, String username, String password){
|
58
|
return new CdmDataSource(dbType, server, database, -1, username, password, null, null, null);
|
59
|
}
|
60
|
|
61
|
public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password){
|
62
|
return new CdmDataSource(dbType, server, database, port, username, password, null, null, null);
|
63
|
}
|
64
|
|
65
|
public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, String username, String password , NomenclaturalCode code){
|
66
|
return new CdmDataSource(dbType, server, database, -1, username, password, null, null, code);
|
67
|
}
|
68
|
|
69
|
public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password , NomenclaturalCode code){
|
70
|
return new CdmDataSource(dbType, server, database, port, username, password, null, null, code);
|
71
|
}
|
72
|
|
73
|
|
74
|
static public CdmDataSource NewMySqlInstance(String server, String database, String username, String password ){
|
75
|
return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, -1, username, password, null, null, null);
|
76
|
}
|
77
|
|
78
|
static public CdmDataSource NewMySqlInstance(String server, String database, String username, String password , NomenclaturalCode code){
|
79
|
return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, -1, username, password, null, null, code);
|
80
|
}
|
81
|
|
82
|
static public CdmDataSource NewMySqlInstance(String server, String database, int port, String username, String password, NomenclaturalCode code){
|
83
|
return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, port, username, password, null, null, code);
|
84
|
}
|
85
|
|
86
|
static public CdmDataSource NewPostgreSQLInstance(String server, String database, int port, String username, String password, NomenclaturalCode code){
|
87
|
return new CdmDataSource(DatabaseTypeEnum.PostgreSQL, server, database, port, username, password, null, null, code);
|
88
|
}
|
89
|
|
90
|
static public CdmDataSource NewSqlServer2005Instance(String server, String database, int port, String username, String password, NomenclaturalCode code){
|
91
|
return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, port, username, password, null, null, code);
|
92
|
}
|
93
|
|
94
|
static public CdmDataSource NewSqlServer2005Instance(String server, String database, int port, String username, String password /*, NomenclaturalCode code*/){
|
95
|
return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, port, username, password, null, null, null);
|
96
|
}
|
97
|
|
98
|
|
99
|
/** in work
|
100
|
* @param code TODO*/
|
101
|
static public CdmDataSource NewH2EmbeddedInstance(String database, String username, String password){
|
102
|
return NewH2EmbeddedInstance(database, username, password, null, null);
|
103
|
}
|
104
|
|
105
|
/** in work
|
106
|
* @param code TODO*/
|
107
|
static public CdmDataSource NewH2EmbeddedInstance(String database, String username, String password, NomenclaturalCode code){
|
108
|
return NewH2EmbeddedInstance(database, username, password, null, code);
|
109
|
}
|
110
|
|
111
|
/** in work
|
112
|
* @param code TODO*/
|
113
|
static public CdmDataSource NewH2EmbeddedInstance(String database, String username, String password, String filePath, NomenclaturalCode code){
|
114
|
//FIXME in work
|
115
|
int port = -1;
|
116
|
H2Mode mode = H2Mode.EMBEDDED;
|
117
|
CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, database, port, username, password, filePath, mode, code);
|
118
|
return dataSource;
|
119
|
}
|
120
|
|
121
|
/** in work */
|
122
|
static public CdmDataSource NewH2InMemoryInstance(){
|
123
|
//FIXME in work
|
124
|
int port = -1;
|
125
|
H2Mode mode = H2Mode.IN_MEMORY;
|
126
|
String username = "sa";
|
127
|
String password = "";
|
128
|
CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, null, port, username, password, null, mode, null);
|
129
|
return dataSource;
|
130
|
}
|
131
|
|
132
|
|
133
|
public static CdmDataSource NewInstance(ICdmDataSource dataSource) {
|
134
|
return new CdmDataSource(dataSource.getDatabaseType(),
|
135
|
dataSource.getServer(),
|
136
|
dataSource.getDatabase(),
|
137
|
dataSource.getPort(),
|
138
|
dataSource.getUsername(),
|
139
|
dataSource.getPassword(),
|
140
|
dataSource.getFilePath(),
|
141
|
dataSource.getMode(),
|
142
|
dataSource.getNomenclaturalCode());
|
143
|
}
|
144
|
/**
|
145
|
* @param server
|
146
|
* @param database
|
147
|
* @param port
|
148
|
*/
|
149
|
protected CdmDataSource(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password, String filePath, H2Mode mode, NomenclaturalCode code) {
|
150
|
super();
|
151
|
this.dbType = dbType;
|
152
|
this.server = server;
|
153
|
this.database = database;
|
154
|
this.port = port;
|
155
|
this.username = username;
|
156
|
this.password = password;
|
157
|
this.initMethodName = dbType.getInitMethod();
|
158
|
this.destroyMethodName = dbType.getDestroyMethod();
|
159
|
this.filePath = filePath;
|
160
|
this.mode = mode;
|
161
|
this.nomenclaturalCode = code;
|
162
|
}
|
163
|
|
164
|
|
165
|
/* (non-Javadoc)
|
166
|
* @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
|
167
|
* A CdmDataSource does not have a name representation therefore the database name is returned
|
168
|
*/
|
169
|
@Override
|
170
|
public String getName() {
|
171
|
return database;
|
172
|
}
|
173
|
|
174
|
@Override
|
175
|
public String getServer() {
|
176
|
return server;
|
177
|
}
|
178
|
|
179
|
@Override
|
180
|
public int getPort() {
|
181
|
return port;
|
182
|
}
|
183
|
|
184
|
@Override
|
185
|
public NomenclaturalCode getNomenclaturalCode() {
|
186
|
return nomenclaturalCode;
|
187
|
}
|
188
|
|
189
|
@Override
|
190
|
public BeanDefinition getDatasourceBean(){
|
191
|
AbstractBeanDefinition bd = new RootBeanDefinition(dbType.getDataSourceClass());
|
192
|
//attributes
|
193
|
bd.setLazyInit(isLazy);
|
194
|
if (! CdmUtils.Nz(initMethodName).trim().equals("") ){
|
195
|
bd.setInitMethodName(initMethodName);
|
196
|
}
|
197
|
if (! CdmUtils.Nz(destroyMethodName).trim().equals("") ){
|
198
|
bd.setInitMethodName(destroyMethodName);
|
199
|
}
|
200
|
|
201
|
//properties
|
202
|
MutablePropertyValues props = new MutablePropertyValues();
|
203
|
Properties persistentProperties = getDatasourceProperties();
|
204
|
Enumeration<Object> keys = (Enumeration<Object>)persistentProperties.keys();
|
205
|
while (keys.hasMoreElements()){
|
206
|
String key = (String)keys.nextElement();
|
207
|
props.addPropertyValue(key, persistentProperties.getProperty(key));
|
208
|
Properties a = Environment.getProperties();
|
209
|
}
|
210
|
|
211
|
bd.setPropertyValues(props);
|
212
|
return bd;
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Returns the list of properties that are defined in the datasource
|
217
|
* @return
|
218
|
*/
|
219
|
private Properties getDatasourceProperties(){
|
220
|
Properties result = new Properties();
|
221
|
result.put("driverClassName", dbType.getDriverClassName());
|
222
|
String connectionString = dbType.getConnectionString(this);
|
223
|
result.put("url", connectionString);
|
224
|
result.put("username", username);
|
225
|
result.put("password", password);
|
226
|
return result;
|
227
|
}
|
228
|
|
229
|
|
230
|
@Override
|
231
|
public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll){
|
232
|
boolean showSql = false;
|
233
|
boolean formatSql = false;
|
234
|
boolean registerSearchListener = false;
|
235
|
Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;
|
236
|
return getHibernatePropertiesBean(hbm2dll, showSql, formatSql, registerSearchListener, cacheProviderClass);
|
237
|
}
|
238
|
|
239
|
@Override
|
240
|
public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll, Boolean showSql, Boolean formatSql, Boolean registerSearchListener, Class<? extends RegionFactory> cacheProviderClass){
|
241
|
//Hibernate default values
|
242
|
if (hbm2dll == null){
|
243
|
hbm2dll = this.hbm2dll;
|
244
|
}
|
245
|
if (showSql == null){
|
246
|
showSql = this.showSql;
|
247
|
}
|
248
|
if (formatSql == null){
|
249
|
formatSql = this.formatSql;
|
250
|
}
|
251
|
if (cacheProviderClass == null){
|
252
|
cacheProviderClass = this.cacheProviderClass;
|
253
|
}
|
254
|
if(registerSearchListener == null){
|
255
|
registerSearchListener = this.registerSearchListener;
|
256
|
}
|
257
|
|
258
|
DatabaseTypeEnum dbtype = dbType;
|
259
|
AbstractBeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);
|
260
|
MutablePropertyValues hibernateProps = new MutablePropertyValues();
|
261
|
|
262
|
Properties props = new Properties();
|
263
|
props.setProperty("hibernate.hbm2ddl.auto", hbm2dll.toString());
|
264
|
props.setProperty("hibernate.dialect", dbtype.getHibernateDialectCanonicalName());
|
265
|
// OLD:props.setProperty("hibernate.cache.provider_class", cacheProviderClass.getName());
|
266
|
props.setProperty("hibernate.cache.region.factory_class", cacheProviderClass.getName());
|
267
|
props.setProperty("hibernate.show_sql", String.valueOf(showSql));
|
268
|
props.setProperty("hibernate.format_sql", String.valueOf(formatSql));
|
269
|
props.setProperty("hibernate.search.autoregister_listeners", String.valueOf(registerSearchListener));
|
270
|
|
271
|
hibernateProps.addPropertyValue("properties",props);
|
272
|
bd.setPropertyValues(hibernateProps);
|
273
|
return bd;
|
274
|
}
|
275
|
|
276
|
public String getInitMethodName() {
|
277
|
return initMethodName;
|
278
|
}
|
279
|
|
280
|
public void setInitMethodName(String initMethodName) {
|
281
|
this.initMethodName = initMethodName;
|
282
|
}
|
283
|
|
284
|
public String getDestroyMethodName() {
|
285
|
return destroyMethodName;
|
286
|
}
|
287
|
|
288
|
public void setDestroyMethodName(String destroyMethodName) {
|
289
|
this.destroyMethodName = destroyMethodName;
|
290
|
}
|
291
|
|
292
|
@Override
|
293
|
public String getDatabase() {
|
294
|
return database;
|
295
|
}
|
296
|
|
297
|
@Override
|
298
|
public void setDatabase(String database) {
|
299
|
this.database = database;
|
300
|
}
|
301
|
|
302
|
@Override
|
303
|
public DatabaseTypeEnum getDatabaseType() {
|
304
|
return dbType;
|
305
|
}
|
306
|
|
307
|
@Override
|
308
|
public String getFilePath() {
|
309
|
return filePath;
|
310
|
}
|
311
|
|
312
|
@Override
|
313
|
public H2Mode getMode() {
|
314
|
return mode;
|
315
|
}
|
316
|
|
317
|
@Override
|
318
|
public void setMode(H2Mode h2Mode) {
|
319
|
this.mode = h2Mode;
|
320
|
|
321
|
}
|
322
|
|
323
|
@Override
|
324
|
public String getPassword() {
|
325
|
return password;
|
326
|
}
|
327
|
|
328
|
@Override
|
329
|
public void setPassword(String password) {
|
330
|
this.password = password;
|
331
|
|
332
|
}
|
333
|
|
334
|
@Override
|
335
|
public String getUsername() {
|
336
|
return username;
|
337
|
}
|
338
|
|
339
|
@Override
|
340
|
public void setUsername(String username) {
|
341
|
this.username = username;
|
342
|
|
343
|
}
|
344
|
|
345
|
|
346
|
|
347
|
|
348
|
|
349
|
|
350
|
|
351
|
|
352
|
@Override
|
353
|
public String toString() {
|
354
|
if (StringUtils.isBlank(this.database)){
|
355
|
return super.toString();
|
356
|
}else{
|
357
|
String result = "DataSource<" + dbType.getConnectionString(this).replace(CdmUtils.Nz(password), "") + ">";
|
358
|
return result;
|
359
|
}
|
360
|
}
|
361
|
|
362
|
|
363
|
|
364
|
|
365
|
|
366
|
}
|
367
|
|