001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.jdbc;
031:
032: import com.caucho.db.Database;
033: import com.caucho.util.L10N;
034:
035: import javax.sql.ConnectionEvent;
036: import javax.sql.ConnectionEventListener;
037: import javax.sql.PooledConnection;
038: import java.sql.Connection;
039: import java.sql.SQLException;
040: import java.util.ArrayList;
041: import javax.sql.StatementEventListener;
042:
043: /**
044: * The JDBC connection implementation.
045: */
046: class PooledConnectionImpl implements PooledConnection {
047: private final static L10N L = new L10N(PooledConnectionImpl.class);
048:
049: private final Database _db;
050:
051: private ArrayList<ConnectionEventListener> _listeners = new ArrayList<ConnectionEventListener>();
052:
053: private boolean _isClosed;
054:
055: PooledConnectionImpl(Database db) {
056: _db = db;
057:
058: if (_db == null)
059: throw new NullPointerException();
060: }
061:
062: /**
063: * Returns the database.
064: */
065: Database getDatabase() {
066: return _db;
067: }
068:
069: /**
070: * Returns a new connection implementation.
071: */
072: public Connection getConnection() throws SQLException {
073: if (_isClosed)
074: throw new IllegalStateException(L
075: .l("getting connection after close"));
076:
077: // PooledConnectionImpl conn = new PooledConnectionImpl(this);
078:
079: return new ConnectionImpl(this );
080: }
081:
082: /**
083: * Adds a new listener.
084: */
085: public void addConnectionEventListener(
086: ConnectionEventListener listener) {
087: if (!_listeners.contains(listener))
088: _listeners.add(listener);
089: }
090:
091: /**
092: * Removes the new listener.
093: */
094: public void removeConnectionEventListener(
095: ConnectionEventListener listener) {
096: _listeners.remove(listener);
097: }
098:
099: void closeEvent(Connection conn) {
100: ConnectionEvent event = new ConnectionEvent(this );
101:
102: for (int i = 0; i < _listeners.size(); i++) {
103: ConnectionEventListener listener = _listeners.get(i);
104:
105: listener.connectionClosed(event);
106: }
107: }
108:
109: public void close() throws SQLException {
110: _isClosed = true;
111: }
112:
113: public void addStatementEventListener(
114: StatementEventListener listener) {
115: throw new UnsupportedOperationException("Not supported yet.");
116: }
117:
118: public void removeStatementEventListener(
119: StatementEventListener listener) {
120: throw new UnsupportedOperationException("Not supported yet.");
121: }
122: }
|