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.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.log4j.Logger;
|
20
|
import org.h2.tools.Server;
|
21
|
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
22
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
|
25
|
|
26
|
|
27
|
/**
|
28
|
* @author a.mueller
|
29
|
*
|
30
|
* IN WORK
|
31
|
*
|
32
|
*/
|
33
|
|
34
|
public class LocalH2 extends DriverManagerDataSource {
|
35
|
private static final Logger logger = Logger.getLogger(LocalH2.class);
|
36
|
|
37
|
private String sep = System.getProperty("file.separator");
|
38
|
|
39
|
/** url without database name */
|
40
|
protected String pureUrl = "jdbc:h2:";
|
41
|
/** database name */
|
42
|
protected String dbName = "cdm";
|
43
|
/** path, where database should be stored in the file system */
|
44
|
protected String databasePath = getDefaultPath();
|
45
|
/** Server instance */
|
46
|
protected Server h2Server;
|
47
|
/** if true starts server on init() */
|
48
|
protected boolean isStartServer = true;
|
49
|
/** makes the Server silent (no messages) */
|
50
|
protected boolean isSilent = true;
|
51
|
/** default driver class name */
|
52
|
protected String DEFAULT_DRIVER_CLASS_NAME = "org.h2.Driver";
|
53
|
String mode = H2Mode.EMBEDDED.toString();
|
54
|
|
55
|
/**
|
56
|
*
|
57
|
*/
|
58
|
public LocalH2() {
|
59
|
setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
|
60
|
setLocalUrl();
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* @param url
|
65
|
* @throws CannotGetJdbcConnectionException
|
66
|
*/
|
67
|
public LocalH2(String url) throws CannotGetJdbcConnectionException {
|
68
|
super(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 LocalH2(String url, String username, String password)
|
79
|
throws CannotGetJdbcConnectionException {
|
80
|
super(url, username, password);
|
81
|
this.setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* @param driverClassName
|
86
|
* @param url
|
87
|
* @param username
|
88
|
* @param password
|
89
|
* @throws CannotGetJdbcConnectionException
|
90
|
*/
|
91
|
public LocalH2(String driverClassName, String url, String username,
|
92
|
String password) throws CannotGetJdbcConnectionException {
|
93
|
super(driverClassName, url, username, password);
|
94
|
}
|
95
|
|
96
|
//** ********************************************************************************/
|
97
|
|
98
|
public void init(){
|
99
|
logger.warn("LocalH2init");
|
100
|
if (true){ //starting sever is not necessary for H2
|
101
|
return;
|
102
|
}
|
103
|
if (isStartServer){
|
104
|
this.startH2Server();
|
105
|
}
|
106
|
}
|
107
|
|
108
|
public void destroy(){
|
109
|
this.stopH2Server();
|
110
|
}
|
111
|
|
112
|
|
113
|
//checks if h2-server is started, if not it will be started (taken over from hsqldb, maybe not necessary for H2
|
114
|
private void startH2Server(){
|
115
|
try {
|
116
|
Driver driver = DriverManager.getDriver(getUrl());
|
117
|
Properties prop = new Properties();
|
118
|
prop.setProperty("user", this.getUsername());
|
119
|
prop.setProperty("password", this.getPassword());
|
120
|
Connection con = driver.connect(getUrl(), prop);
|
121
|
if (con == null) {
|
122
|
logger.warn("Connection to URL " + getUrl() + " could not be established");
|
123
|
throw new SQLException();
|
124
|
}
|
125
|
} catch (SQLException e) {
|
126
|
try {
|
127
|
//server is probably not runing on the url (or login is wrong !!)
|
128
|
logger.info("Start H2Server");
|
129
|
String[] args = new String[] { "-trace" };
|
130
|
h2Server = Server.createTcpServer(args).start();
|
131
|
if (logger.isDebugEnabled()){
|
132
|
for (int i = 0; i < 10; i++){
|
133
|
// logger.info("DatabaseName " + i + ": " + h2Server.getDatabaseName(i, true));
|
134
|
// logger.info("DatabaseName " + i + ": " + h2Server.getDatabaseName(i, false));
|
135
|
// logger.info("DatabasePath " + i + ": " + h2Server.getDatabasePath(i, true));
|
136
|
// logger.info("DatabasePath " + i + ": " + h2Server.getDatabasePath(i, false));
|
137
|
// logger.info("DatabaseType " + i + ": " + h2Server.getDatabaseType(i));
|
138
|
}
|
139
|
}
|
140
|
// h2Server.setDatabaseName(0, getDbName());
|
141
|
// h2Server.setDatabasePath(0, getDatabasePath());
|
142
|
h2Server.start();
|
143
|
// h2Server.checkRunning(true);
|
144
|
} catch (SQLException sqle1) {
|
145
|
logger.error("SQL Exception when starting Local H2Server: "+ sqle1);
|
146
|
} catch (RuntimeException e1) {
|
147
|
logger.error("Local H2Server could not be started or connection to existing server could not be established.");
|
148
|
}
|
149
|
}
|
150
|
}
|
151
|
|
152
|
|
153
|
/**
|
154
|
* stops the Hsqldb Server
|
155
|
*/
|
156
|
private void stopH2Server(){
|
157
|
if (h2Server != null){
|
158
|
logger.info("stop H2Server");
|
159
|
h2Server.stop();
|
160
|
}
|
161
|
}
|
162
|
|
163
|
private static final String getDefaultPath(){
|
164
|
//String path = System.getProperty("user.dir");
|
165
|
File path = CdmApplicationUtils.getWritableResourceDir();
|
166
|
String subPath = File.separator + "h2" + File.separator + "LocalH2";
|
167
|
return path + subPath;
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* @return the dbPath
|
172
|
*/
|
173
|
public String getDatabasePath() {
|
174
|
return databasePath;
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* @param dbPath the dbPath to set
|
179
|
*/
|
180
|
public void setDatabasePath(String databasePath) {
|
181
|
if (databasePath.endsWith(sep)){
|
182
|
databasePath = databasePath + "localCdm";
|
183
|
}
|
184
|
this.databasePath = databasePath;
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* @return the isStartServer
|
189
|
*/
|
190
|
public boolean isStartServer() {
|
191
|
return isStartServer;
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* @param isStartServer the isStartServer to set
|
196
|
*/
|
197
|
public void setStartServer(boolean isStartServer) {
|
198
|
this.isStartServer = isStartServer;
|
199
|
}
|
200
|
|
201
|
public void setLocalUrl(){
|
202
|
logger.warn("setLocalUrl");
|
203
|
String dbName = "cdmLocal";
|
204
|
setUrl(pureUrl + "file:" + getDefaultPath() + "/" + dbName);
|
205
|
}
|
206
|
|
207
|
public void setMode(String mode){
|
208
|
this.mode = mode;
|
209
|
}
|
210
|
|
211
|
public String getMode(){
|
212
|
return mode;
|
213
|
}
|
214
|
|
215
|
|
216
|
}
|