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.support.AbstractBeanDefinition;
|
22
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
23
|
|
24
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
25
|
import eu.etaxonomy.cdm.persistence.hibernate.HibernateConfiguration;
|
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 boolean registerAuditing = true;
|
52
|
private Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;
|
53
|
|
54
|
public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, String username, String password){
|
55
|
return new CdmDataSource(dbType, server, database, -1, username, password, null, null);
|
56
|
}
|
57
|
|
58
|
public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password){
|
59
|
return new CdmDataSource(dbType, server, database, port, username, password, null, null);
|
60
|
}
|
61
|
|
62
|
|
63
|
static public CdmDataSource NewMySqlInstance(String server, String database, String username, String password ){
|
64
|
return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, -1, username, password, null, null);
|
65
|
}
|
66
|
|
67
|
static public CdmDataSource NewMySqlInstance(String server, String database, int port, String username, String password){
|
68
|
return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, port, username, password, null, null);
|
69
|
}
|
70
|
|
71
|
static public CdmDataSource NewPostgreSQLInstance(String server, String database, int port, String username, String password){
|
72
|
return new CdmDataSource(DatabaseTypeEnum.PostgreSQL, server, database, port, username, password, null, null);
|
73
|
}
|
74
|
|
75
|
static public CdmDataSource NewSqlServer2005Instance(String server, String database, int port, String username, String password){
|
76
|
return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, port, username, password, null, null);
|
77
|
}
|
78
|
static public CdmDataSource NewSqlServer2008Instance(String server, String database, int port, String username, String password){
|
79
|
return new CdmDataSource(DatabaseTypeEnum.SqlServer2008, server, database, port, username, password, null, null);
|
80
|
}
|
81
|
static public CdmDataSource NewSqlServer2012Instance(String server, String database, int port, String username, String password){
|
82
|
return new CdmDataSource(DatabaseTypeEnum.SqlServer2012, server, database, port, username, password, null, null);
|
83
|
}
|
84
|
|
85
|
/** in work
|
86
|
* @param code TODO*/
|
87
|
static public CdmDataSource NewH2EmbeddedInstance(String database, String username, String password){
|
88
|
return NewH2EmbeddedInstance(database, username, password, null);
|
89
|
}
|
90
|
|
91
|
/** in work
|
92
|
* @param code TODO*/
|
93
|
static public CdmDataSource NewH2EmbeddedInstance(String database, String username, String password, String filePath){
|
94
|
//FIXME in work
|
95
|
int port = -1;
|
96
|
H2Mode mode = H2Mode.EMBEDDED;
|
97
|
CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, database, port, username, password, filePath, mode);
|
98
|
return dataSource;
|
99
|
}
|
100
|
|
101
|
/** in work */
|
102
|
static public CdmDataSource NewH2InMemoryInstance(){
|
103
|
//FIXME in work
|
104
|
int port = -1;
|
105
|
H2Mode mode = H2Mode.IN_MEMORY;
|
106
|
String username = "sa";
|
107
|
String password = "";
|
108
|
CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, null, port, username, password, null, mode);
|
109
|
return dataSource;
|
110
|
}
|
111
|
|
112
|
|
113
|
public static CdmDataSource NewInstance(ICdmDataSource dataSource) {
|
114
|
return new CdmDataSource(dataSource.getDatabaseType(),
|
115
|
dataSource.getServer(),
|
116
|
dataSource.getDatabase(),
|
117
|
dataSource.getPort(),
|
118
|
dataSource.getUsername(),
|
119
|
dataSource.getPassword(),
|
120
|
dataSource.getFilePath(),
|
121
|
dataSource.getMode());
|
122
|
}
|
123
|
/**
|
124
|
* @param server
|
125
|
* @param database
|
126
|
* @param port
|
127
|
*/
|
128
|
protected CdmDataSource(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password, String filePath, H2Mode mode) {
|
129
|
super();
|
130
|
this.dbType = dbType;
|
131
|
this.server = server;
|
132
|
this.database = database;
|
133
|
this.port = port;
|
134
|
this.username = username;
|
135
|
this.password = password;
|
136
|
this.initMethodName = dbType.getInitMethod();
|
137
|
this.destroyMethodName = dbType.getDestroyMethod();
|
138
|
this.filePath = filePath;
|
139
|
this.mode = mode;
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public String getName() {
|
144
|
return database;
|
145
|
}
|
146
|
|
147
|
@Override
|
148
|
public String getServer() {
|
149
|
return server;
|
150
|
}
|
151
|
|
152
|
@Override
|
153
|
public int getPort() {
|
154
|
return port;
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public BeanDefinition getDatasourceBean(){
|
159
|
AbstractBeanDefinition bd = new RootBeanDefinition(dbType.getDataSourceClass());
|
160
|
//attributes
|
161
|
bd.setLazyInit(isLazy);
|
162
|
if (! CdmUtils.Nz(initMethodName).trim().equals("") ){
|
163
|
bd.setInitMethodName(initMethodName);
|
164
|
}
|
165
|
if (! CdmUtils.Nz(destroyMethodName).trim().equals("") ){
|
166
|
bd.setInitMethodName(destroyMethodName);
|
167
|
}
|
168
|
|
169
|
//properties
|
170
|
MutablePropertyValues props = new MutablePropertyValues();
|
171
|
Properties persistentProperties = getDatasourceProperties();
|
172
|
Enumeration<Object> keys = persistentProperties.keys();
|
173
|
while (keys.hasMoreElements()){
|
174
|
String key = (String)keys.nextElement();
|
175
|
props.addPropertyValue(key, persistentProperties.getProperty(key));
|
176
|
// Properties a = Environment.getProperties();
|
177
|
}
|
178
|
|
179
|
bd.setPropertyValues(props);
|
180
|
return bd;
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* Returns the list of properties that are defined in the datasource
|
185
|
* @return
|
186
|
*/
|
187
|
private Properties getDatasourceProperties(){
|
188
|
Properties result = new Properties();
|
189
|
result.put("driverClassName", dbType.getDriverClassName());
|
190
|
String connectionString = dbType.getConnectionString(this);
|
191
|
result.put("url", connectionString);
|
192
|
result.put("username", username);
|
193
|
result.put("password", password);
|
194
|
return result;
|
195
|
}
|
196
|
|
197
|
|
198
|
@Override
|
199
|
public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll){
|
200
|
HibernateConfiguration hibernateConfig = HibernateConfiguration.NewDefaultInstance();
|
201
|
return getHibernatePropertiesBean(hbm2dll, hibernateConfig);
|
202
|
}
|
203
|
|
204
|
@Override
|
205
|
@Deprecated
|
206
|
public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll, Boolean showSql, Boolean formatSql,
|
207
|
Boolean registerSearchListener, Class<? extends RegionFactory> cacheProviderClass){
|
208
|
HibernateConfiguration hibernateConfig = HibernateConfiguration.NewInstance(showSql, formatSql,
|
209
|
registerSearchListener, null, cacheProviderClass);
|
210
|
return getHibernatePropertiesBean(hbm2dll, hibernateConfig);
|
211
|
}
|
212
|
|
213
|
@Override
|
214
|
public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll,
|
215
|
HibernateConfiguration hibernateConfig){
|
216
|
if (hibernateConfig == null){
|
217
|
hibernateConfig = new HibernateConfiguration(); //empty
|
218
|
}
|
219
|
|
220
|
boolean showSql = hibernateConfig.getShowSql(this.showSql);
|
221
|
boolean formatSql = hibernateConfig.getFormatSql(this.formatSql);
|
222
|
boolean registerAuditing = hibernateConfig.getRegisterEnvers(this.registerAuditing);
|
223
|
boolean registerSearchListener = hibernateConfig.getRegisterSearch(this.registerSearchListener);
|
224
|
Class<? extends RegionFactory> cacheProviderClass = hibernateConfig.getCacheProviderClass(this.cacheProviderClass);
|
225
|
|
226
|
//Hibernate default values
|
227
|
if (hbm2dll == null){
|
228
|
hbm2dll = this.hbm2dll;
|
229
|
}
|
230
|
|
231
|
AbstractBeanDefinition bd = makeHibernatePropertiesBean(dbType, hbm2dll, showSql, formatSql, registerAuditing,
|
232
|
registerSearchListener, cacheProviderClass);
|
233
|
return bd;
|
234
|
}
|
235
|
|
236
|
public String getInitMethodName() {
|
237
|
return initMethodName;
|
238
|
}
|
239
|
|
240
|
public void setInitMethodName(String initMethodName) {
|
241
|
this.initMethodName = initMethodName;
|
242
|
}
|
243
|
|
244
|
public String getDestroyMethodName() {
|
245
|
return destroyMethodName;
|
246
|
}
|
247
|
|
248
|
public void setDestroyMethodName(String destroyMethodName) {
|
249
|
this.destroyMethodName = destroyMethodName;
|
250
|
}
|
251
|
|
252
|
@Override
|
253
|
public String getDatabase() {
|
254
|
return database;
|
255
|
}
|
256
|
|
257
|
@Override
|
258
|
public void setDatabase(String database) {
|
259
|
this.database = database;
|
260
|
}
|
261
|
|
262
|
@Override
|
263
|
public DatabaseTypeEnum getDatabaseType() {
|
264
|
return dbType;
|
265
|
}
|
266
|
|
267
|
@Override
|
268
|
public String getFilePath() {
|
269
|
return filePath;
|
270
|
}
|
271
|
|
272
|
@Override
|
273
|
public H2Mode getMode() {
|
274
|
return mode;
|
275
|
}
|
276
|
|
277
|
@Override
|
278
|
public void setMode(H2Mode h2Mode) {
|
279
|
this.mode = h2Mode;
|
280
|
|
281
|
}
|
282
|
|
283
|
@Override
|
284
|
public String getPassword() {
|
285
|
return password;
|
286
|
}
|
287
|
|
288
|
@Override
|
289
|
public void setPassword(String password) {
|
290
|
this.password = password;
|
291
|
|
292
|
}
|
293
|
|
294
|
@Override
|
295
|
public String getUsername() {
|
296
|
return username;
|
297
|
}
|
298
|
|
299
|
@Override
|
300
|
public void setUsername(String username) {
|
301
|
this.username = username;
|
302
|
|
303
|
}
|
304
|
|
305
|
//********************* TO STRING() **********************/
|
306
|
|
307
|
@Override
|
308
|
public String toString() {
|
309
|
if (StringUtils.isBlank(this.database)){
|
310
|
return super.toString();
|
311
|
}else{
|
312
|
String result = "DataSource<" + dbType.getConnectionString(this).replace(CdmUtils.Nz(password), "") + ">";
|
313
|
return result;
|
314
|
}
|
315
|
}
|
316
|
|
317
|
}
|
318
|
|