1 17 18 package org.apache.geronimo.connector.outbound; 19 20 import java.util.Collections ; 21 22 import javax.resource.ResourceException ; 23 24 30 public class ThreadLocalCachingConnectionInterceptor implements ConnectionInterceptor { 31 32 private final ConnectionInterceptor next; 33 34 private final ThreadLocal connections = new ThreadLocal (); 35 private final boolean matchConnections; 36 37 public ThreadLocalCachingConnectionInterceptor(final ConnectionInterceptor next, final boolean matchConnections) { 38 this.next = next; 39 this.matchConnections = matchConnections; 40 } 41 42 43 public void getConnection(ConnectionInfo connectionInfo) throws ResourceException { 44 if (connectionInfo.isUnshareable()) { 45 next.getConnection(connectionInfo); 46 return; 47 } 48 ManagedConnectionInfo managedConnectionInfo = (ManagedConnectionInfo) connections.get(); 49 if (managedConnectionInfo != null) { 50 if (matchConnections) { 51 ManagedConnectionInfo mciRequest = connectionInfo.getManagedConnectionInfo(); 52 if (null != managedConnectionInfo.getManagedConnectionFactory().matchManagedConnections( 53 Collections.singleton(managedConnectionInfo.getManagedConnection()), 54 mciRequest.getSubject(), 55 mciRequest.getConnectionRequestInfo() 56 )) { 57 connectionInfo.setManagedConnectionInfo(managedConnectionInfo); 58 return; 59 } else { 60 connections.set(null); 62 next.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE); 63 } 64 } else { 65 connectionInfo.setManagedConnectionInfo(managedConnectionInfo); 66 return; 67 } 68 } 69 next.getConnection(connectionInfo); 71 connections.set(connectionInfo.getManagedConnectionInfo()); 72 } 73 74 public void returnConnection(ConnectionInfo connectionInfo, ConnectionReturnAction connectionReturnAction) { 75 if (connectionReturnAction == ConnectionReturnAction.DESTROY || connectionInfo.isUnshareable()) { 76 next.returnConnection(connectionInfo, connectionReturnAction); 77 } 78 } 79 80 public void destroy() { 81 next.destroy(); 82 } 83 } 84 | Popular Tags |