KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > GeronimoConnectionEventListener


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector.outbound;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24
25 import javax.resource.spi.ConnectionEvent JavaDoc;
26 import javax.resource.spi.ConnectionEventListener JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * ConnectionEventListener.java
33  *
34  *
35  * Created: Thu Oct 2 14:57:43 2003
36  *
37  * @version 1.0
38  */

39 public class GeronimoConnectionEventListener implements ConnectionEventListener JavaDoc {
40
41     private static Log log = LogFactory.getLog(GeronimoConnectionEventListener.class.getName());
42
43     private final ManagedConnectionInfo managedConnectionInfo;
44     private final ConnectionInterceptor stack;
45     private final List JavaDoc connectionInfos = new ArrayList JavaDoc();
46     private boolean errorOccurred = false;
47
48     public GeronimoConnectionEventListener(
49             final ConnectionInterceptor stack,
50             final ManagedConnectionInfo managedConnectionInfo) {
51         this.stack = stack;
52         this.managedConnectionInfo = managedConnectionInfo;
53     }
54
55     public void connectionClosed(ConnectionEvent JavaDoc connectionEvent) {
56         if (connectionEvent.getSource() != managedConnectionInfo.getManagedConnection()) {
57             throw new IllegalArgumentException JavaDoc(
58                     "ConnectionClosed event received from wrong ManagedConnection. Expected "
59                     + managedConnectionInfo.getManagedConnection()
60                     + ", actual "
61                     + connectionEvent.getSource());
62         }
63         if (log.isTraceEnabled()) {
64             log.trace("connectionClosed called with " + connectionEvent.getConnectionHandle() + " for MCI: " + managedConnectionInfo + " and MC: " + managedConnectionInfo.getManagedConnection());
65         }
66         ConnectionInfo ci = new ConnectionInfo(managedConnectionInfo);
67         ci.setConnectionHandle(connectionEvent.getConnectionHandle());
68         try {
69             stack.returnConnection(ci, ConnectionReturnAction.RETURN_HANDLE);
70         } catch (Throwable JavaDoc e) {
71             if (log.isTraceEnabled()) {
72                 log.trace("connectionClosed failed with " + connectionEvent.getConnectionHandle() + " for MCI: " + managedConnectionInfo + " and MC: " + managedConnectionInfo.getManagedConnection(), e);
73             }
74             if (e instanceof Error JavaDoc) {
75                 throw (Error JavaDoc)e;
76             }
77         }
78     }
79
80     public void connectionErrorOccurred(ConnectionEvent JavaDoc connectionEvent) {
81         if (connectionEvent.getSource() != managedConnectionInfo.getManagedConnection()) {
82             throw new IllegalArgumentException JavaDoc(
83                     "ConnectionError event received from wrong ManagedConnection. Expected "
84                     + managedConnectionInfo.getManagedConnection()
85                     + ", actual "
86                     + connectionEvent.getSource());
87         }
88         log.warn("connectionErrorOccurred called with " + connectionEvent.getConnectionHandle(), connectionEvent.getException());
89         boolean errorOccurred = this.errorOccurred;
90         this.errorOccurred = true;
91         if (!errorOccurred) {
92             ConnectionInfo ci = new ConnectionInfo(managedConnectionInfo);
93             ci.setConnectionHandle(connectionEvent.getConnectionHandle());
94             stack.returnConnection(ci, ConnectionReturnAction.DESTROY);
95         }
96     }
97
98     public void localTransactionStarted(ConnectionEvent JavaDoc event) {
99         //TODO implement this method
100
}
101
102     /**
103      * The <code>localTransactionCommitted</code> method
104      *
105      * @param event a <code>ConnectionEvent</code> value
106      * todo implement this method
107      */

108     public void localTransactionCommitted(ConnectionEvent JavaDoc event) {
109     }
110
111     /**
112      * The <code>localTransactionRolledback</code> method
113      *
114      * @param event a <code>ConnectionEvent</code> value
115      * todo implement this method
116      */

117     public void localTransactionRolledback(ConnectionEvent JavaDoc event) {
118     }
119
120     public void addConnectionInfo(ConnectionInfo connectionInfo) {
121         assert connectionInfo.getConnectionHandle() != null;
122         connectionInfos.add(connectionInfo);
123     }
124
125     public void removeConnectionInfo(ConnectionInfo connectionInfo) {
126         assert connectionInfo.getConnectionHandle() != null;
127         connectionInfos.remove(connectionInfo);
128     }
129
130     public boolean hasConnectionInfos() {
131         return !connectionInfos.isEmpty();
132     }
133
134     public void clearConnectionInfos() {
135         connectionInfos.clear();
136     }
137
138     public boolean hasConnectionInfo(ConnectionInfo connectionInfo) {
139         return connectionInfos.contains(connectionInfo);
140     }
141
142     public boolean isFirstConnectionInfo(ConnectionInfo connectionInfo) {
143         return !connectionInfos.isEmpty() && connectionInfos.get(0) == connectionInfo;
144     }
145
146     public Collection JavaDoc getConnectionInfos() {
147         return Collections.unmodifiableCollection(connectionInfos);
148     }
149
150 }
151
Popular Tags