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 javax
.sql
.DataSource
;
22 import org
.apache
.commons
.dbcp
.BasicDataSource
;
23 import org
.apache
.commons
.pool
.impl
.GenericObjectPool
;
24 import org
.apache
.log4j
.Logger
;
25 import org
.h2
.tools
.Server
;
26 import org
.springframework
.jdbc
.CannotGetJdbcConnectionException
;
28 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationUtils
;
29 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
39 public class LocalH2
extends BasicDataSource
{
40 private static final Logger logger
= Logger
.getLogger(LocalH2
.class);
42 private String sep
= System
.getProperty("file.separator");
44 /** url without database name */
45 protected String pureUrl
= "jdbc:h2:";
47 protected String dbName
= "cdm";
48 /** path, where database should be stored in the file system */
49 protected String databasePath
= getDefaultPath();
50 /** Server instance */
51 protected Server h2Server
;
52 /** if true starts server on init() */
53 protected boolean isStartServer
= true;
54 /** makes the Server silent (no messages) */
55 protected boolean isSilent
= true;
56 /** default driver class name */
57 protected String DEFAULT_DRIVER_CLASS_NAME
= "org.h2.Driver";
58 String mode
= H2Mode
.EMBEDDED
.toString();
60 private NomenclaturalCode nomenclaturalCode
;
66 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
72 * @throws CannotGetJdbcConnectionException
74 public LocalH2(String url
) throws CannotGetJdbcConnectionException
{
77 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
80 /* FIXME This is a workaround to solve a problem with dbcp connection pooling.
81 * Remove this when dbcp connection pool gets configured correctly
84 * @see org.apache.commons.dbcp.BasicDataSource#createDataSource()
87 protected synchronized DataSource
createDataSource() throws SQLException
{
88 super.createDataSource();
89 connectionPool
.setWhenExhaustedAction(GenericObjectPool
.WHEN_EXHAUSTED_GROW
);
97 * @throws CannotGetJdbcConnectionException
99 public LocalH2(String url
, String username
, String password
)
100 throws CannotGetJdbcConnectionException
{
102 this.setUsername(username
);
103 this.setPassword(password
);
107 * @param driverClassName
111 * @throws CannotGetJdbcConnectionException
113 public LocalH2(String driverClassName
, String url
, String username
,
114 String password
) throws CannotGetJdbcConnectionException
{
115 this(url
, username
, password
);
116 this.setDriverClassName(driverClassName
);
119 //** ********************************************************************************/
122 logger
.info("LocalH2init");
123 if (true){ //starting sever is not necessary for H2
127 this.startH2Server();
131 public void destroy(){
136 //checks if h2-server is started, if not it will be started (taken over from hsqldb, maybe not necessary for H2
137 private void startH2Server(){
139 Driver driver
= DriverManager
.getDriver(getUrl());
140 Properties prop
= new Properties();
141 prop
.setProperty("user", this.getUsername());
142 prop
.setProperty("password", this.getPassword());
143 Connection con
= driver
.connect(getUrl(), prop
);
145 logger
.warn("Connection to URL " + getUrl() + " could not be established");
146 throw new SQLException();
148 } catch (SQLException e
) {
150 //server is probably not runing on the url (or login is wrong !!)
151 logger
.info("Start H2Server");
152 String
[] args
= new String
[] { "-trace" };
153 h2Server
= Server
.createTcpServer(args
).start();
154 // h2Server.setDatabaseName(0, getDbName());
155 // h2Server.setDatabasePath(0, getDatabasePath());
157 } catch (SQLException sqle1
) {
158 logger
.error("SQL Exception when starting Local H2Server: "+ sqle1
);
159 } catch (RuntimeException e1
) {
160 logger
.error("Local H2Server could not be started or connection to existing server could not be established.");
167 * stops the Hsqldb Server
169 private void stopH2Server(){
170 if (h2Server
!= null){
171 logger
.info("stop H2Server");
176 private static final String
getDefaultPath(){
179 path
= CdmApplicationUtils
.getWritableResourceDir();
180 } catch (IOException e
) {
182 throw new RuntimeException(e
);
184 String subPath
= File
.separator
+ "h2" + File
.separator
+ "LocalH2";
185 return path
+ subPath
;
191 public String
getDatabasePath() {
196 * @param dbPath the dbPath to set
198 public void setDatabasePath(String databasePath
) {
199 if (databasePath
.endsWith(sep
)){
200 databasePath
= databasePath
+ "localCdm";
202 this.databasePath
= databasePath
;
206 * @return the isStartServer
208 public boolean isStartServer() {
209 return isStartServer
;
213 * @param isStartServer the isStartServer to set
215 public void setStartServer(boolean isStartServer
) {
216 this.isStartServer
= isStartServer
;
219 public void setLocalUrl(){
220 String dbName
= "cdmLocal";
221 String localUrlString
= pureUrl
+ "file:" + getDefaultPath() + "/" + dbName
;
222 logger
.info("setLocalUrl: " + localUrlString
);
223 setUrl(localUrlString
);
226 public void setMode(String mode
){
230 public String
getMode(){
234 public void setNomenclaturalCode(NomenclaturalCode code
){
235 this.nomenclaturalCode
= code
;