1 17 18 package org.apache.geronimo.connector.outbound; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 25 import javax.resource.spi.ConnectionEvent ; 26 import javax.resource.spi.ConnectionEventListener ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 39 public class GeronimoConnectionEventListener implements ConnectionEventListener { 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 connectionInfos = new ArrayList (); 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 connectionEvent) { 56 if (connectionEvent.getSource() != managedConnectionInfo.getManagedConnection()) { 57 throw new IllegalArgumentException ( 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 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 ) { 75 throw (Error )e; 76 } 77 } 78 } 79 80 public void connectionErrorOccurred(ConnectionEvent connectionEvent) { 81 if (connectionEvent.getSource() != managedConnectionInfo.getManagedConnection()) { 82 throw new IllegalArgumentException ( 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 event) { 99 } 101 102 108 public void localTransactionCommitted(ConnectionEvent event) { 109 } 110 111 117 public void localTransactionRolledback(ConnectionEvent 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 getConnectionInfos() { 147 return Collections.unmodifiableCollection(connectionInfos); 148 } 149 150 } 151 | Popular Tags |