Commit c486d644 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Automatically create embedded database if needed and populate it with data.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@143 b35dd754-fafc-0310-a699-88a17e54d16e
parent 4fd70fa9
......@@ -13,13 +13,14 @@ package org.jivesoftware.database;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.database.ConnectionPool;
import org.jivesoftware.database.ConnectionProvider;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
/**
* A connection provider for the embedded hsqlDB database. The database file is stored at
......@@ -35,7 +36,6 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
private Object initLock = new Object();
public boolean isPooled() {
return true;
}
......@@ -60,18 +60,29 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
synchronized (initLock) {
try {
String driver = "org.hsqldb.jdbcDriver";
File messengerHome = new File(JiveGlobals.getMessengerHome(), File.separator + "database" + File.separator + "messenger");
String path = messengerHome.getCanonicalPath();
File databaseDir = new File(JiveGlobals.getMessengerHome(), File.separator +
"embedded-db");
boolean initData = false;
// If the database doesn't exist, create it.
if (!databaseDir.exists()) {
databaseDir.mkdirs();
initData = true;
}
String serverURL = "jdbc:hsqldb:" + path;
String serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() +
File.separator + "messenger";
String username = "sa";
String password = "jiverocks";
String password = "";
int minConnections = 3;
int maxConnections = 10;
double connectionTimeout = 0.5;
connectionPool = new ConnectionPool(driver, serverURL, username, password,
minConnections, maxConnections, connectionTimeout, false);
// Create initial tables if they don't already exist.
if (initData) {
initializeDatabase();
}
}
catch (IOException ioe) {
Log.error("Error starting connection pool.", ioe);
......@@ -102,4 +113,61 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
public void finalize() {
destroy();
}
private void initializeDatabase() {
BufferedReader in = null;
Connection con = null;
try {
in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("/database/messenger_hsqldb.sql")));
con = connectionPool.getConnection();
boolean done = false;
while (!done) {
StringBuffer command = new StringBuffer();
while (true) {
String line = in.readLine();
if (line == null) {
done = true;
break;
}
// Ignore comments and blank lines.
if (isCommandPart(line)) {
command.append(line);
}
if (line.endsWith(";")) {
break;
}
}
// Send command to database.
Statement stmt = con.createStatement();
stmt.execute(command.toString());
stmt.close();
}
}
catch (Exception e) {
Log.error(e);
e.printStackTrace();
}
finally {
if (in != null) {
try { in.close(); }
catch (Exception e) { }
}
if (con != null) {
try { con.close(); }
catch (Exception e) { }
}
}
}
private static boolean isCommandPart(String line) {
line = line.trim();
if (line.equals("")) {
return false;
}
if (line.startsWith("//")) {
return false;
}
return true;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment