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
.log4j
.Logger
;
21 import org
.h2
.tools
.Server
;
22 import org
.springframework
.jdbc
.CannotGetJdbcConnectionException
;
23 import org
.springframework
.jdbc
.datasource
.DriverManagerDataSource
;
25 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationUtils
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
36 public class LocalH2
extends DriverManagerDataSource
{
37 private static final Logger logger
= Logger
.getLogger(LocalH2
.class);
39 private String sep
= System
.getProperty("file.separator");
41 /** url without database name */
42 protected String pureUrl
= "jdbc:h2:";
44 protected String dbName
= "cdm";
45 /** path, where database should be stored in the file system */
46 protected String databasePath
= getDefaultPath();
47 /** Server instance */
48 protected Server h2Server
;
49 /** if true starts server on init() */
50 protected boolean isStartServer
= true;
51 /** makes the Server silent (no messages) */
52 protected boolean isSilent
= true;
53 /** default driver class name */
54 protected String DEFAULT_DRIVER_CLASS_NAME
= "org.h2.Driver";
55 String mode
= H2Mode
.EMBEDDED
.toString();
57 private NomenclaturalCode nomenclaturalCode
;
63 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
69 * @throws CannotGetJdbcConnectionException
71 public LocalH2(String url
) throws CannotGetJdbcConnectionException
{
73 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
80 * @throws CannotGetJdbcConnectionException
82 public LocalH2(String url
, String username
, String password
)
83 throws CannotGetJdbcConnectionException
{
84 super(url
, username
, password
);
85 this.setDriverClassName(DEFAULT_DRIVER_CLASS_NAME
);
89 * @param driverClassName
93 * @throws CannotGetJdbcConnectionException
95 public LocalH2(String driverClassName
, String url
, String username
,
96 String password
) throws CannotGetJdbcConnectionException
{
97 super(driverClassName
, url
, username
, password
);
100 //** ********************************************************************************/
103 logger
.info("LocalH2init");
104 if (true){ //starting sever is not necessary for H2
108 this.startH2Server();
112 public void destroy(){
117 //checks if h2-server is started, if not it will be started (taken over from hsqldb, maybe not necessary for H2
118 private void startH2Server(){
120 Driver driver
= DriverManager
.getDriver(getUrl());
121 Properties prop
= new Properties();
122 prop
.setProperty("user", this.getUsername());
123 prop
.setProperty("password", this.getPassword());
124 Connection con
= driver
.connect(getUrl(), prop
);
126 logger
.warn("Connection to URL " + getUrl() + " could not be established");
127 throw new SQLException();
129 } catch (SQLException e
) {
131 //server is probably not runing on the url (or login is wrong !!)
132 logger
.info("Start H2Server");
133 String
[] args
= new String
[] { "-trace" };
134 h2Server
= Server
.createTcpServer(args
).start();
135 // h2Server.setDatabaseName(0, getDbName());
136 // h2Server.setDatabasePath(0, getDatabasePath());
138 } catch (SQLException sqle1
) {
139 logger
.error("SQL Exception when starting Local H2Server: "+ sqle1
);
140 } catch (RuntimeException e1
) {
141 logger
.error("Local H2Server could not be started or connection to existing server could not be established.");
148 * stops the Hsqldb Server
150 private void stopH2Server(){
151 if (h2Server
!= null){
152 logger
.info("stop H2Server");
157 private static final String
getDefaultPath(){
160 path
= CdmApplicationUtils
.getWritableResourceDir();
161 } catch (IOException e
) {
163 throw new RuntimeException(e
);
165 String subPath
= File
.separator
+ "h2" + File
.separator
+ "LocalH2";
166 return path
+ subPath
;
172 public String
getDatabasePath() {
177 * @param dbPath the dbPath to set
179 public void setDatabasePath(String databasePath
) {
180 if (databasePath
.endsWith(sep
)){
181 databasePath
= databasePath
+ "localCdm";
183 this.databasePath
= databasePath
;
187 * @return the isStartServer
189 public boolean isStartServer() {
190 return isStartServer
;
194 * @param isStartServer the isStartServer to set
196 public void setStartServer(boolean isStartServer
) {
197 this.isStartServer
= isStartServer
;
200 public void setLocalUrl(){
201 String dbName
= "cdmLocal";
202 String localUrlString
= pureUrl
+ "file:" + getDefaultPath() + "/" + dbName
;
203 logger
.info("setLocalUrl: " + localUrlString
);
204 setUrl(localUrlString
);
207 public void setMode(String mode
){
211 public String
getMode(){
215 public void setNomenclaturalCode(NomenclaturalCode code
){
216 this.nomenclaturalCode
= code
;