1 18 package org.apache.activemq.ra; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 23 import javax.resource.ResourceException ; 24 import javax.resource.spi.ConnectionEvent ; 25 import javax.resource.spi.ConnectionEventListener ; 26 import javax.resource.spi.ConnectionManager ; 27 import javax.resource.spi.ConnectionRequestInfo ; 28 import javax.resource.spi.ManagedConnection ; 29 import javax.resource.spi.ManagedConnectionFactory ; 30 import javax.security.auth.Subject ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 36 42 public class ConnectionManagerAdapter implements ConnectionManager , ConnectionEventListener { 43 44 private static final long serialVersionUID = 5205646563916645831L; 45 46 private static final Log log = LogFactory.getLog(ConnectionManagerAdapter.class); 47 ArrayList listners = new ArrayList (); 48 ArrayList connections = new ArrayList (); 49 50 56 public void addConnectionEventListener(ConnectionEventListener l ) { 57 for (Iterator iter = connections.iterator(); iter.hasNext();) { 58 ManagedConnection c = (ManagedConnection ) iter.next(); 59 c.addConnectionEventListener(l); 60 } 61 listners.add(l); 62 } 63 64 67 public Object allocateConnection(ManagedConnectionFactory connectionFactory, ConnectionRequestInfo info) throws ResourceException { 68 Subject subject = null; 69 ManagedConnection connection = connectionFactory.createManagedConnection(subject, info); 70 connection.addConnectionEventListener(this); 71 for (Iterator iter = listners.iterator(); iter.hasNext();) { 72 ConnectionEventListener l = (ConnectionEventListener ) iter.next(); 73 connection.addConnectionEventListener(l); 74 } 75 connections.add(connection); 76 return connection.getConnection(subject, info); 77 } 78 79 82 public void connectionClosed(ConnectionEvent event) { 83 connections.remove(event.getSource()); 84 try { 85 ((ManagedConnection )event.getSource()).cleanup(); 86 } catch (ResourceException e) { 87 log.warn("Error occured during the cleanup of a managed connection: ",e); 88 } 89 try { 90 ((ManagedConnection )event.getSource()).destroy(); 91 } catch (ResourceException e) { 92 log.warn("Error occured during the destruction of a managed connection: ",e); 93 } 94 } 95 96 99 public void localTransactionStarted(ConnectionEvent event) { 100 } 101 102 105 public void localTransactionCommitted(ConnectionEvent event) { 106 } 107 108 111 public void localTransactionRolledback(ConnectionEvent event) { 112 } 113 114 117 public void connectionErrorOccurred(ConnectionEvent event) { 118 log.warn("Managed connection experiened an error: ",event.getException()); 119 try { 120 ((ManagedConnection )event.getSource()).cleanup(); 121 } catch (ResourceException e) { 122 log.warn("Error occured during the cleanup of a managed connection: ",e); 123 } 124 try { 125 ((ManagedConnection )event.getSource()).destroy(); 126 } catch (ResourceException e) { 127 log.warn("Error occured during the destruction of a managed connection: ",e); 128 } 129 } 130 131 } 132 | Popular Tags |