2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.database
;
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
;
20 import org
.apache
.commons
.dbcp
.BasicDataSource
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.hsqldb
.Server
;
23 import org
.springframework
.jdbc
.CannotGetJdbcConnectionException
;
25 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationUtils
;
33 public class LocalHsqldb
extends BasicDataSource
{
34 private static final Logger logger
= Logger
.getLogger(LocalHsqldb
.class);
36 private String sep
= System
.getProperty("file.separator");
38 /** url without database name */
39 protected String pureUrl
= "jdbc:hsqldb:hsql://localhost/";
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";
57 public LocalHsqldb() {
58 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
64 * @throws CannotGetJdbcConnectionException
66 public LocalHsqldb(String url
) throws CannotGetJdbcConnectionException
{
69 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
76 * @throws CannotGetJdbcConnectionException
78 public LocalHsqldb(String url
, String username
, String password
)
79 throws CannotGetJdbcConnectionException
{
82 this.setUsername(username
);
83 this.setPassword(password
);
84 this.setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
88 * @param driverClassName
92 * @throws CannotGetJdbcConnectionException
94 public LocalHsqldb(String driverClassName
, String url
, String username
,
95 String password
) throws CannotGetJdbcConnectionException
{
98 this.setUsername(username
);
99 this.setPassword(password
);
100 this.setDriverClassName(driverClassName
);
105 this.startHsqldbServer();
109 public void destroy(){
110 this.stopHsqldbServer();
115 * @see org.springframework.jdbc.datasource.BasicDataSource#getUrl()
118 public String
getUrl() {
119 return super.getUrl();
123 * @see org.springframework.jdbc.datasource.BasicDataSource#setUrl(java.lang.String)
126 public void setUrl(String url
) {
131 * @return the pureUrl
133 public String
getPureUrl() {
138 * @param pureUrl the pureUrl to set
140 public void setPureUrl(String pureUrl
) {
141 this.pureUrl
= pureUrl
;
150 public String
getDbName() {
155 * @param dbName the dbName to set
157 public void setDbName(String dbName
) {
158 this.dbName
= dbName
;
159 if (pureUrl
!= null){
164 private void setComposedUrl(){
165 setUrl(getPureUrl() + getDbName());
168 //checks if hsqldb-server is started, if not it will be started
169 private void startHsqldbServer(){
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
);
177 logger
.warn("Connection to URL " + getUrl() + " could not be established");
178 throw new SQLException();
180 } catch (SQLException e
) {
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
));
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.");
207 * stops the Hsqldb Server
209 private void stopHsqldbServer(){
210 if (hsqldbServer
!= null){
211 logger
.info("stop HsqldbServer");
216 private static final String
getDefaultPath(){
218 File path
= CdmApplicationUtils
.getWritableResourceDir();
219 String subPath
= File
.separator
+ "hsqlDb" + File
.separator
+ "LocalHsqldb";
220 return path
+ subPath
;
221 } catch (IOException e
) {
223 throw new RuntimeException(e
);
230 public String
getFilePath() {
235 * @param dbPath the dbPath to set
237 public void setFilePath(String filePath
) {
238 if (databasePath
.endsWith(sep
)){
239 databasePath
= databasePath
+ "localCdm";
241 this.databasePath
= filePath
;
245 * @return the isStartServer
247 public boolean isStartServer() {
248 return isStartServer
;
252 * @param isStartServer the isStartServer to set
254 public void setStartServer(boolean isStartServer
) {
255 this.isStartServer
= isStartServer
;
259 * @return the isSilent
261 public boolean isSilent() {
266 * @param isSilent the isSilent to set
268 public void setSilent(boolean isSilent
) {
269 if (this.hsqldbServer
!= null){
270 this.hsqldbServer
.setSilent(isSilent
);
272 this.isSilent
= isSilent
;