moved taxonNode specific code to TaxonTreeService. Cleaned up the datasource classes
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / LocalH2.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.database;
11
12 import java.io.File;
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;
19
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;
24
25 import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
26 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
27
28
29 /**
30 * @author a.mueller
31 *
32 * IN WORK
33 *
34 */
35
36 public class LocalH2 extends DriverManagerDataSource {
37 private static final Logger logger = Logger.getLogger(LocalH2.class);
38
39 private String sep = System.getProperty("file.separator");
40
41 /** url without database name */
42 protected String pureUrl = "jdbc:h2:";
43 /** database name */
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();
56
57 private NomenclaturalCode nomenclaturalCode;
58
59 /**
60 *
61 */
62 public LocalH2() {
63 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
64 setLocalUrl();
65 }
66
67 /**
68 * @param url
69 * @throws CannotGetJdbcConnectionException
70 */
71 public LocalH2(String url) throws CannotGetJdbcConnectionException {
72 super(url);
73 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
74 }
75
76 /**
77 * @param url
78 * @param username
79 * @param password
80 * @throws CannotGetJdbcConnectionException
81 */
82 public LocalH2(String url, String username, String password)
83 throws CannotGetJdbcConnectionException {
84 super(url, username, password);
85 this.setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
86 }
87
88 /**
89 * @param driverClassName
90 * @param url
91 * @param username
92 * @param password
93 * @throws CannotGetJdbcConnectionException
94 */
95 public LocalH2(String driverClassName, String url, String username,
96 String password) throws CannotGetJdbcConnectionException {
97 super(driverClassName, url, username, password);
98 }
99
100 //** ********************************************************************************/
101
102 public void init(){
103 logger.info("LocalH2init");
104 if (true){ //starting sever is not necessary for H2
105 return;
106 }
107 if (isStartServer){
108 this.startH2Server();
109 }
110 }
111
112 public void destroy(){
113 this.stopH2Server();
114 }
115
116
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(){
119 try {
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);
125 if (con == null) {
126 logger.warn("Connection to URL " + getUrl() + " could not be established");
127 throw new SQLException();
128 }
129 } catch (SQLException e) {
130 try {
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());
137 h2Server.start();
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.");
142 }
143 }
144 }
145
146
147 /**
148 * stops the Hsqldb Server
149 */
150 private void stopH2Server(){
151 if (h2Server != null){
152 logger.info("stop H2Server");
153 h2Server.stop();
154 }
155 }
156
157 private static final String getDefaultPath(){
158 File path;
159 try {
160 path = CdmApplicationUtils.getWritableResourceDir();
161 } catch (IOException e) {
162 logger.error(e);
163 throw new RuntimeException(e);
164 }
165 String subPath = File.separator + "h2" + File.separator + "LocalH2";
166 return path + subPath;
167 }
168
169 /**
170 * @return the dbPath
171 */
172 public String getDatabasePath() {
173 return databasePath;
174 }
175
176 /**
177 * @param dbPath the dbPath to set
178 */
179 public void setDatabasePath(String databasePath) {
180 if (databasePath.endsWith(sep)){
181 databasePath = databasePath + "localCdm";
182 }
183 this.databasePath = databasePath;
184 }
185
186 /**
187 * @return the isStartServer
188 */
189 public boolean isStartServer() {
190 return isStartServer;
191 }
192
193 /**
194 * @param isStartServer the isStartServer to set
195 */
196 public void setStartServer(boolean isStartServer) {
197 this.isStartServer = isStartServer;
198 }
199
200 public void setLocalUrl(){
201 String dbName = "cdmLocal";
202 String localUrlString = pureUrl + "file:" + getDefaultPath() + "/" + dbName;
203 logger.info("setLocalUrl: " + localUrlString);
204 setUrl(localUrlString);
205 }
206
207 public void setMode(String mode){
208 this.mode = mode;
209 }
210
211 public String getMode(){
212 return mode;
213 }
214
215 public void setNomenclaturalCode(NomenclaturalCode code){
216 this.nomenclaturalCode = code;
217 }
218
219
220 }