1 17 18 package org.apache.geronimo.connector.outbound.connectiontracking; 19 20 import java.util.Collection ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import javax.resource.ResourceException ; 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 49 public class ConnectionTrackingCoordinator implements TrackedConnectionAssociator, ConnectionTracker { 50 private static final Log log = LogFactory.getLog(ConnectionTrackingCoordinator.class.getName()); 51 52 private final ThreadLocal currentInstanceContexts = new ThreadLocal (); 53 54 public ConnectorInstanceContext enter(ConnectorInstanceContext newConnectorInstanceContext) 55 throws ResourceException { 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 { 63 Map connectionManagerToManagedConnectionInfoMap = oldConnectorInstanceContext.getConnectionManagerMap(); 64 for (Iterator i = connectionManagerToManagedConnectionInfoMap.entrySet().iterator(); i.hasNext();) { 65 Map.Entry entry = (Map.Entry ) i.next(); 66 ConnectionTrackingInterceptor mcci = 67 (ConnectionTrackingInterceptor) entry.getKey(); 68 Set connections = (Set ) entry.getValue(); 69 mcci.enter(connections); 70 } 71 } 72 73 public void newTransaction() throws ResourceException { 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 { 83 ConnectorInstanceContext oldConnectorInstanceContext = (ConnectorInstanceContext) currentInstanceContexts.get(); 84 Map resources = oldConnectorInstanceContext.getConnectionManagerMap(); 85 for (Iterator i = resources.entrySet().iterator(); i.hasNext();) { 86 Map.Entry entry = (Map.Entry ) i.next(); 87 ConnectionTrackingInterceptor mcci = 88 (ConnectionTrackingInterceptor) entry.getKey(); 89 Set connections = (Set ) 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 resources = connectorInstanceContext.getConnectionManagerMap(); 107 Set infos = (Set ) resources.get(connectionTrackingInterceptor); 108 if (infos == null) { 109 infos = new HashSet (); 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 resources = connectorInstanceContext.getConnectionManagerMap(); 123 Set infos = (Set ) 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 ("Stack Trace")); 127 } 128 } 129 if (connectionInfo.getConnectionHandle() == null) { 130 ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo(); 132 Collection toRemove = mci.getConnectionInfos(); 133 infos.removeAll(toRemove); 134 } else { 135 infos.remove(connectionInfo); 136 } 137 } 138 139 public void setEnvironment(ConnectionInfo connectionInfo, String key) { 140 ConnectorInstanceContext currentConnectorInstanceContext = (ConnectorInstanceContext) currentInstanceContexts.get(); 141 if (currentConnectorInstanceContext != null) { 142 Set unshareableResources = currentConnectorInstanceContext.getUnshareableResources(); 143 boolean unshareable = unshareableResources.contains(key); 144 connectionInfo.setUnshareable(unshareable); 145 Set applicationManagedSecurityResources = currentConnectorInstanceContext.getApplicationManagedSecurityResources(); 146 boolean applicationManagedSecurity = applicationManagedSecurityResources.contains(key); 147 connectionInfo.setApplicationManagedSecurity(applicationManagedSecurity); 148 } 149 } 150 151 } 152 | Popular Tags |