Project

General

Profile

Download (6.62 KB) Statistics
| Branch: | Tag: | Revision:
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.hsqldb.Server;
21
import org.springframework.jdbc.CannotGetJdbcConnectionException;
22
import org.springframework.jdbc.datasource.DriverManagerDataSource;
23
//import org.springframework.jdbc.datasource.DriverManagerDataSource;
24

    
25
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
26

    
27

    
28
/**
29
 * @author a.mueller
30
 *
31
 */
32

    
33
public class LocalHsqldb extends DriverManagerDataSource {
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(url);
68
		setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
69
	}
70

    
71
	/**
72
	 * @param url
73
	 * @param username
74
	 * @param password
75
	 * @throws CannotGetJdbcConnectionException
76
	 */
77
	public LocalHsqldb(String url, String username, String password)
78
			throws CannotGetJdbcConnectionException {
79
		super(url, username, password);
80
		this.setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
81
	}
82

    
83
	/**
84
	 * @param driverClassName
85
	 * @param url
86
	 * @param username
87
	 * @param password
88
	 * @throws CannotGetJdbcConnectionException
89
	 */
90
	public LocalHsqldb(String driverClassName, String url, String username,
91
			String password) throws CannotGetJdbcConnectionException {
92
		super(driverClassName, url, username, password);
93
	}
94

    
95
	public void init(){
96
		if (isStartServer){
97
			this.startHsqldbServer();
98
		}
99
	}
100
	
101
	public void destroy(){
102
		this.stopHsqldbServer();
103
	}
104
	
105

    
106
	/* (non-Javadoc)
107
	 * @see org.springframework.jdbc.datasource.DriverManagerDataSource#getUrl()
108
	 */
109
	@Override
110
	public String getUrl() {
111
		return super.getUrl();
112
	}
113

    
114
	/* (non-Javadoc)
115
	 * @see org.springframework.jdbc.datasource.DriverManagerDataSource#setUrl(java.lang.String)
116
	 */
117
	@Override
118
	public void setUrl(String url) {
119
		super.setUrl(url);
120
	}
121

    
122
	/**
123
	 * @return the pureUrl
124
	 */
125
	public String getPureUrl() {
126
		return pureUrl;
127
	}
128

    
129
	/**
130
	 * @param pureUrl the pureUrl to set
131
	 */
132
	public void setPureUrl(String pureUrl) {
133
		this.pureUrl = pureUrl;
134
		if (dbName != null){
135
			setComposedUrl();
136
		}
137
	}
138

    
139
	/**
140
	 * @return the dbName
141
	 */
142
	public String getDbName() {
143
		return dbName;
144
	}
145

    
146
	/**
147
	 * @param dbName the dbName to set
148
	 */
149
	public void setDbName(String dbName) {
150
		this.dbName = dbName;
151
		if (pureUrl != null){
152
			setComposedUrl();
153
		}
154
	}
155
	
156
	private void setComposedUrl(){
157
		setUrl(getPureUrl() + getDbName());
158
	}
159
	
160
	//checks if hsqldb-server is started, if not it will be started	
161
	private void startHsqldbServer(){
162
		try {
163
			Driver driver = DriverManager.getDriver(getUrl());
164
			Properties prop = new Properties();
165
			prop.setProperty("user", this.getUsername());
166
			prop.setProperty("password", this.getPassword());
167
			Connection con = driver.connect(getUrl(),  prop);
168
			if (con == null) {
169
				logger.warn("Connection to URL " +  getUrl() +  " could not be established");
170
				throw new SQLException();
171
			}
172
		} catch (SQLException e) {
173
			try {
174
				//server is probably not runing on the url (or login is wrong !!)
175
				logger.info("Start HsqldbServer"); 
176
				hsqldbServer = new Server();
177
				hsqldbServer.setSilent(this.isSilent);
178
				if (logger.isDebugEnabled()){
179
					for (int i = 0; i < 10; i++){
180
						logger.info("DatabaseName " + i + ": " + hsqldbServer.getDatabaseName(i, true));
181
						logger.info("DatabaseName " + i + ": " + hsqldbServer.getDatabaseName(i, false));
182
						logger.info("DatabasePath " + i + ": " + hsqldbServer.getDatabasePath(i, true));
183
						logger.info("DatabasePath " + i + ": " + hsqldbServer.getDatabasePath(i, false));
184
						logger.info("DatabaseType " + i + ": " + hsqldbServer.getDatabaseType(i));
185
					}
186
				}
187
				hsqldbServer.setDatabaseName(0, getDbName());
188
				hsqldbServer.setDatabasePath(0,  getFilePath());
189
				hsqldbServer.start();
190
				hsqldbServer.checkRunning(true);
191
			} catch (RuntimeException e1) {
192
				logger.error("Local hsqlServer could not be started or connection to existing server could not be established.");
193
			}
194
		}
195
	}
196
	
197
	
198
	/**
199
	 * stops the Hsqldb Server
200
	 */
201
	private void stopHsqldbServer(){
202
		if (hsqldbServer != null){
203
			logger.info("stop HsqldbServer");
204
			hsqldbServer.stop();
205
		}
206
	}
207
	
208
	private static final String getDefaultPath(){
209
		//String path = System.getProperty("user.dir");
210
		File path = CdmApplicationUtils.getWritableResourceDir();
211
		String subPath = File.separator + "hsqlDb" + File.separator + "LocalHsqldb"; 
212
		return  path + subPath;
213
	}
214

    
215
	/**
216
	 * @return the dbPath
217
	 */
218
	public String getFilePath() {
219
		return databasePath;
220
	}
221

    
222
	/**
223
	 * @param dbPath the dbPath to set
224
	 */
225
	public void setFilePath(String filePath) {
226
		if (databasePath.endsWith(sep)){
227
			databasePath = databasePath + "localCdm";
228
		}
229
		this.databasePath = filePath;
230
	}
231

    
232
	/**
233
	 * @return the isStartServer
234
	 */
235
	public boolean isStartServer() {
236
		return isStartServer;
237
	}
238

    
239
	/**
240
	 * @param isStartServer the isStartServer to set
241
	 */
242
	public void setStartServer(boolean isStartServer) {
243
		this.isStartServer = isStartServer;
244
	}
245

    
246
	/**
247
	 * @return the isSilent
248
	 */
249
	public boolean isSilent() {
250
		return isSilent;
251
	}
252

    
253
	/**
254
	 * @param isSilent the isSilent to set
255
	 */
256
	public void setSilent(boolean isSilent) {
257
		if (this.hsqldbServer != null){
258
			this.hsqldbServer.setSilent(isSilent);
259
		}
260
		this.isSilent = isSilent;
261
	}
262
	
263
	
264

    
265
}
(10-10/11)