001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.connector.mock;
017:
018: import java.io.PrintWriter;
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025: import javax.resource.ResourceException;
026: import javax.resource.spi.ConnectionEvent;
027: import javax.resource.spi.ConnectionEventListener;
028: import javax.resource.spi.ConnectionRequestInfo;
029: import javax.resource.spi.LocalTransaction;
030: import javax.resource.spi.ManagedConnection;
031: import javax.resource.spi.ManagedConnectionMetaData;
032: import javax.security.auth.Subject;
033: import javax.transaction.xa.XAResource;
034:
035: /**
036: *
037: *
038: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
039: *
040: * */
041: public class MockManagedConnection implements ManagedConnection {
042:
043: private final MockManagedConnectionFactory managedConnectionFactory;
044: private final MockXAResource mockXAResource;
045: private Subject subject;
046: private MockConnectionRequestInfo connectionRequestInfo;
047:
048: private final Set connections = new HashSet();
049: private final List connectionEventListeners = Collections
050: .synchronizedList(new ArrayList());
051:
052: private boolean destroyed;
053: private PrintWriter logWriter;
054:
055: public MockManagedConnection(
056: MockManagedConnectionFactory managedConnectionFactory,
057: Subject subject,
058: MockConnectionRequestInfo connectionRequestInfo) {
059: this .managedConnectionFactory = managedConnectionFactory;
060: mockXAResource = new MockXAResource(this );
061: this .subject = subject;
062: this .connectionRequestInfo = connectionRequestInfo;
063: }
064:
065: public Object getConnection(Subject subject,
066: ConnectionRequestInfo connectionRequestInfo)
067: throws ResourceException {
068: checkSecurityConsistency(subject, connectionRequestInfo);
069: MockConnection mockConnection = new MockConnection(this ,
070: subject,
071: (MockConnectionRequestInfo) connectionRequestInfo);
072: connections.add(mockConnection);
073: return mockConnection;
074: }
075:
076: private void checkSecurityConsistency(Subject subject,
077: ConnectionRequestInfo connectionRequestInfo) {
078: if (!managedConnectionFactory.isReauthentication()) {
079: assert subject == null ? this .subject == null : subject
080: .equals(this .subject);
081: assert connectionRequestInfo == null ? this .connectionRequestInfo == null
082: : connectionRequestInfo
083: .equals(this .connectionRequestInfo);
084: }
085: }
086:
087: public void destroy() throws ResourceException {
088: destroyed = true;
089: cleanup();
090: }
091:
092: public void cleanup() throws ResourceException {
093: for (Iterator iterator = new HashSet(connections).iterator(); iterator
094: .hasNext();) {
095: MockConnection mockConnection = (MockConnection) iterator
096: .next();
097: mockConnection.close();
098: }
099: assert connections.isEmpty();
100: }
101:
102: public void associateConnection(Object connection)
103: throws ResourceException {
104: assert connection != null;
105: assert connection.getClass() == MockConnection.class;
106: MockConnection mockConnection = (MockConnection) connection;
107: checkSecurityConsistency(mockConnection.getSubject(),
108: mockConnection.getConnectionRequestInfo());
109: mockConnection.reassociate(this );
110: connections.add(mockConnection);
111: }
112:
113: public void addConnectionEventListener(
114: ConnectionEventListener listener) {
115: connectionEventListeners.add(listener);
116: }
117:
118: public void removeConnectionEventListener(
119: ConnectionEventListener listener) {
120: connectionEventListeners.remove(listener);
121: }
122:
123: public XAResource getXAResource() throws ResourceException {
124: return mockXAResource;
125: }
126:
127: public LocalTransaction getLocalTransaction()
128: throws ResourceException {
129: return new MockSPILocalTransaction();
130: }
131:
132: public ManagedConnectionMetaData getMetaData()
133: throws ResourceException {
134: return null;
135: }
136:
137: public void setLogWriter(PrintWriter logWriter)
138: throws ResourceException {
139: this .logWriter = logWriter;
140: }
141:
142: public PrintWriter getLogWriter() throws ResourceException {
143: return logWriter;
144: }
145:
146: public Subject getSubject() {
147: return subject;
148: }
149:
150: public MockConnectionRequestInfo getConnectionRequestInfo() {
151: return connectionRequestInfo;
152: }
153:
154: public void removeHandle(MockConnection mockConnection) {
155: connections.remove(mockConnection);
156: }
157:
158: public MockManagedConnectionFactory getManagedConnectionFactory() {
159: return managedConnectionFactory;
160: }
161:
162: public Set getConnections() {
163: return connections;
164: }
165:
166: public List getConnectionEventListeners() {
167: return connectionEventListeners;
168: }
169:
170: public boolean isDestroyed() {
171: return destroyed;
172: }
173:
174: public void closedEvent(MockConnection mockConnection) {
175: ConnectionEvent connectionEvent = new ConnectionEvent(this ,
176: ConnectionEvent.CONNECTION_CLOSED);
177: connectionEvent.setConnectionHandle(mockConnection);
178: for (Iterator iterator = new ArrayList(connectionEventListeners)
179: .iterator(); iterator.hasNext();) {
180: ConnectionEventListener connectionEventListener = (ConnectionEventListener) iterator
181: .next();
182: connectionEventListener.connectionClosed(connectionEvent);
183: }
184: }
185:
186: public void errorEvent(MockConnection mockConnection) {
187: ConnectionEvent connectionEvent = new ConnectionEvent(this ,
188: ConnectionEvent.CONNECTION_ERROR_OCCURRED);
189: connectionEvent.setConnectionHandle(mockConnection);
190: for (Iterator iterator = new ArrayList(connectionEventListeners)
191: .iterator(); iterator.hasNext();) {
192: ConnectionEventListener connectionEventListener = (ConnectionEventListener) iterator
193: .next();
194: connectionEventListener
195: .connectionErrorOccurred(connectionEvent);
196: }
197: }
198:
199: public void localTransactionStartedEvent(
200: MockConnection mockConnection) {
201: ConnectionEvent connectionEvent = new ConnectionEvent(this ,
202: ConnectionEvent.LOCAL_TRANSACTION_STARTED);
203: connectionEvent.setConnectionHandle(mockConnection);
204: for (Iterator iterator = new ArrayList(connectionEventListeners)
205: .iterator(); iterator.hasNext();) {
206: ConnectionEventListener connectionEventListener = (ConnectionEventListener) iterator
207: .next();
208: connectionEventListener
209: .localTransactionStarted(connectionEvent);
210: }
211: }
212:
213: public void localTransactionCommittedEvent(
214: MockConnection mockConnection) {
215: ConnectionEvent connectionEvent = new ConnectionEvent(this ,
216: ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
217: connectionEvent.setConnectionHandle(mockConnection);
218: for (Iterator iterator = new ArrayList(connectionEventListeners)
219: .iterator(); iterator.hasNext();) {
220: ConnectionEventListener connectionEventListener = (ConnectionEventListener) iterator
221: .next();
222: connectionEventListener
223: .localTransactionCommitted(connectionEvent);
224: }
225: }
226:
227: public void localTransactionRolledBackEvent(
228: MockConnection mockConnection) {
229: ConnectionEvent connectionEvent = new ConnectionEvent(this ,
230: ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
231: connectionEvent.setConnectionHandle(mockConnection);
232: for (Iterator iterator = new ArrayList(connectionEventListeners)
233: .iterator(); iterator.hasNext();) {
234: ConnectionEventListener connectionEventListener = (ConnectionEventListener) iterator
235: .next();
236: connectionEventListener
237: .localTransactionRolledback(connectionEvent);
238: }
239: }
240: }
|