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