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.io.File;
|
13
|
import java.io.IOException;
|
14
|
import java.sql.Connection;
|
15
|
import java.sql.Driver;
|
16
|
import java.sql.DriverManager;
|
17
|
import java.sql.SQLException;
|
18
|
import java.util.Properties;
|
19
|
|
20
|
import org.apache.commons.dbcp.BasicDataSource;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.hsqldb.Server;
|
23
|
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
|
26
|
|
27
|
|
28
|
/**
|
29
|
* @author a.mueller
|
30
|
*
|
31
|
*/
|
32
|
|
33
|
public class LocalHsqldb extends BasicDataSource {
|
34
|
private static final Logger logger = Logger.getLogger(LocalHsqldb.class);
|
35
|
|
36
|
private String sep = System.getProperty("file.separator");
|
37
|
|
38
|
/** url without database name */
|
39
|
protected String pureUrl = "jdbc:hsqldb:hsql://localhost/";
|
40
|
/** database name */
|
41
|
protected String dbName = "cdm";
|
42
|
/** path, where database should be stored in the file system */
|
43
|
protected String databasePath = getDefaultPath();
|
44
|
/** Server instance */
|
45
|
protected Server hsqldbServer;
|
46
|
/** if true starts server on init() */
|
47
|
protected boolean isStartServer = true;
|
48
|
/** makes the Server silent (no messages) */
|
49
|
protected boolean isSilent = true;
|
50
|
/** default driver class name */
|
51
|
protected String DEFAULT_DRIVER_CLASS_NAME = "org.hsqldb.jdbcDriver";
|
52
|
|
53
|
|
54
|
/**
|
55
|
*
|
56
|
*/
|
57
|
public LocalHsqldb() {
|
58
|
setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
|
59
|
setComposedUrl();
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* @param url
|
64
|
* @throws CannotGetJdbcConnectionException
|
65
|
*/
|
66
|
public LocalHsqldb(String url) throws CannotGetJdbcConnectionException {
|
67
|
super();
|
68
|
this.setUrl(url);
|
69
|
setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* @param url
|
74
|
* @param username
|
75
|
* @param password
|
76
|
* @throws CannotGetJdbcConnectionException
|
77
|
*/
|
78
|
public LocalHsqldb(String url, String username, String password)
|
79
|
throws CannotGetJdbcConnectionException {
|
80
|
super();
|
81
|
this.setUrl(url);
|
82
|
this.setUsername(username);
|
83
|
this.setPassword(password);
|
84
|
this.setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* @param driverClassName
|
89
|
* @param url
|
90
|
* @param username
|
91
|
* @param password
|
92
|
* @throws CannotGetJdbcConnectionException
|
93
|
*/
|
94
|
public LocalHsqldb(String driverClassName, String url, String username,
|
95
|
String password) throws CannotGetJdbcConnectionException {
|
96
|
super();
|
97
|
this.setUrl(url);
|
98
|
this.setUsername(username);
|
99
|
this.setPassword(password);
|
100
|
this.setDriverClassName(driverClassName);
|
101
|
}
|
102
|
|
103
|
public void init(){
|
104
|
if (isStartServer){
|
105
|
this.startHsqldbServer();
|
106
|
}
|
107
|
}
|
108
|
|
109
|
public void destroy(){
|
110
|
this.stopHsqldbServer();
|
111
|
}
|
112
|
|
113
|
|
114
|
/* (non-Javadoc)
|
115
|
* @see org.springframework.jdbc.datasource.BasicDataSource#getUrl()
|
116
|
*/
|
117
|
@Override
|
118
|
public String getUrl() {
|
119
|
return super.getUrl();
|
120
|
}
|
121
|
|
122
|
/* (non-Javadoc)
|
123
|
* @see org.springframework.jdbc.datasource.BasicDataSource#setUrl(java.lang.String)
|
124
|
*/
|
125
|
@Override
|
126
|
public void setUrl(String url) {
|
127
|
super.setUrl(url);
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* @return the pureUrl
|
132
|
*/
|
133
|
public String getPureUrl() {
|
134
|
return pureUrl;
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* @param pureUrl the pureUrl to set
|
139
|
*/
|
140
|
public void setPureUrl(String pureUrl) {
|
141
|
this.pureUrl = pureUrl;
|
142
|
if (dbName != null){
|
143
|
setComposedUrl();
|
144
|
}
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* @return the dbName
|
149
|
*/
|
150
|
public String getDbName() {
|
151
|
return dbName;
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* @param dbName the dbName to set
|
156
|
*/
|
157
|
public void setDbName(String dbName) {
|
158
|
this.dbName = dbName;
|
159
|
if (pureUrl != null){
|
160
|
setComposedUrl();
|
161
|
}
|
162
|
}
|
163
|
|
164
|
private void setComposedUrl(){
|
165
|
setUrl(getPureUrl() + getDbName());
|
166
|
}
|
167
|
|
168
|
//checks if hsqldb-server is started, if not it will be started
|
169
|
private void startHsqldbServer(){
|
170
|
try {
|
171
|
Driver driver = DriverManager.getDriver(getUrl());
|
172
|
Properties prop = new Properties();
|
173
|
prop.setProperty("user", this.getUsername());
|
174
|
prop.setProperty("password", this.getPassword());
|
175
|
Connection con = driver.connect(getUrl(), prop);
|
176
|
if (con == null) {
|
177
|
logger.warn("Connection to URL " + getUrl() + " could not be established");
|
178
|
throw new SQLException();
|
179
|
}
|
180
|
} catch (SQLException e) {
|
181
|
try {
|
182
|
//server is probably not runing on the url (or login is wrong !!)
|
183
|
logger.info("Start HsqldbServer");
|
184
|
hsqldbServer = new Server();
|
185
|
hsqldbServer.setSilent(this.isSilent);
|
186
|
if (logger.isDebugEnabled()){
|
187
|
for (int i = 0; i < 10; i++){
|
188
|
logger.info("DatabaseName " + i + ": " + hsqldbServer.getDatabaseName(i, true));
|
189
|
logger.info("DatabaseName " + i + ": " + hsqldbServer.getDatabaseName(i, false));
|
190
|
logger.info("DatabasePath " + i + ": " + hsqldbServer.getDatabasePath(i, true));
|
191
|
logger.info("DatabasePath " + i + ": " + hsqldbServer.getDatabasePath(i, false));
|
192
|
logger.info("DatabaseType " + i + ": " + hsqldbServer.getDatabaseType(i));
|
193
|
}
|
194
|
}
|
195
|
hsqldbServer.setDatabaseName(0, getDbName());
|
196
|
hsqldbServer.setDatabasePath(0, getFilePath());
|
197
|
hsqldbServer.start();
|
198
|
hsqldbServer.checkRunning(true);
|
199
|
} catch (RuntimeException e1) {
|
200
|
logger.error("Local hsqlServer could not be started or connection to existing server could not be established.");
|
201
|
}
|
202
|
}
|
203
|
}
|
204
|
|
205
|
|
206
|
/**
|
207
|
* stops the Hsqldb Server
|
208
|
*/
|
209
|
private void stopHsqldbServer(){
|
210
|
if (hsqldbServer != null){
|
211
|
logger.info("stop HsqldbServer");
|
212
|
hsqldbServer.stop();
|
213
|
}
|
214
|
}
|
215
|
|
216
|
private static final String getDefaultPath(){
|
217
|
try {
|
218
|
File path = CdmApplicationUtils.getWritableResourceDir();
|
219
|
String subPath = File.separator + "hsqlDb" + File.separator + "LocalHsqldb";
|
220
|
return path + subPath;
|
221
|
} catch (IOException e) {
|
222
|
logger.error(e);
|
223
|
throw new RuntimeException(e);
|
224
|
}
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* @return the dbPath
|
229
|
*/
|
230
|
public String getFilePath() {
|
231
|
return databasePath;
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* @param dbPath the dbPath to set
|
236
|
*/
|
237
|
public void setFilePath(String filePath) {
|
238
|
if (databasePath.endsWith(sep)){
|
239
|
databasePath = databasePath + "localCdm";
|
240
|
}
|
241
|
this.databasePath = filePath;
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* @return the isStartServer
|
246
|
*/
|
247
|
public boolean isStartServer() {
|
248
|
return isStartServer;
|
249
|
}
|
250
|
|
251
|
/**
|
252
|
* @param isStartServer the isStartServer to set
|
253
|
*/
|
254
|
public void setStartServer(boolean isStartServer) {
|
255
|
this.isStartServer = isStartServer;
|
256
|
}
|
257
|
|
258
|
/**
|
259
|
* @return the isSilent
|
260
|
*/
|
261
|
public boolean isSilent() {
|
262
|
return isSilent;
|
263
|
}
|
264
|
|
265
|
/**
|
266
|
* @param isSilent the isSilent to set
|
267
|
*/
|
268
|
public void setSilent(boolean isSilent) {
|
269
|
if (this.hsqldbServer != null){
|
270
|
this.hsqldbServer.setSilent(isSilent);
|
271
|
}
|
272
|
this.isSilent = isSilent;
|
273
|
}
|
274
|
|
275
|
|
276
|
|
277
|
}
|