EmbeddedConnectionProvider.java 3.86 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 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Matt Tucker's avatar
Matt Tucker committed
19 20 21 22
 */

package org.jivesoftware.database;

23
import org.jivesoftware.util.JiveGlobals;
24 25
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Matt Tucker's avatar
Matt Tucker committed
26 27 28

import java.io.File;
import java.io.IOException;
29
import java.sql.Connection;
30
import java.sql.PreparedStatement;
31
import java.sql.SQLException;
32 33
import java.sql.DriverManager;
import java.util.Properties;
Matt Tucker's avatar
Matt Tucker committed
34 35 36

/**
 * A connection provider for the embedded hsqlDB database. The database file is stored at
37 38 39
 * <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
40 41 42 43 44
 *
 * @author Matt Tucker
 */
public class EmbeddedConnectionProvider implements ConnectionProvider {

45 46
    private static final Logger Log = LoggerFactory.getLogger(EmbeddedConnectionProvider.class);

47 48 49 50 51 52 53
    private Properties settings;
    private String serverURL;
    private String driver = "org.hsqldb.jdbcDriver";
    private String proxoolURL;

    public EmbeddedConnectionProvider() {
    }
Matt Tucker's avatar
Matt Tucker committed
54

55
    @Override
Matt Tucker's avatar
Matt Tucker committed
56 57 58 59
    public boolean isPooled() {
        return true;
    }

60
    @Override
Matt Tucker's avatar
Matt Tucker committed
61
    public Connection getConnection() throws SQLException {
62 63
        try {
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
64
            return DriverManager.getConnection(proxoolURL, settings);
Matt Tucker's avatar
Matt Tucker committed
65
        }
66
        catch (ClassNotFoundException e) {
67
            throw new SQLException("EmbeddedConnectionProvider: Unable to find driver: "+e);
68
        }
Matt Tucker's avatar
Matt Tucker committed
69 70
    }

71
    @Override
Matt Tucker's avatar
Matt Tucker committed
72
    public void start() {
73 74 75 76 77
        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
78

79 80 81 82 83
        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
84
        }
85 86 87 88 89 90 91
        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
92 93
    }

94
    @Override
Matt Tucker's avatar
Matt Tucker committed
95 96 97 98 99 100 101
    public void restart() {
        // Kill off pool.
        destroy();
        // Start a new pool.
        start();
    }

102
    @Override
Matt Tucker's avatar
Matt Tucker committed
103
    public void destroy() {
104 105
        // Shutdown the database.
        Connection con = null;
106
        PreparedStatement pstmt = null;
107 108
        try {
            con = getConnection();
109 110
            pstmt = con.prepareStatement("SHUTDOWN");
            pstmt.execute();
111 112
        }
        catch (SQLException sqle) {
113
            Log.error(sqle.getMessage(), sqle);
114 115
        }
        finally {
116
            DbConnectionManager.closeConnection(pstmt, con);
117
        }
118 119
        // Blank out the settings
        settings = null;
Matt Tucker's avatar
Matt Tucker committed
120 121
    }

122 123
    @Override
	public void finalize() throws Throwable {
Matt Tucker's avatar
Matt Tucker committed
124
        super.finalize();
Matt Tucker's avatar
Matt Tucker committed
125 126
        destroy();
    }
127
}