Project

General

Profile

Download (5.79 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
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

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

    
156

    
157
	/**
158
	 * stops the Hsqldb Server
159
	 */
160
	private void stopH2Server(){
161
		if (h2Server != null){
162
			logger.info("stop H2Server");
163
			h2Server.stop();
164
		}
165
	}
166

    
167
	private static final String getDefaultPath(){
168
		File path;
169
		try {
170
			path = CdmApplicationUtils.getWritableResourceDir();
171
		} catch (IOException e) {
172
			logger.error(e);
173
			throw new RuntimeException(e);
174
		}
175
		String subPath = File.separator + "h2" + File.separator + "LocalH2";
176
		return  path + subPath;
177
	}
178

    
179
	/**
180
	 * @return the dbPath
181
	 */
182
	public String getDatabasePath() {
183
		return databasePath;
184
	}
185

    
186
	/**
187
	 * @param dbPath the dbPath to set
188
	 */
189
	public void setDatabasePath(String databasePath) {
190
		if (databasePath.endsWith(sep)){
191
			databasePath = databasePath + "localCdm";
192
		}
193
		this.databasePath = databasePath;
194
	}
195

    
196
	/**
197
	 * @return the isStartServer
198
	 */
199
	public boolean isStartServer() {
200
		return isStartServer;
201
	}
202

    
203
	/**
204
	 * @param isStartServer the isStartServer to set
205
	 */
206
	public void setStartServer(boolean isStartServer) {
207
		this.isStartServer = isStartServer;
208
	}
209

    
210
	public void setLocalUrl(){
211
		String dbName = "cdmLocal";
212
		String localUrlString = pureUrl + "file:" + getDefaultPath() + "/" + dbName;
213
		logger.info("setLocalUrl: " + localUrlString);
214
		setUrl(localUrlString);
215
	}
216

    
217
	public void setMode(String mode){
218
		this.mode = mode;
219
	}
220

    
221
	public String getMode(){
222
		return mode;
223
	}
224

    
225
}
(15-15/21)