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.
9 package eu
.etaxonomy
.cdm
.database
;
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
;
19 import javax
.sql
.DataSource
;
21 import org
.apache
.commons
.dbcp
.BasicDataSource
;
22 import org
.apache
.commons
.pool
.impl
.GenericObjectPool
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.h2
.tools
.Server
;
25 import org
.springframework
.jdbc
.CannotGetJdbcConnectionException
;
27 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationUtils
;
35 public class LocalH2
extends BasicDataSource
{
36 private static final Logger logger
= Logger
.getLogger(LocalH2
.class);
38 private final String sep
= System
.getProperty("file.separator");
40 /** url without database name */
41 protected String pureUrl
= "jdbc:h2:";
43 protected String dbName
= "cdm";
44 /** path, where database should be stored in the file system */
45 protected String databasePath
= getDefaultPath();
46 /** Server instance */
47 protected Server h2Server
;
48 /** if true starts server on init() */
49 protected boolean isStartServer
= true;
50 /** makes the Server silent (no messages) */
51 protected boolean isSilent
= true;
52 /** default driver class name */
53 protected String DEFAULT_DRIVER_CLASS_NAME
= "org.h2.Driver";
54 String mode
= H2Mode
.EMBEDDED
.toString();
57 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
63 * @throws CannotGetJdbcConnectionException
65 public LocalH2(String url
) throws CannotGetJdbcConnectionException
{
68 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
71 /* FIXME This is a workaround to solve a problem with dbcp connection pooling.
72 * Remove this when dbcp connection pool gets configured correctly
75 * @see org.apache.commons.dbcp.BasicDataSource#createDataSource()
78 protected synchronized DataSource
createDataSource() throws SQLException
{
79 super.createDataSource();
80 connectionPool
.setWhenExhaustedAction(GenericObjectPool
.WHEN_EXHAUSTED_GROW
);
88 * @throws CannotGetJdbcConnectionException
90 public LocalH2(String url
, String username
, String password
)
91 throws CannotGetJdbcConnectionException
{
93 this.setUsername(username
);
94 this.setPassword(password
);
98 * @param driverClassName
102 * @throws CannotGetJdbcConnectionException
104 public LocalH2(String driverClassName
, String url
, String username
,
105 String password
) throws CannotGetJdbcConnectionException
{
106 this(url
, username
, password
);
107 this.setDriverClassName(driverClassName
);
110 //** ********************************************************************************/
113 logger
.info("LocalH2init");
114 if (true){ //starting sever is not necessary for H2
118 this.startH2Server();
122 public void destroy(){
126 //checks if h2-server is started, if not it will be started (taken over from hsqldb, maybe not necessary for H2
127 private void startH2Server(){
129 Driver driver
= DriverManager
.getDriver(getUrl());
130 Properties prop
= new Properties();
131 prop
.setProperty("user", this.getUsername());
132 prop
.setProperty("password", this.getPassword());
133 Connection con
= driver
.connect(getUrl(), prop
);
135 logger
.warn("Connection to URL " + getUrl() + " could not be established");
136 throw new SQLException();
138 } catch (SQLException e
) {
140 //server is probably not running on the url (or login is wrong !!)
141 logger
.info("Start H2Server");
142 String
[] args
= new String
[] { "-trace" };
143 h2Server
= Server
.createTcpServer(args
).start();
144 // h2Server.setDatabaseName(0, getDbName());
145 // h2Server.setDatabasePath(0, getDatabasePath());
147 } catch (SQLException sqle1
) {
148 logger
.error("SQL Exception when starting Local H2Server: "+ sqle1
);
149 } catch (RuntimeException e1
) {
150 logger
.error("Local H2Server could not be started or connection to existing server could not be established.");
157 * stops the Hsqldb Server
159 private void stopH2Server(){
160 if (h2Server
!= null){
161 logger
.info("stop H2Server");
166 private static final String
getDefaultPath(){
169 path
= CdmApplicationUtils
.getWritableResourceDir();
170 } catch (IOException e
) {
172 throw new RuntimeException(e
);
174 String subPath
= File
.separator
+ "h2" + File
.separator
+ "LocalH2";
175 return path
+ subPath
;
178 public String
getDatabasePath() {
182 public void setDatabasePath(String databasePath
) {
183 if (databasePath
.endsWith(sep
)){
184 databasePath
= databasePath
+ "localCdm";
186 this.databasePath
= databasePath
;
189 public boolean isStartServer() {
190 return isStartServer
;
193 public void setStartServer(boolean isStartServer
) {
194 this.isStartServer
= isStartServer
;
197 public void setLocalUrl(){
198 String dbName
= "cdmLocal";
199 String localUrlString
= pureUrl
+ "file:" + getDefaultPath() + "/" + dbName
;
200 logger
.info("setLocalUrl: " + localUrlString
);
201 setUrl(localUrlString
);
204 public void setMode(String mode
){
208 public String
getMode(){