001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import java.sql.Connection;
009: import java.sql.SQLException;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.logicalcobwebs.proxool.util.AbstractListenerContainer;
014:
015: /**
016: * A {@link ConnectionListenerIF} that keeps a list of <code>ConnectionListenerIF</code>s
017: * and notifies them in a thread safe manner.
018: * It also implements {@link org.logicalcobwebs.proxool.util.ListenerContainerIF ListenerContainerIF}
019: * which provides methods for
020: * {@link org.logicalcobwebs.proxool.util.ListenerContainerIF#addListener(Object) adding} and
021: * {@link org.logicalcobwebs.proxool.util.ListenerContainerIF#removeListener(Object) removing} listeners.
022: *
023: * @version $Revision: 1.6 $, $Date: 2006/01/18 14:40:01 $
024: * @author Christian Nedregaard (christian_nedregaard@email.com)
025: * @author $Author: billhorsman $ (current maintainer)
026: * @since Proxool 0.7
027: */
028: public class CompositeConnectionListener extends
029: AbstractListenerContainer implements ConnectionListenerIF {
030: static final Log LOG = LogFactory
031: .getLog(CompositeConnectionListener.class);
032:
033: /**
034: * @see ConnectionListenerIF#onBirth(Connection)
035: */
036: public void onBirth(Connection connection) throws SQLException {
037: Object[] listeners = getListeners();
038:
039: for (int i = 0; i < listeners.length; i++) {
040: try {
041: ConnectionListenerIF connectionListener = (ConnectionListenerIF) listeners[i];
042: connectionListener.onBirth(connection);
043: } catch (RuntimeException re) {
044: LOG.warn("RuntimeException received from listener "
045: + listeners[i]
046: + " when dispatching onBirth event", re);
047: } catch (SQLException se) {
048: LOG
049: .warn("SQLException received from listener "
050: + listeners[i]
051: + " when dispatching onBirth event - event dispatching cancelled");
052: throw se;
053: }
054: }
055: }
056:
057: /**
058: * @see ConnectionListenerIF#onDeath(Connection)
059: */
060: public void onDeath(Connection connection) throws SQLException {
061: Object[] listeners = getListeners();
062:
063: for (int i = 0; i < listeners.length; i++) {
064: try {
065: ConnectionListenerIF connectionListener = (ConnectionListenerIF) listeners[i];
066: connectionListener.onDeath(connection);
067: } catch (RuntimeException re) {
068: LOG.warn("RuntimeException received from listener "
069: + listeners[i]
070: + " when dispatching onDeath event", re);
071: } catch (SQLException se) {
072: LOG
073: .warn("SQLException received from listener "
074: + listeners[i]
075: + " when dispatching onDeath event - event dispatching cancelled");
076: throw se;
077: }
078: }
079: }
080:
081: /**
082: * @see ConnectionListenerIF#onExecute(String, long)
083: */
084: public void onExecute(String command, long elapsedTime) {
085: Object[] listeners = getListeners();
086:
087: for (int i = 0; i < listeners.length; i++) {
088: try {
089: ConnectionListenerIF connectionListener = (ConnectionListenerIF) listeners[i];
090: connectionListener.onExecute(command, elapsedTime);
091: } catch (RuntimeException re) {
092: LOG.warn("RuntimeException received from listener "
093: + listeners[i]
094: + " when dispatching onExecute event", re);
095: }
096: }
097: }
098:
099: /**
100: * @see ConnectionListenerIF#onFail(String, Exception)
101: */
102: public void onFail(String command, Exception exception) {
103: Object[] listeners = getListeners();
104:
105: for (int i = 0; i < listeners.length; i++) {
106: try {
107: ConnectionListenerIF connectionListener = (ConnectionListenerIF) listeners[i];
108: connectionListener.onFail(command, exception);
109: } catch (RuntimeException re) {
110: LOG.warn("RuntimeException received from listener "
111: + listeners[i]
112: + " when dispatching onFail event", re);
113: }
114: }
115: }
116: }
117:
118: /*
119: Revision history:
120: $Log: CompositeConnectionListener.java,v $
121: Revision 1.6 2006/01/18 14:40:01 billhorsman
122: Unbundled Jakarta's Commons Logging.
123:
124: Revision 1.5 2004/03/16 08:48:32 brenuart
125: Changes in the AbstractListenerContainer:
126: - provide more efficient concurrent handling;
127: - better handling of RuntimeException thrown by external listeners.
128:
129: Revision 1.4 2003/03/10 15:26:43 billhorsman
130: refactoringn of concurrency stuff (and some import
131: optimisation)
132:
133: Revision 1.3 2003/03/03 11:11:56 billhorsman
134: fixed licence
135:
136: Revision 1.2 2003/02/07 17:20:16 billhorsman
137: checkstyle
138:
139: Revision 1.1 2003/02/07 01:47:17 chr32
140: Initial revition.
141:
142: */
|