001: package org.methodize.nntprss.db;
002:
003: /* -----------------------------------------------------------
004: * nntp//rss - a bridge between the RSS world and NNTP clients
005: * Copyright (c) 2002, 2003 Jason Brome. All Rights Reserved.
006: *
007: * email: nntprss@methodize.org
008: * mail: Methodize Solutions
009: * PO Box 3865
010: * Grand Central Station
011: * New York NY 10163
012: *
013: * This file is part of nntp//rss
014: *
015: * nntp//rss is free software; you can redistribute it
016: * and/or modify it under the terms of the GNU General
017: * Public License as published by the Free Software Foundation;
018: * either version 2 of the License, or (at your option) any
019: * later version.
020: *
021: * This program is distributed in the hope that it will be
022: * useful, but WITHOUT ANY WARRANTY; without even the implied
023: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
024: * PURPOSE. See the GNU General Public License for more
025: * details.
026: *
027: * You should have received a copy of the GNU General Public
028: * License along with this program; if not, write to the
029: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
030: * Boston, MA 02111-1307 USA
031: * ----------------------------------------------------- */
032:
033: import java.sql.Connection;
034: import java.sql.DriverManager;
035: import java.sql.SQLException;
036: import java.sql.Statement;
037:
038: import org.apache.commons.dbcp.ConnectionFactory;
039: import org.apache.commons.dbcp.DriverManagerConnectionFactory;
040: import org.apache.commons.dbcp.PoolableConnectionFactory;
041: import org.apache.commons.dbcp.PoolingDriver;
042: import org.apache.commons.pool.ObjectPool;
043: import org.apache.commons.pool.impl.GenericObjectPool;
044: import org.methodize.nntprss.rss.db.ChannelManagerDAO;
045: import org.w3c.dom.Document;
046: import org.w3c.dom.Element;
047:
048: /**
049: * @author Jason Brome <jason@methodize.org>
050: * @version $Id: DBManager.java,v 1.3 2003/01/22 05:05:27 jasonbrome Exp $
051: */
052: public class DBManager {
053:
054: private String connectString;
055:
056: public static final String POOL_CONNECT_STRING = "jdbc:apache:commons:dbcp:nntprss";
057:
058: public DBManager() {
059: }
060:
061: public void startup() {
062: }
063:
064: public void shutdown() {
065: Connection conn = null;
066: Statement stmt = null;
067: try {
068: conn = DriverManager.getConnection(POOL_CONNECT_STRING);
069: stmt = conn.createStatement();
070: stmt.executeQuery("CHECKPOINT");
071: stmt.executeQuery("SHUTDOWN");
072: } catch (SQLException e) {
073: } finally {
074: try {
075: if (stmt != null)
076: stmt.close();
077: } catch (Exception e) {
078: }
079: try {
080: if (conn != null)
081: conn.close();
082: } catch (Exception e) {
083: }
084: }
085: }
086:
087: public void configure(Document config) throws Exception {
088:
089: Element rootElm = config.getDocumentElement();
090: Element dbConfig = (Element) rootElm.getElementsByTagName("db")
091: .item(0);
092: connectString = dbConfig.getAttribute("connect");
093:
094: ObjectPool connectionPool = new GenericObjectPool(null);
095:
096: Class.forName("org.hsqldb.jdbcDriver");
097:
098: ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
099: connectString, "sa", "");
100:
101: //
102: // Now we'll create the PoolableConnectionFactory, which wraps
103: // the "real" Connections created by the ConnectionFactory with
104: // the classes that implement the pooling functionality.
105: //
106: PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
107: connectionFactory, connectionPool, null, null, false,
108: true);
109:
110: //
111: // Finally, we create the PoolingDriver itself...
112: //
113: PoolingDriver driver = new PoolingDriver();
114:
115: //
116: // ...and register our pool with it.
117: //
118: driver.registerPool("nntprss", connectionPool);
119:
120: ChannelManagerDAO.getChannelManagerDAO().initialize(config);
121:
122: }
123:
124: }
|