001: /**
002: * $RCSfile$
003: * $Revision: 9758 $
004: * $Date: 2008-01-12 17:54:08 -0800 (Sat, 12 Jan 2008) $
005: *
006: * Copyright (C) 2004 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.database;
011:
012: import org.jivesoftware.util.JiveGlobals;
013: import org.jivesoftware.util.Log;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.sql.Connection;
018: import java.sql.SQLException;
019: import java.sql.Statement;
020: import java.sql.DriverManager;
021: import java.util.Properties;
022:
023: /**
024: * A connection provider for the embedded hsqlDB database. The database file is stored at
025: * <tt>home/database</tt>. The log file for this connection provider is stored at
026: * <tt>[home]/logs/EmbeddedConnectionProvider.log</tt>, so you should ensure
027: * that the <tt>[home]/logs</tt> directory exists.
028: *
029: * @author Matt Tucker
030: */
031: public class EmbeddedConnectionProvider implements ConnectionProvider {
032:
033: private Properties settings;
034: private String serverURL;
035: private String driver = "org.hsqldb.jdbcDriver";
036: private String proxoolURL;
037:
038: public EmbeddedConnectionProvider() {
039: System.setProperty("org.apache.commons.logging.LogFactory",
040: "org.jivesoftware.util.log.util.CommonsLogFactory");
041: }
042:
043: public boolean isPooled() {
044: return true;
045: }
046:
047: public Connection getConnection() throws SQLException {
048: Connection connection = null;
049: try {
050: Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
051: try {
052: connection = DriverManager.getConnection(proxoolURL,
053: settings);
054: } catch (SQLException e) {
055: Log
056: .error(
057: "EmbeddedConnectionProvider: Error while getting connection: ",
058: e);
059: }
060: } catch (ClassNotFoundException e) {
061: Log
062: .error(
063: "EmbeddedConnectionProvider: Unable to find driver: ",
064: e);
065: }
066: return connection;
067: }
068:
069: public void start() {
070: File databaseDir = new File(JiveGlobals.getHomeDirectory(),
071: File.separator + "embedded-db");
072: // If the database doesn't exist, create it.
073: if (!databaseDir.exists()) {
074: databaseDir.mkdirs();
075: }
076:
077: try {
078: serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath()
079: + File.separator + "openfire";
080: } catch (IOException ioe) {
081: Log
082: .error(
083: "EmbeddedConnectionProvider: Error starting connection pool: ",
084: ioe);
085: }
086: proxoolURL = "proxool.openfire:" + driver + ":" + serverURL;
087: settings = new Properties();
088: settings.setProperty("proxool.maximum-connection-count", "25");
089: settings.setProperty("proxool.minimum-connection-count", "3");
090: settings.setProperty("proxool.maximum-connection-lifetime",
091: Integer.toString((int) (86400000 * 0.5)));
092: settings.setProperty("user", "sa");
093: settings.setProperty("password", "");
094: }
095:
096: public void restart() {
097: // Kill off pool.
098: destroy();
099: // Start a new pool.
100: start();
101: }
102:
103: public void destroy() {
104: // Shutdown the database.
105: Connection con = null;
106: try {
107: con = getConnection();
108: Statement stmt = con.createStatement();
109: stmt.execute("SHUTDOWN");
110: stmt.close();
111: } catch (SQLException sqle) {
112: Log.error(sqle);
113: } finally {
114: try {
115: if (con != null) {
116: con.close();
117: }
118: } catch (Exception e) {
119: Log.error(e);
120: }
121: }
122: // Blank out the settings
123: settings = null;
124: }
125:
126: public void finalize() throws Throwable {
127: super.finalize();
128: destroy();
129: }
130: }
|