cleanup
[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 package eu.etaxonomy.cdm.database;
10
11 import java.io.File;
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;
18
19 import javax.sql.DataSource;
20
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;
26
27 import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
28
29 /**
30 * @author a.mueller
31 *
32 * IN WORK
33 *
34 */
35 public class LocalH2 extends BasicDataSource {
36 private static final Logger logger = Logger.getLogger(LocalH2.class);
37
38 private final String sep = System.getProperty("file.separator");
39
40 /** url without database name */
41 protected String pureUrl = "jdbc:h2:";
42 /** database name */
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();
55
56 public LocalH2() {
57 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
58 setLocalUrl();
59 }
60
61 /**
62 * @param url
63 * @throws CannotGetJdbcConnectionException
64 */
65 public LocalH2(String url) throws CannotGetJdbcConnectionException {
66 super();
67 this.setUrl(url);
68 setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
69 }
70
71 /* FIXME This is a workaround to solve a problem with dbcp connection pooling.
72 * Remove this when dbcp connection pool gets configured correctly
73 *
74 * (non-Javadoc)
75 * @see org.apache.commons.dbcp.BasicDataSource#createDataSource()
76 */
77 @Override
78 protected synchronized DataSource createDataSource() throws SQLException {
79 super.createDataSource();
80 connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
81 return dataSource;
82 }
83
84 /**
85 * @param url
86 * @param username
87 * @param password
88 * @throws CannotGetJdbcConnectionException
89 */
90 public LocalH2(String url, String username, String password)
91 throws CannotGetJdbcConnectionException {
92 this(url);
93 this.setUsername(username);
94 this.setPassword(password);
95 }
96
97 /**
98 * @param driverClassName
99 * @param url
100 * @param username
101 * @param password
102 * @throws CannotGetJdbcConnectionException
103 */
104 public LocalH2(String driverClassName, String url, String username,
105 String password) throws CannotGetJdbcConnectionException {
106 this(url, username, password);
107 this.setDriverClassName(driverClassName);
108 }
109
110 //** ********************************************************************************/
111
112 public void init(){
113 logger.info("LocalH2init");
114 if (true){ //starting sever is not necessary for H2
115 return;
116 }
117 if (isStartServer){
118 this.startH2Server();
119 }
120 }
121
122 public void destroy(){
123 this.stopH2Server();
124 }
125
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(){
128 try {
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);
134 if (con == null) {
135 logger.warn("Connection to URL " + getUrl() + " could not be established");
136 throw new SQLException();
137 }
138 } catch (SQLException e) {
139 try {
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());
146 h2Server.start();
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.");
151 }
152 }
153 }
154
155
156 /**
157 * stops the Hsqldb Server
158 */
159 private void stopH2Server(){
160 if (h2Server != null){
161 logger.info("stop H2Server");
162 h2Server.stop();
163 }
164 }
165
166 private static final String getDefaultPath(){
167 File path;
168 try {
169 path = CdmApplicationUtils.getWritableResourceDir();
170 } catch (IOException e) {
171 logger.error(e);
172 throw new RuntimeException(e);
173 }
174 String subPath = File.separator + "h2" + File.separator + "LocalH2";
175 return path + subPath;
176 }
177
178 public String getDatabasePath() {
179 return databasePath;
180 }
181
182 public void setDatabasePath(String databasePath) {
183 if (databasePath.endsWith(sep)){
184 databasePath = databasePath + "localCdm";
185 }
186 this.databasePath = databasePath;
187 }
188
189 public boolean isStartServer() {
190 return isStartServer;
191 }
192
193 public void setStartServer(boolean isStartServer) {
194 this.isStartServer = isStartServer;
195 }
196
197 public void setLocalUrl(){
198 String dbName = "cdmLocal";
199 String localUrlString = pureUrl + "file:" + getDefaultPath() + "/" + dbName;
200 logger.info("setLocalUrl: " + localUrlString);
201 setUrl(localUrlString);
202 }
203
204 public void setMode(String mode){
205 this.mode = mode;
206 }
207
208 public String getMode(){
209 return mode;
210 }
211
212 }