KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > connectiontracking > ConnectionTrackingCoordinator


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.connectiontracking;
19
20 import java.util.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.resource.ResourceException JavaDoc;
26
27 import org.apache.geronimo.connector.outbound.ConnectionInfo;
28 import org.apache.geronimo.connector.outbound.ConnectionTrackingInterceptor;
29 import org.apache.geronimo.connector.outbound.ManagedConnectionInfo;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * ConnectionTrackingCoordinator tracks connections that are in use by
35  * components such as EJB's. The component must notify the ccm
36  * when a method enters and exits. On entrance, the ccm will
37  * notify ConnectionManager stacks so the stack can make sure all
38  * connection handles left open from previous method calls are
39  * attached to ManagedConnections of the correct security context, and
40  * the ManagedConnections are enrolled in any current transaction.
41  * On exit, the ccm will notify ConnectionManager stacks of the handles
42  * left open, so they may be disassociated if appropriate.
43  * In addition, when a UserTransaction is started the ccm will notify
44  * ConnectionManager stacks so the existing ManagedConnections can be
45  * enrolled properly.
46  *
47  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
48  */

49 public class ConnectionTrackingCoordinator implements TrackedConnectionAssociator, ConnectionTracker {
50     private static final Log log = LogFactory.getLog(ConnectionTrackingCoordinator.class.getName());
51
52     private final ThreadLocal JavaDoc currentInstanceContexts = new ThreadLocal JavaDoc();
53
54     public ConnectorInstanceContext enter(ConnectorInstanceContext newConnectorInstanceContext)
55             throws ResourceException JavaDoc {
56         ConnectorInstanceContext oldConnectorInstanceContext = (ConnectorInstanceContext) currentInstanceContexts.get();
57         currentInstanceContexts.set(newConnectorInstanceContext);
58         notifyConnections(newConnectorInstanceContext);
59         return oldConnectorInstanceContext;
60     }
61
62     private void notifyConnections(ConnectorInstanceContext oldConnectorInstanceContext) throws ResourceException JavaDoc {
63         Map JavaDoc connectionManagerToManagedConnectionInfoMap = oldConnectorInstanceContext.getConnectionManagerMap();
64         for (Iterator JavaDoc i = connectionManagerToManagedConnectionInfoMap.entrySet().iterator(); i.hasNext();) {
65             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
66             ConnectionTrackingInterceptor mcci =
67                     (ConnectionTrackingInterceptor) entry.getKey();
68             Set JavaDoc connections = (Set JavaDoc) entry.getValue();
69             mcci.enter(connections);
70         }
71     }
72
73     public void newTransaction() throws ResourceException JavaDoc {
74         ConnectorInstanceContext oldConnectorInstanceContext = (ConnectorInstanceContext) currentInstanceContexts.get();
75         if (oldConnectorInstanceContext == null) {
76             return;
77         }
78         notifyConnections(oldConnectorInstanceContext);
79     }
80
81     public void exit(ConnectorInstanceContext reenteringConnectorInstanceContext)
82             throws ResourceException JavaDoc {
83         ConnectorInstanceContext oldConnectorInstanceContext = (ConnectorInstanceContext) currentInstanceContexts.get();
84         Map JavaDoc resources = oldConnectorInstanceContext.getConnectionManagerMap();
85         for (Iterator JavaDoc i = resources.entrySet().iterator(); i.hasNext();) {
86             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
87             ConnectionTrackingInterceptor mcci =
88                     (ConnectionTrackingInterceptor) entry.getKey();
89             Set JavaDoc connections = (Set JavaDoc) entry.getValue();
90             mcci.exit(connections);
91             if (connections.isEmpty()) {
92                 i.remove();
93             }
94         }
95         currentInstanceContexts.set(reenteringConnectorInstanceContext);
96     }
97
98
99     public void handleObtained(
100             ConnectionTrackingInterceptor connectionTrackingInterceptor,
101             ConnectionInfo connectionInfo) {
102         ConnectorInstanceContext connectorInstanceContext = (ConnectorInstanceContext) currentInstanceContexts.get();
103         if (connectorInstanceContext == null) {
104             return;
105         }
106         Map JavaDoc resources = connectorInstanceContext.getConnectionManagerMap();
107         Set JavaDoc infos = (Set JavaDoc) resources.get(connectionTrackingInterceptor);
108         if (infos == null) {
109             infos = new HashSet JavaDoc();
110             resources.put(connectionTrackingInterceptor, infos);
111         }
112         infos.add(connectionInfo);
113     }
114
115     public void handleReleased(
116             ConnectionTrackingInterceptor connectionTrackingInterceptor,
117             ConnectionInfo connectionInfo) {
118         ConnectorInstanceContext connectorInstanceContext = (ConnectorInstanceContext) currentInstanceContexts.get();
119         if (connectorInstanceContext == null) {
120             return;
121         }
122         Map JavaDoc resources = connectorInstanceContext.getConnectionManagerMap();
123         Set JavaDoc infos = (Set JavaDoc) resources.get(connectionTrackingInterceptor);
124         if (infos == null) {
125             if (log.isTraceEnabled()) {
126                 log.trace("No infos found for handle " + connectionInfo.getConnectionHandle() + " for MCI: " + connectionInfo.getManagedConnectionInfo() + " for MC: " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " for CTI: " + connectionTrackingInterceptor, new Exception JavaDoc("Stack Trace"));
127             }
128         }
129         if (connectionInfo.getConnectionHandle() == null) {
130             //destroy was called as a result of an error
131
ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo();
132             Collection JavaDoc toRemove = mci.getConnectionInfos();
133             infos.removeAll(toRemove);
134         } else {
135             infos.remove(connectionInfo);
136         }
137     }
138
139     public void setEnvironment(ConnectionInfo connectionInfo, String JavaDoc key) {
140         ConnectorInstanceContext currentConnectorInstanceContext = (ConnectorInstanceContext) currentInstanceContexts.get();
141         if (currentConnectorInstanceContext != null) {
142             Set JavaDoc unshareableResources = currentConnectorInstanceContext.getUnshareableResources();
143             boolean unshareable = unshareableResources.contains(key);
144             connectionInfo.setUnshareable(unshareable);
145             Set JavaDoc applicationManagedSecurityResources = currentConnectorInstanceContext.getApplicationManagedSecurityResources();
146             boolean applicationManagedSecurity = applicationManagedSecurityResources.contains(key);
147             connectionInfo.setApplicationManagedSecurity(applicationManagedSecurity);
148         }
149     }
150
151 }
152
Popular Tags