EmbeddedConnectionProvider.java 5.51 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.database;

14
import org.jivesoftware.util.JiveGlobals;
Matt Tucker's avatar
Matt Tucker committed
15 16
import org.jivesoftware.util.Log;

17
import java.io.BufferedReader;
Matt Tucker's avatar
Matt Tucker committed
18 19
import java.io.File;
import java.io.IOException;
20
import java.io.InputStreamReader;
21 22 23
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
Matt Tucker's avatar
Matt Tucker committed
24 25 26

/**
 * A connection provider for the embedded hsqlDB database. The database file is stored at
27 28 29
 * <tt>home/database</tt>. The log file for this connection provider is stored at
 * <tt>[home]/logs/EmbeddedConnectionProvider.log</tt>, so you should ensure
 * that the <tt>[home]/logs</tt> directory exists.
Matt Tucker's avatar
Matt Tucker committed
30 31 32 33 34
 *
 * @author Matt Tucker
 */
public class EmbeddedConnectionProvider implements ConnectionProvider {

35
    private ConnectionPool connectionPool = null;
Matt Tucker's avatar
Matt Tucker committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    private Object initLock = new Object();

    public boolean isPooled() {
        return true;
    }

    public Connection getConnection() throws SQLException {
        if (connectionPool == null) {
            // Block until the init has been done
            synchronized (initLock) {
                // If still null, something has gone wrong
                if (connectionPool == null) {
                    Log.error("Error: EmbeddedConnectionProvider.getConnection() was" +
                            "called before the internal pool has been initialized.");
                    return null;
                }
            }
        }
        return connectionPool.getConnection();
    }

    public void start() {
        // Acquire lock so that no connections can be returned while creating the pool.
        synchronized (initLock) {
            try {
                String driver = "org.hsqldb.jdbcDriver";
62
                File databaseDir = new File(JiveGlobals.getHomeDirectory(), File.separator +
63 64 65 66 67 68 69
                        "embedded-db");
                boolean initData = false;
                // If the database doesn't exist, create it.
                if (!databaseDir.exists()) {
                    databaseDir.mkdirs();
                    initData = true;
                }
Matt Tucker's avatar
Matt Tucker committed
70

71
                String serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() +
72
                        File.separator + "wildfire";
Matt Tucker's avatar
Matt Tucker committed
73
                String username = "sa";
74
                String password = "";
Matt Tucker's avatar
Matt Tucker committed
75
                int minConnections = 3;
76
                int maxConnections = 25;
Matt Tucker's avatar
Matt Tucker committed
77 78 79 80
                double connectionTimeout = 0.5;

                connectionPool = new ConnectionPool(driver, serverURL, username, password,
                        minConnections, maxConnections, connectionTimeout, false);
81 82 83 84
                // Create initial tables if they don't already exist.
                if (initData) {
                    initializeDatabase();
                }
Matt Tucker's avatar
Matt Tucker committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            }
            catch (IOException ioe) {
                Log.error("Error starting connection pool.", ioe);
            }
        }
    }

    public void restart() {
        // Kill off pool.
        destroy();
        // Start a new pool.
        start();
    }

    public void destroy() {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        if (connectionPool == null) {
            return;
        }
        // Shutdown the database.
        Connection con = null;
        try {
            con = getConnection();
            Statement stmt = con.createStatement();
            stmt.execute("SHUTDOWN");
            stmt.close();
        }
        catch (SQLException sqle) {
            Log.error(sqle);
        }
        finally {
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
        // Close the connection pool.
        try {
            connectionPool.destroy();
        }
        catch (Exception e) {
            Log.error(e);
Matt Tucker's avatar
Matt Tucker committed
124 125 126 127 128 129 130 131
        }
        // Release reference to connectionPool
        connectionPool = null;
    }

    public void finalize() {
        destroy();
    }
132 133 134 135 136 137

    private void initializeDatabase() {
        BufferedReader in = null;
        Connection con = null;
        try {
            in = new BufferedReader(new InputStreamReader(
138
                    getClass().getResourceAsStream("/database/wildfire_hsqldb.sql")));
139 140 141
            con = connectionPool.getConnection();
            boolean done = false;
            while (!done) {
142
                StringBuilder command = new StringBuilder();
143 144 145 146 147 148 149
                while (true) {
                    String line = in.readLine();
                    if (line == null) {
                        done = true;
                        break;
                    }
                    // Ignore comments and blank lines.
150
                    if (DbConnectionManager.isSQLCommandPart(line)) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
                        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) { }
            }
172 173 174
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
175
    }
176
}