Project

General

Profile

Download (5.8 KB) Statistics
| Branch: | Tag: | Revision:
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 javax.sql.DataSource;
21

    
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;
27

    
28
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
29

    
30

    
31
/**
32
 * @author a.mueller
33
 *
34
 * IN WORK
35
 *
36
 */
37

    
38
public class LocalH2 extends BasicDataSource {
39
	private static final Logger logger = Logger.getLogger(LocalH2.class);
40

    
41
	private final String sep = System.getProperty("file.separator");
42

    
43
	/** url without database name */
44
	protected String pureUrl = "jdbc:h2:";
45
	/** database name */
46
	protected String dbName = "cdm";
47
	/** path, where database should be stored in the file system */
48
	protected String databasePath = getDefaultPath();
49
	/** Server instance */
50
	protected Server h2Server;
51
	/** if true starts server on init() */
52
	protected boolean isStartServer = true;
53
	/** makes the Server silent (no messages) */
54
	protected boolean isSilent = true;
55
	/** default driver class name */
56
	protected String DEFAULT_DRIVER_CLASS_NAME = "org.h2.Driver";
57
	String mode = H2Mode.EMBEDDED.toString();
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();
73
		this.setUrl(url);
74
		setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);
75
	}
76

    
77
	/* FIXME This is a workaround to solve a problem with dbcp connection pooling.
78
	 * Remove this when dbcp connection pool gets configured correctly
79
	 *
80
	 * (non-Javadoc)
81
	 * @see org.apache.commons.dbcp.BasicDataSource#createDataSource()
82
	 */
83
	@Override
84
	protected synchronized DataSource createDataSource() throws SQLException {
85
		super.createDataSource();
86
		connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
87
		return dataSource;
88
	}
89

    
90
	/**
91
	 * @param url
92
	 * @param username
93
	 * @param password
94
	 * @throws CannotGetJdbcConnectionException
95
	 */
96
	public LocalH2(String url, String username, String password)
97
			throws CannotGetJdbcConnectionException {
98
		this(url);
99
		this.setUsername(username);
100
		this.setPassword(password);
101
	}
102

    
103
	/**
104
	 * @param driverClassName
105
	 * @param url
106
	 * @param username
107
	 * @param password
108
	 * @throws CannotGetJdbcConnectionException
109
	 */
110
	public LocalH2(String driverClassName, String url, String username,
111
			String password) throws CannotGetJdbcConnectionException {
112
		this(url, username, password);
113
		this.setDriverClassName(driverClassName);
114
	}
115

    
116
//** ********************************************************************************/
117

    
118
	public void init(){
119
		logger.info("LocalH2init");
120
		if (true){   //starting sever is not necessary for H2
121
			return;
122
		}
123
		if (isStartServer){
124
			this.startH2Server();
125
		}
126
	}
127

    
128
	public void destroy(){
129
		this.stopH2Server();
130
	}
131

    
132

    
133
	//checks if h2-server is started, if not it will be started	(taken over from hsqldb, maybe not necessary for H2
134
	private void startH2Server(){
135
		try {
136
			Driver driver = DriverManager.getDriver(getUrl());
137
			Properties prop = new Properties();
138
			prop.setProperty("user", this.getUsername());
139
			prop.setProperty("password", this.getPassword());
140
			Connection con = driver.connect(getUrl(),  prop);
141
			if (con == null) {
142
				logger.warn("Connection to URL " +  getUrl() +  " could not be established");
143
				throw new SQLException();
144
			}
145
		} catch (SQLException e) {
146
			try {
147
				//server is probably not running on the url (or login is wrong !!)
148
				logger.info("Start H2Server");
149
				String[] args = new String[] { "-trace" };
150
				h2Server = Server.createTcpServer(args).start();
151
//				h2Server.setDatabaseName(0, getDbName());
152
//				h2Server.setDatabasePath(0,  getDatabasePath());
153
				h2Server.start();
154
			} catch (SQLException sqle1) {
155
				logger.error("SQL Exception when starting Local H2Server: "+ sqle1);
156
			} catch (RuntimeException e1) {
157
				logger.error("Local H2Server could not be started or connection to existing server could not be established.");
158
			}
159
		}
160
	}
161

    
162

    
163
	/**
164
	 * stops the Hsqldb Server
165
	 */
166
	private void stopH2Server(){
167
		if (h2Server != null){
168
			logger.info("stop H2Server");
169
			h2Server.stop();
170
		}
171
	}
172

    
173
	private static final String getDefaultPath(){
174
		File path;
175
		try {
176
			path = CdmApplicationUtils.getWritableResourceDir();
177
		} catch (IOException e) {
178
			logger.error(e);
179
			throw new RuntimeException(e);
180
		}
181
		String subPath = File.separator + "h2" + File.separator + "LocalH2";
182
		return  path + subPath;
183
	}
184

    
185
	/**
186
	 * @return the dbPath
187
	 */
188
	public String getDatabasePath() {
189
		return databasePath;
190
	}
191

    
192
	/**
193
	 * @param dbPath the dbPath to set
194
	 */
195
	public void setDatabasePath(String databasePath) {
196
		if (databasePath.endsWith(sep)){
197
			databasePath = databasePath + "localCdm";
198
		}
199
		this.databasePath = databasePath;
200
	}
201

    
202
	/**
203
	 * @return the isStartServer
204
	 */
205
	public boolean isStartServer() {
206
		return isStartServer;
207
	}
208

    
209
	/**
210
	 * @param isStartServer the isStartServer to set
211
	 */
212
	public void setStartServer(boolean isStartServer) {
213
		this.isStartServer = isStartServer;
214
	}
215

    
216
	public void setLocalUrl(){
217
		String dbName = "cdmLocal";
218
		String localUrlString = pureUrl + "file:" + getDefaultPath() + "/" + dbName;
219
		logger.info("setLocalUrl: " + localUrlString);
220
		setUrl(localUrlString);
221
	}
222

    
223
	public void setMode(String mode){
224
		this.mode = mode;
225
	}
226

    
227
	public String getMode(){
228
		return mode;
229
	}
230

    
231
}
(14-14/20)