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

package org.jivesoftware.database;

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

import java.io.File;
import java.io.IOException;
20 21 22
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
23 24
import java.sql.DriverManager;
import java.util.Properties;
Matt Tucker's avatar
Matt Tucker committed
25 26 27

/**
 * A connection provider for the embedded hsqlDB database. The database file is stored at
28 29 30
 * <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
31 32 33 34 35
 *
 * @author Matt Tucker
 */
public class EmbeddedConnectionProvider implements ConnectionProvider {

36 37 38 39 40 41 42 43
    private Properties settings;
    private String serverURL;
    private String driver = "org.hsqldb.jdbcDriver";
    private String proxoolURL;

    public EmbeddedConnectionProvider() {
        System.setProperty("org.apache.commons.logging.LogFactory", "org.jivesoftware.util.log.util.CommonsLogFactory");
    }
Matt Tucker's avatar
Matt Tucker committed
44 45 46 47 48 49

    public boolean isPooled() {
        return true;
    }

    public Connection getConnection() throws SQLException {
50 51
        try {
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
52
            return DriverManager.getConnection(proxoolURL, settings);
Matt Tucker's avatar
Matt Tucker committed
53
        }
54
        catch (ClassNotFoundException e) {
55
            throw new SQLException("EmbeddedConnectionProvider: Unable to find driver: "+e);
56
        }
Matt Tucker's avatar
Matt Tucker committed
57 58 59
    }

    public void start() {
60 61 62 63 64
        File databaseDir = new File(JiveGlobals.getHomeDirectory(), File.separator + "embedded-db");
        // If the database doesn't exist, create it.
        if (!databaseDir.exists()) {
            databaseDir.mkdirs();
        }
Matt Tucker's avatar
Matt Tucker committed
65

66 67 68 69 70
        try {
            serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() + File.separator + "openfire";
        }
        catch (IOException ioe) {
            Log.error("EmbeddedConnectionProvider: Error starting connection pool: ", ioe);
Matt Tucker's avatar
Matt Tucker committed
71
        }
72 73 74 75 76 77 78
        proxoolURL = "proxool.openfire:"+driver+":"+serverURL;
        settings = new Properties();
        settings.setProperty("proxool.maximum-connection-count", "25");
        settings.setProperty("proxool.minimum-connection-count", "3");
        settings.setProperty("proxool.maximum-connection-lifetime", Integer.toString((int)(86400000 * 0.5)));
        settings.setProperty("user", "sa");
        settings.setProperty("password", "");
Matt Tucker's avatar
Matt Tucker committed
79 80 81 82 83 84 85 86 87 88
    }

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

    public void destroy() {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        // 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); }
        }
104 105
        // Blank out the settings
        settings = null;
Matt Tucker's avatar
Matt Tucker committed
106 107
    }

Matt Tucker's avatar
Matt Tucker committed
108 109
    public void finalize() throws Throwable {
        super.finalize();
Matt Tucker's avatar
Matt Tucker committed
110 111
        destroy();
    }
112
}