2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.persistence;
19 import java.io.PrintWriter;
20 import java.sql.Connection;
21 import java.sql.DriverManager;
22 import java.sql.SQLException;
23 import java.util.Properties;
25 import junit.framework.TestCase;
27 import org.apache.derby.drda.NetworkServerControl;
28 import org.apache.derby.jdbc.ClientDriver;
29 import org.apache.log4j.Logger;
30 import org.wamblee.io.FileSystemUtils;
33 * Derby database setup. The external JDBC url used to connect to a running
37 * jdbc:derby:net://localhost:1527/testdb
40 * and the driver class is
43 * com.ibm.db2.jcc.DB2Driver
46 * The following jars will have to be used <code>db2jcc.jar</code> and
47 * <code>db2jcc_license_c.jar</code>.
49 public class DerbyDatabase implements Database {
54 private static final Logger LOGGER = Logger.getLogger(DerbyDatabase.class);
59 private static final String USERNAME = "sa";
64 private static final String PASSWORD = "123";
67 * Poll interval for the checking the server status.
69 private static final int POLL_INTERVAL = 100;
72 * Maximum time to wait until the server has started or stopped.
74 private static final int MAX_WAIT_TIME = 10000;
77 * Database name to use.
79 private static final String DATABASE_NAME = "testdb";
82 * Path on the file system where derby files are stored.
84 private static final String DATABASE_PATH = "target/db/persistence/derby";
87 * Derby property required to set the file system path
88 * {@link #DATABASE_PATH}.
90 private static final String SYSTEM_PATH_PROPERTY = "derby.system.home";
93 * Constructs derby database class to allow creation of derby database
96 public DerbyDatabase() {
103 * @see org.wamblee.persistence.Database#start()
105 public void start() {
107 // just in case a previous run was killed without the
109 cleanPersistentStorage();
111 // set database path.
112 Properties lProperties = System.getProperties();
113 lProperties.put(SYSTEM_PATH_PROPERTY, DATABASE_PATH);
115 Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
119 waitUntilStartedOrStopped(true);
121 // Force creation of the database.
122 Connection lConnection = createConnection();
124 } catch (Exception e) {
125 throw new RuntimeException("Problem starting database", e);
130 * Waits until the database server has started or stopped.
133 * If true, waits until the server is up, if false, waits until
134 * the server is down.
135 * @throws InterruptedException
137 private void waitUntilStartedOrStopped(boolean aStarted)
138 throws InterruptedException {
141 while (aStarted != isStarted()) {
142 Thread.sleep(POLL_INTERVAL);
143 lWaited += POLL_INTERVAL;
145 if (lWaited > MAX_WAIT_TIME) {
146 throw new RuntimeException(
147 "Derby database did not start within " + MAX_WAIT_TIME
154 * Checks if the database server has started or not.
156 * @return True if started, false otherwise.
158 private boolean isStarted() {
163 } catch (Exception e) {
169 * Gets the controller for the database server.
171 * @return Controller.
174 private NetworkServerControl getControl() throws Exception {
175 return new NetworkServerControl();
182 private void runDatabase() {
184 getControl().start(new PrintWriter(System.out));
185 } catch (Exception e) {
186 throw new RuntimeException(e);
193 * @see org.wamblee.persistence.Database#getJdbcUrl()
195 public String getJdbcUrl() {
196 return getBaseJdbcUrl()
197 + ";create=true;retrieveMessagesFromServerOnGetMessage=true;";
200 private String getBaseJdbcUrl() {
201 return "jdbc:derby:" + DATABASE_NAME;
207 * @see org.wamblee.persistence.Database#getExternalJdbcUrl()
209 public String getExternalJdbcUrl() {
210 return "jdbc:derby:net://localhost:1527/" + DATABASE_NAME;
214 * Shuts down the derby database and cleans up all created files.
217 private void shutdownDerby() {
219 DriverManager.getConnection("jdbc:derby:;shutdown=true");
220 throw new RuntimeException("Derby did not shutdown, "
221 + " should always throw exception at shutdown");
222 } catch (Exception e) {
223 LOGGER.info("Derby has been shut down.");
230 * @see org.wamblee.persistence.Database#getDriverClassName()
232 public String getDriverClassName() {
233 return ClientDriver.class.getName();
237 * Gets the user name.
239 public String getUsername() {
248 public String getPassword() {
255 * @see org.wamblee.persistence.Database#createConnection()
257 public Connection createConnection() throws SQLException {
258 Connection c = DriverManager.getConnection(getJdbcUrl(), getUsername(),
265 * Stops the derby database and cleans up all derby files.
269 // shutdown network server.
270 getControl().shutdown();
271 waitUntilStartedOrStopped(false);
273 // shutdown inmemory access.
275 cleanPersistentStorage();
276 } catch (Exception e) {
277 LOGGER.warn("Problem stopping database", e);
282 * Cleans up persistent storage of Derby.
284 private void cleanPersistentStorage() {
285 File lFile = new File(DATABASE_PATH);
287 if (lFile.isFile()) {
288 TestCase.fail("A regular file by the name " + DATABASE_PATH
289 + " exists, clean this up first");
292 if (!lFile.isDirectory()) {
293 return; // no-op already cleanup up.
296 FileSystemUtils.deleteDirRecursively(DATABASE_PATH);