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
.sql
.Connection
;
14 import java
.sql
.Driver
;
15 import java
.sql
.DriverManager
;
16 import java
.sql
.SQLException
;
17 import java
.util
.Properties
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.h2
.tools
.Server
;
21 import org
.springframework
.jdbc
.CannotGetJdbcConnectionException
;
22 import org
.springframework
.jdbc
.datasource
.DriverManagerDataSource
;
24 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationUtils
;
34 public class LocalH2
extends DriverManagerDataSource
{
35 private static final Logger logger
= Logger
.getLogger(LocalH2
.class);
37 private String sep
= System
.getProperty("file.separator");
39 /** url without database name */
40 protected String pureUrl
= "jdbc:h2:";
42 protected String dbName
= "cdm";
43 /** path, where database should be stored in the file system */
44 protected String databasePath
= getDefaultPath();
45 /** Server instance */
46 protected Server h2Server
;
47 /** if true starts server on init() */
48 protected boolean isStartServer
= true;
49 /** makes the Server silent (no messages) */
50 protected boolean isSilent
= true;
51 /** default driver class name */
52 protected String DEFAULT_DRIVER_CLASS_NAME
= "org.h2.Driver";
53 String mode
= H2Mode
.EMBEDDED
.toString();
59 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
65 * @throws CannotGetJdbcConnectionException
67 public LocalH2(String url
) throws CannotGetJdbcConnectionException
{
69 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
76 * @throws CannotGetJdbcConnectionException
78 public LocalH2(String url
, String username
, String password
)
79 throws CannotGetJdbcConnectionException
{
80 super(url
, username
, password
);
81 this.setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
85 * @param driverClassName
89 * @throws CannotGetJdbcConnectionException
91 public LocalH2(String driverClassName
, String url
, String username
,
92 String password
) throws CannotGetJdbcConnectionException
{
93 super(driverClassName
, url
, username
, password
);
96 //** ********************************************************************************/
99 logger
.warn("LocalH2init");
100 if (true){ //starting sever is not necessary for H2
104 this.startH2Server();
108 public void destroy(){
113 //checks if h2-server is started, if not it will be started (taken over from hsqldb, maybe not necessary for H2
114 private void startH2Server(){
116 Driver driver
= DriverManager
.getDriver(getUrl());
117 Properties prop
= new Properties();
118 prop
.setProperty("user", this.getUsername());
119 prop
.setProperty("password", this.getPassword());
120 Connection con
= driver
.connect(getUrl(), prop
);
122 logger
.warn("Connection to URL " + getUrl() + " could not be established");
123 throw new SQLException();
125 } catch (SQLException e
) {
127 //server is probably not runing on the url (or login is wrong !!)
128 logger
.info("Start H2Server");
129 String
[] args
= new String
[] { "-trace" };
130 h2Server
= Server
.createTcpServer(args
).start();
131 if (logger
.isDebugEnabled()){
132 for (int i
= 0; i
< 10; i
++){
133 // logger.info("DatabaseName " + i + ": " + h2Server.getDatabaseName(i, true));
134 // logger.info("DatabaseName " + i + ": " + h2Server.getDatabaseName(i, false));
135 // logger.info("DatabasePath " + i + ": " + h2Server.getDatabasePath(i, true));
136 // logger.info("DatabasePath " + i + ": " + h2Server.getDatabasePath(i, false));
137 // logger.info("DatabaseType " + i + ": " + h2Server.getDatabaseType(i));
140 // h2Server.setDatabaseName(0, getDbName());
141 // h2Server.setDatabasePath(0, getDatabasePath());
143 // h2Server.checkRunning(true);
144 } catch (SQLException sqle1
) {
145 logger
.error("SQL Exception when starting Local H2Server: "+ sqle1
);
146 } catch (RuntimeException e1
) {
147 logger
.error("Local H2Server could not be started or connection to existing server could not be established.");
154 * stops the Hsqldb Server
156 private void stopH2Server(){
157 if (h2Server
!= null){
158 logger
.info("stop H2Server");
163 private static final String
getDefaultPath(){
164 //String path = System.getProperty("user.dir");
165 File path
= CdmApplicationUtils
.getWritableResourceDir();
166 String subPath
= File
.separator
+ "h2" + File
.separator
+ "LocalH2";
167 return path
+ subPath
;
173 public String
getDatabasePath() {
178 * @param dbPath the dbPath to set
180 public void setDatabasePath(String databasePath
) {
181 if (databasePath
.endsWith(sep
)){
182 databasePath
= databasePath
+ "localCdm";
184 this.databasePath
= databasePath
;
188 * @return the isStartServer
190 public boolean isStartServer() {
191 return isStartServer
;
195 * @param isStartServer the isStartServer to set
197 public void setStartServer(boolean isStartServer
) {
198 this.isStartServer
= isStartServer
;
201 public void setLocalUrl(){
202 logger
.warn("setLocalUrl");
203 String dbName
= "cdmLocal";
204 setUrl(pureUrl
+ "file:" + getDefaultPath() + "/" + dbName
);
207 public void setMode(String mode
){
211 public String
getMode(){