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