KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > CompositeConnectionListener


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import java.sql.Connection JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.logicalcobwebs.proxool.util.AbstractListenerContainer;
14
15 /**
16  * A {@link ConnectionListenerIF} that keeps a list of <code>ConnectionListenerIF</code>s
17  * and notifies them in a thread safe manner.
18  * It also implements {@link org.logicalcobwebs.proxool.util.ListenerContainerIF ListenerContainerIF}
19  * which provides methods for
20  * {@link org.logicalcobwebs.proxool.util.ListenerContainerIF#addListener(Object) adding} and
21  * {@link org.logicalcobwebs.proxool.util.ListenerContainerIF#removeListener(Object) removing} listeners.
22  *
23  * @version $Revision: 1.6 $, $Date: 2006/01/18 14:40:01 $
24  * @author Christian Nedregaard (christian_nedregaard@email.com)
25  * @author $Author: billhorsman $ (current maintainer)
26  * @since Proxool 0.7
27  */

28 public class CompositeConnectionListener extends AbstractListenerContainer implements ConnectionListenerIF {
29     static final Log LOG = LogFactory.getLog(CompositeConnectionListener.class);
30
31     /**
32      * @see ConnectionListenerIF#onBirth(Connection)
33      */

34     public void onBirth(Connection JavaDoc connection) throws SQLException JavaDoc
35     {
36         Object JavaDoc[] listeners = getListeners();
37         
38         for(int i=0; i<listeners.length; i++) {
39             try {
40                 ConnectionListenerIF connectionListener = (ConnectionListenerIF) listeners[i];
41                 connectionListener.onBirth(connection);
42             }
43             catch (RuntimeException JavaDoc re) {
44                 LOG.warn("RuntimeException received from listener "+listeners[i]+" when dispatching onBirth event", re);
45             }
46             catch(SQLException JavaDoc se) {
47                 LOG.warn("SQLException received from listener "+listeners[i]+" when dispatching onBirth event - event dispatching cancelled");
48                 throw se;
49             }
50         }
51     }
52
53     /**
54      * @see ConnectionListenerIF#onDeath(Connection)
55      */

56     public void onDeath(Connection JavaDoc connection) throws SQLException JavaDoc
57     {
58         Object JavaDoc[] listeners = getListeners();
59         
60         for(int i=0; i<listeners.length; i++) {
61             try {
62                 ConnectionListenerIF connectionListener = (ConnectionListenerIF) listeners[i];
63                 connectionListener.onDeath(connection);
64             }
65             catch (RuntimeException JavaDoc re) {
66                 LOG.warn("RuntimeException received from listener "+listeners[i]+" when dispatching onDeath event", re);
67             }
68             catch(SQLException JavaDoc se) {
69                 LOG.warn("SQLException received from listener "+listeners[i]+" when dispatching onDeath event - event dispatching cancelled");
70                 throw se;
71             }
72         }
73     }
74
75     /**
76      * @see ConnectionListenerIF#onExecute(String, long)
77      */

78     public void onExecute(String JavaDoc command, long elapsedTime)
79     {
80         Object JavaDoc[] listeners = getListeners();
81         
82         for(int i=0; i<listeners.length; i++) {
83             try {
84                 ConnectionListenerIF connectionListener = (ConnectionListenerIF) listeners[i];
85                 connectionListener.onExecute(command, elapsedTime);
86             }
87             catch (RuntimeException JavaDoc re) {
88                 LOG.warn("RuntimeException received from listener "+listeners[i]+" when dispatching onExecute event", re);
89             }
90         }
91     }
92
93     /**
94      * @see ConnectionListenerIF#onFail(String, Exception)
95      */

96     public void onFail(String JavaDoc command, Exception JavaDoc exception)
97     {
98         Object JavaDoc[] listeners = getListeners();
99         
100         for(int i=0; i<listeners.length; i++) {
101             try {
102                 ConnectionListenerIF connectionListener = (ConnectionListenerIF) listeners[i];
103                 connectionListener.onFail(command, exception);
104             }
105             catch (RuntimeException JavaDoc re) {
106                 LOG.warn("RuntimeException received from listener "+listeners[i]+" when dispatching onFail event", re);
107             }
108         }
109     }
110 }
111
112 /*
113  Revision history:
114  $Log: CompositeConnectionListener.java,v $
115  Revision 1.6 2006/01/18 14:40:01 billhorsman
116  Unbundled Jakarta's Commons Logging.
117
118  Revision 1.5 2004/03/16 08:48:32 brenuart
119  Changes in the AbstractListenerContainer:
120  - provide more efficient concurrent handling;
121  - better handling of RuntimeException thrown by external listeners.
122
123  Revision 1.4 2003/03/10 15:26:43 billhorsman
124  refactoringn of concurrency stuff (and some import
125  optimisation)
126
127  Revision 1.3 2003/03/03 11:11:56 billhorsman
128  fixed licence
129
130  Revision 1.2 2003/02/07 17:20:16 billhorsman
131  checkstyle
132
133  Revision 1.1 2003/02/07 01:47:17 chr32
134  Initial revition.
135
136 */
Popular Tags