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.database.DatabaseTypeEnum;
|
26
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
27
|
|
28
|
/**
|
29
|
* @author a.mueller
|
30
|
*
|
31
|
*/
|
32
|
public class CdmDataSource extends CdmDataSourceBase {
|
33
|
@SuppressWarnings("unused")
|
34
|
private static final Logger logger = Logger.getLogger(CdmDataSource.class);
|
35
|
|
36
|
private DatabaseTypeEnum dbType;
|
37
|
private String server;
|
38
|
private String database;
|
39
|
private int port = -1;
|
40
|
private String username;
|
41
|
private String password;
|
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
|
static public CdmDataSource NewMySqlInstance(String server, String database, String username, String password){
|
56
|
return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, -1, username, password, null, null);
|
57
|
}
|
58
|
|
59
|
static public CdmDataSource NewMySqlInstance(String server, String database, int port, String username, String password){
|
60
|
return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, port, username, password, null, null);
|
61
|
}
|
62
|
|
63
|
static public CdmDataSource NewSqlServer2005Instance(String server, String database, String username, String password){
|
64
|
return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, -1, username, password, null, null);
|
65
|
}
|
66
|
|
67
|
static public CdmDataSource NewSqlServer2005Instance(String server, String database, int port, String username, String password){
|
68
|
return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, port, username, password, null, null);
|
69
|
}
|
70
|
|
71
|
/** in work */
|
72
|
static public CdmDataSource NewH2EmbeddedInstance(String database, String username, String password){
|
73
|
//FIXME in work
|
74
|
int port = -1;
|
75
|
H2Mode mode = H2Mode.EMBEDDED;
|
76
|
CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, database, port, username, password, null, mode);
|
77
|
return dataSource;
|
78
|
}
|
79
|
|
80
|
/** in work */
|
81
|
static public CdmDataSource NewH2InMemoryInstance(){
|
82
|
//FIXME in work
|
83
|
int port = -1;
|
84
|
H2Mode mode = H2Mode.IN_MEMORY;
|
85
|
String username = "sa";
|
86
|
String password = "";
|
87
|
CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, null, port, username, password, null, mode);
|
88
|
return dataSource;
|
89
|
}
|
90
|
|
91
|
|
92
|
/**
|
93
|
* @param server
|
94
|
* @param database
|
95
|
* @param port
|
96
|
*/
|
97
|
protected CdmDataSource(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password, String filePath, H2Mode mode) {
|
98
|
super();
|
99
|
this.dbType = dbType;
|
100
|
this.server = server;
|
101
|
this.database = database;
|
102
|
this.port = port;
|
103
|
this.username = username;
|
104
|
this.password = password;
|
105
|
this.initMethodName = dbType.getInitMethod();
|
106
|
this.destroyMethodName = dbType.getDestroyMethod();
|
107
|
this.filePath = filePath;
|
108
|
this.mode = mode;
|
109
|
}
|
110
|
|
111
|
|
112
|
/* (non-Javadoc)
|
113
|
* @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
|
114
|
* A CdmDataSource does not have a name representation therefor the database name is returned
|
115
|
*/
|
116
|
public String getName() {
|
117
|
return database;
|
118
|
}
|
119
|
|
120
|
/* (non-Javadoc)
|
121
|
* @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
|
122
|
*/
|
123
|
@SuppressWarnings("unchecked")
|
124
|
public BeanDefinition getDatasourceBean(){
|
125
|
AbstractBeanDefinition bd = new RootBeanDefinition(dbType.getDriverManagerDataSourceClass());
|
126
|
//attributes
|
127
|
bd.setLazyInit(isLazy);
|
128
|
if (! CdmUtils.Nz(initMethodName).trim().equals("") ){
|
129
|
bd.setInitMethodName(initMethodName);
|
130
|
}
|
131
|
if (! CdmUtils.Nz(destroyMethodName).trim().equals("") ){
|
132
|
bd.setInitMethodName(destroyMethodName);
|
133
|
}
|
134
|
|
135
|
//properties
|
136
|
MutablePropertyValues props = new MutablePropertyValues();
|
137
|
Properties persistentProperties = getDatasourceProperties();
|
138
|
Enumeration<String> keys = (Enumeration)persistentProperties.keys();
|
139
|
while (keys.hasMoreElements()){
|
140
|
String key = (String)keys.nextElement();
|
141
|
props.addPropertyValue(key, persistentProperties.getProperty(key));
|
142
|
}
|
143
|
|
144
|
bd.setPropertyValues(props);
|
145
|
return bd;
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Returns the list of properties that are defined in the datasource
|
150
|
* @return
|
151
|
*/
|
152
|
private Properties getDatasourceProperties(){
|
153
|
Properties result = new Properties();
|
154
|
result.put("driverClassName", dbType.getDriverClassName());
|
155
|
String connectionString = dbType.getConnectionString(this);
|
156
|
result.put("url", connectionString);
|
157
|
result.put("username", username);
|
158
|
result.put("password", password);
|
159
|
return result;
|
160
|
}
|
161
|
|
162
|
|
163
|
/* (non-Javadoc)
|
164
|
* @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
|
165
|
*/
|
166
|
public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll){
|
167
|
boolean showSql = false;
|
168
|
boolean formatSql = false;
|
169
|
boolean registerSearchListener = false;
|
170
|
Class<? extends CacheProvider> cacheProviderClass = NoCacheProvider.class;
|
171
|
return getHibernatePropertiesBean(hbm2dll, showSql, formatSql, registerSearchListener, cacheProviderClass);
|
172
|
}
|
173
|
|
174
|
/* (non-Javadoc)
|
175
|
* @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
|
176
|
*/
|
177
|
public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll, Boolean showSql, Boolean formatSql, Boolean registerSearchListener, Class<? extends CacheProvider> cacheProviderClass){
|
178
|
//Hibernate default values
|
179
|
if (hbm2dll == null){
|
180
|
hbm2dll = this.hbm2dll;
|
181
|
}
|
182
|
if (showSql == null){
|
183
|
showSql = this.showSql;
|
184
|
}
|
185
|
if (formatSql == null){
|
186
|
formatSql = this.formatSql;
|
187
|
}
|
188
|
if (cacheProviderClass == null){
|
189
|
cacheProviderClass = this.cacheProviderClass;
|
190
|
}
|
191
|
if(registerSearchListener == null){
|
192
|
registerSearchListener = this.registerSearchListener;
|
193
|
}
|
194
|
|
195
|
DatabaseTypeEnum dbtype = dbType;
|
196
|
AbstractBeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);
|
197
|
MutablePropertyValues hibernateProps = new MutablePropertyValues();
|
198
|
|
199
|
Properties props = new Properties();
|
200
|
props.setProperty("hibernate.hbm2ddl.auto", hbm2dll.toString());
|
201
|
props.setProperty("hibernate.dialect", dbtype.getHibernateDialect());
|
202
|
props.setProperty("hibernate.cache.provider_class", cacheProviderClass.getName());
|
203
|
props.setProperty("hibernate.show_sql", String.valueOf(showSql));
|
204
|
props.setProperty("hibernate.format_sql", String.valueOf(formatSql));
|
205
|
props.setProperty("hibernate.search.autoregister_listeners", String.valueOf(registerSearchListener));
|
206
|
|
207
|
hibernateProps.addPropertyValue("properties",props);
|
208
|
bd.setPropertyValues(hibernateProps);
|
209
|
return bd;
|
210
|
}
|
211
|
|
212
|
public String getInitMethodName() {
|
213
|
return initMethodName;
|
214
|
}
|
215
|
|
216
|
public void setInitMethodName(String initMethodName) {
|
217
|
this.initMethodName = initMethodName;
|
218
|
}
|
219
|
|
220
|
public String getDestroyMethodName() {
|
221
|
return destroyMethodName;
|
222
|
}
|
223
|
|
224
|
public void setDestroyMethodName(String destroyMethodName) {
|
225
|
this.destroyMethodName = destroyMethodName;
|
226
|
}
|
227
|
|
228
|
public String getDatabase() {
|
229
|
return database;
|
230
|
}
|
231
|
|
232
|
public DatabaseTypeEnum getDatabaseType() {
|
233
|
return dbType;
|
234
|
}
|
235
|
|
236
|
public String getFilePath() {
|
237
|
return filePath;
|
238
|
}
|
239
|
|
240
|
|
241
|
public int getPort() {
|
242
|
return port;
|
243
|
}
|
244
|
|
245
|
public String getServer() {
|
246
|
return server;
|
247
|
}
|
248
|
|
249
|
public H2Mode getMode() {
|
250
|
return mode;
|
251
|
}
|
252
|
|
253
|
/* (non-Javadoc)
|
254
|
* @see eu.etaxonomy.cdm.database.ICdmDataSource#getPassword()
|
255
|
*/
|
256
|
public String getPassword() {
|
257
|
return password;
|
258
|
}
|
259
|
|
260
|
/* (non-Javadoc)
|
261
|
* @see eu.etaxonomy.cdm.database.ICdmDataSource#getUserName()
|
262
|
*/
|
263
|
public String getUsername() {
|
264
|
return username;
|
265
|
}
|
266
|
|
267
|
|
268
|
|
269
|
|
270
|
}
|
271
|
|