1 17 18 package org.apache.geronimo.connector.mock; 19 20 import java.io.PrintWriter ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Set ; 27 import javax.resource.ResourceException ; 28 import javax.resource.spi.ConnectionEvent ; 29 import javax.resource.spi.ConnectionEventListener ; 30 import javax.resource.spi.ConnectionRequestInfo ; 31 import javax.resource.spi.LocalTransaction ; 32 import javax.resource.spi.ManagedConnection ; 33 import javax.resource.spi.ManagedConnectionMetaData ; 34 import javax.security.auth.Subject ; 35 import javax.transaction.xa.XAResource ; 36 37 43 public class MockManagedConnection implements ManagedConnection { 44 45 private final MockManagedConnectionFactory managedConnectionFactory; 46 private final MockXAResource mockXAResource; 47 private Subject subject; 48 private MockConnectionRequestInfo connectionRequestInfo; 49 50 private final Set connections = new HashSet (); 51 private final List connectionEventListeners = Collections.synchronizedList(new ArrayList ()); 52 53 private boolean destroyed; 54 private PrintWriter logWriter; 55 56 public MockManagedConnection(MockManagedConnectionFactory managedConnectionFactory, Subject subject, MockConnectionRequestInfo connectionRequestInfo) { 57 this.managedConnectionFactory = managedConnectionFactory; 58 mockXAResource = new MockXAResource(this); 59 this.subject = subject; 60 this.connectionRequestInfo = connectionRequestInfo; 61 } 62 63 public Object getConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) throws ResourceException { 64 checkSecurityConsistency(subject, connectionRequestInfo); 65 MockConnection mockConnection = new MockConnection(this, subject, (MockConnectionRequestInfo) connectionRequestInfo); 66 connections.add(mockConnection); 67 return mockConnection; 68 } 69 70 private void checkSecurityConsistency(Subject subject, ConnectionRequestInfo connectionRequestInfo) { 71 if (!managedConnectionFactory.isReauthentication()) { 72 assert subject == null ? this.subject == null : subject.equals(this.subject); 73 assert connectionRequestInfo == null ? this.connectionRequestInfo == null : connectionRequestInfo.equals(this.connectionRequestInfo); 74 } 75 } 76 77 public void destroy() throws ResourceException { 78 destroyed = true; 79 cleanup(); 80 } 81 82 public void cleanup() throws ResourceException { 83 for (Iterator iterator = new HashSet (connections).iterator(); iterator.hasNext();) { 84 MockConnection mockConnection = (MockConnection) iterator.next(); 85 mockConnection.close(); 86 } 87 assert connections.isEmpty(); 88 } 89 90 public void associateConnection(Object connection) throws ResourceException { 91 assert connection != null; 92 assert connection.getClass() == MockConnection.class; 93 MockConnection mockConnection = (MockConnection) connection; 94 checkSecurityConsistency(mockConnection.getSubject(), mockConnection.getConnectionRequestInfo()); 95 mockConnection.reassociate(this); 96 connections.add(mockConnection); 97 } 98 99 public void addConnectionEventListener(ConnectionEventListener listener) { 100 connectionEventListeners.add(listener); 101 } 102 103 public void removeConnectionEventListener(ConnectionEventListener listener) { 104 connectionEventListeners.remove(listener); 105 } 106 107 public XAResource getXAResource() throws ResourceException { 108 return mockXAResource; 109 } 110 111 public LocalTransaction getLocalTransaction() throws ResourceException { 112 return new MockSPILocalTransaction(); 113 } 114 115 public ManagedConnectionMetaData getMetaData() throws ResourceException { 116 return null; 117 } 118 119 public void setLogWriter(PrintWriter logWriter) throws ResourceException { 120 this.logWriter = logWriter; 121 } 122 123 public PrintWriter getLogWriter() throws ResourceException { 124 return logWriter; 125 } 126 127 public Subject getSubject() { 128 return subject; 129 } 130 131 public MockConnectionRequestInfo getConnectionRequestInfo() { 132 return connectionRequestInfo; 133 } 134 135 public void removeHandle(MockConnection mockConnection) { 136 connections.remove(mockConnection); 137 } 138 139 public MockManagedConnectionFactory getManagedConnectionFactory() { 140 return managedConnectionFactory; 141 } 142 143 public Set getConnections() { 144 return connections; 145 } 146 147 public List getConnectionEventListeners() { 148 return connectionEventListeners; 149 } 150 151 public boolean isDestroyed() { 152 return destroyed; 153 } 154 155 public void closedEvent(MockConnection mockConnection) { 156 ConnectionEvent connectionEvent = new ConnectionEvent (this, ConnectionEvent.CONNECTION_CLOSED); 157 connectionEvent.setConnectionHandle(mockConnection); 158 for (Iterator iterator = new ArrayList (connectionEventListeners).iterator(); iterator.hasNext();) { 159 ConnectionEventListener connectionEventListener = (ConnectionEventListener ) iterator.next(); 160 connectionEventListener.connectionClosed(connectionEvent); 161 } 162 } 163 164 public void errorEvent(MockConnection mockConnection) { 165 ConnectionEvent connectionEvent = new ConnectionEvent (this, ConnectionEvent.CONNECTION_ERROR_OCCURRED); 166 connectionEvent.setConnectionHandle(mockConnection); 167 for (Iterator iterator = new ArrayList (connectionEventListeners).iterator(); iterator.hasNext();) { 168 ConnectionEventListener connectionEventListener = (ConnectionEventListener ) iterator.next(); 169 connectionEventListener.connectionErrorOccurred(connectionEvent); 170 } 171 } 172 173 public void localTransactionStartedEvent(MockConnection mockConnection) { 174 ConnectionEvent connectionEvent = new ConnectionEvent (this, ConnectionEvent.LOCAL_TRANSACTION_STARTED); 175 connectionEvent.setConnectionHandle(mockConnection); 176 for (Iterator iterator = new ArrayList (connectionEventListeners).iterator(); iterator.hasNext();) { 177 ConnectionEventListener connectionEventListener = (ConnectionEventListener ) iterator.next(); 178 connectionEventListener.localTransactionStarted(connectionEvent); 179 } 180 } 181 182 public void localTransactionCommittedEvent(MockConnection mockConnection) { 183 ConnectionEvent connectionEvent = new ConnectionEvent (this, ConnectionEvent.LOCAL_TRANSACTION_COMMITTED); 184 connectionEvent.setConnectionHandle(mockConnection); 185 for (Iterator iterator = new ArrayList (connectionEventListeners).iterator(); iterator.hasNext();) { 186 ConnectionEventListener connectionEventListener = (ConnectionEventListener ) iterator.next(); 187 connectionEventListener.localTransactionCommitted(connectionEvent); 188 } 189 } 190 191 public void localTransactionRolledBackEvent(MockConnection mockConnection) { 192 ConnectionEvent connectionEvent = new ConnectionEvent (this, ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK); 193 connectionEvent.setConnectionHandle(mockConnection); 194 for (Iterator iterator = new ArrayList (connectionEventListeners).iterator(); iterator.hasNext();) { 195 ConnectionEventListener connectionEventListener = (ConnectionEventListener ) iterator.next(); 196 connectionEventListener.localTransactionRolledback(connectionEvent); 197 } 198 } 199 } 200 | Popular Tags |