KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.resource.ResourceException JavaDoc;
24 import javax.resource.spi.DissociatableManagedConnection JavaDoc;
25 import javax.resource.spi.ManagedConnection JavaDoc;
26
27 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker;
28
29 /**
30  * ConnectionTrackingInterceptor.java handles communication with the
31  * CachedConnectionManager. On method call entry, cached handles are
32  * checked for the correct Subject. On method call exit, cached
33  * handles are disassociated if possible. On getting or releasing
34  * a connection the CachedConnectionManager is notified.
35  *
36  *
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

39 public class ConnectionTrackingInterceptor implements ConnectionInterceptor {
40
41     private final ConnectionInterceptor next;
42     private final String JavaDoc key;
43     private final ConnectionTracker connectionTracker;
44
45     public ConnectionTrackingInterceptor(
46             final ConnectionInterceptor next,
47             final String JavaDoc key,
48             final ConnectionTracker connectionTracker
49             ) {
50         this.next = next;
51         this.key = key;
52         this.connectionTracker = connectionTracker;
53     }
54
55     /**
56      * called by: GenericConnectionManager.allocateConnection, GenericConnectionManager.associateConnection, and enter.
57      * in: connectionInfo is non-null, and has non-null ManagedConnectionInfo with non-null managedConnectionfactory.
58      * connection handle may or may not be null.
59      * out: connectionInfo has non-null connection handle, non null ManagedConnectionInfo with non-null ManagedConnection and GeronimoConnectionEventListener.
60      * connection tracker has been notified of handle-managed connection association.
61      * @param connectionInfo
62      * @throws ResourceException
63      */

64     public void getConnection(ConnectionInfo connectionInfo) throws ResourceException JavaDoc {
65         connectionTracker.setEnvironment(connectionInfo, key);
66         next.getConnection(connectionInfo);
67         connectionTracker.handleObtained(this, connectionInfo);
68     }
69
70     /**
71      * called by: GeronimoConnectionEventListener.connectionClosed, GeronimoConnectionEventListener.connectionErrorOccurred, exit
72      * in: handle has already been dissociated from ManagedConnection. connectionInfo not null, has non-null ManagedConnectionInfo, ManagedConnectionInfo has non-null ManagedConnection
73      * handle can be null if called from error in ManagedConnection in pool.
74      * out: connectionTracker has been notified, ManagedConnectionInfo null.
75      * @param connectionInfo
76      * @param connectionReturnAction
77      */

78     public void returnConnection(
79             ConnectionInfo connectionInfo,
80             ConnectionReturnAction connectionReturnAction) {
81         connectionTracker.handleReleased(this, connectionInfo);
82         next.returnConnection(connectionInfo, connectionReturnAction);
83     }
84
85     public void destroy() {
86         next.destroy();
87     }
88     
89     public void enter(Collection JavaDoc connectionInfos)
90             throws ResourceException JavaDoc {
91         for (Iterator JavaDoc i = connectionInfos.iterator(); i.hasNext();) {
92             ConnectionInfo connectionInfo = (ConnectionInfo) i.next();
93             next.getConnection(connectionInfo);
94         }
95
96     }
97
98     public void exit(Collection JavaDoc connectionInfos)
99             throws ResourceException JavaDoc {
100         for (Iterator JavaDoc i = connectionInfos.iterator(); i.hasNext();) {
101             ConnectionInfo connectionInfo = (ConnectionInfo) i.next();
102             if (connectionInfo.isUnshareable()) {
103                 //if one is, they all are
104
return;
105             }
106             ManagedConnectionInfo managedConnectionInfo = connectionInfo.getManagedConnectionInfo();
107             ManagedConnection JavaDoc managedConnection = managedConnectionInfo.getManagedConnection();
108             if (managedConnection instanceof DissociatableManagedConnection JavaDoc
109                     && managedConnectionInfo.isFirstConnectionInfo(connectionInfo)) {
110                 int size = connectionInfos.size();
111                 i.remove();
112                 assert size - 1 == connectionInfos.size();
113                 ((DissociatableManagedConnection JavaDoc) managedConnection).dissociateConnections();
114                 managedConnectionInfo.clearConnectionHandles();
115                 //todo this needs some kind of check so cx isn't returned more than once
116
//in case dissociate calls connection closed event and returns cx to pool.
117
returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE);
118             }
119         }
120     }
121 }
122
Popular Tags