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