KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > mock > MockManagedConnection


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector.mock;
19
20 import java.io.PrintWriter JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.resource.ResourceException JavaDoc;
28 import javax.resource.spi.ConnectionEvent JavaDoc;
29 import javax.resource.spi.ConnectionEventListener JavaDoc;
30 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
31 import javax.resource.spi.LocalTransaction JavaDoc;
32 import javax.resource.spi.ManagedConnection JavaDoc;
33 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
34 import javax.security.auth.Subject JavaDoc;
35 import javax.transaction.xa.XAResource JavaDoc;
36
37 /**
38  *
39  *
40  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
41  *
42  * */

43 public class MockManagedConnection implements ManagedConnection JavaDoc {
44
45     private final MockManagedConnectionFactory managedConnectionFactory;
46     private final MockXAResource mockXAResource;
47     private Subject JavaDoc subject;
48     private MockConnectionRequestInfo connectionRequestInfo;
49
50     private final Set JavaDoc connections = new HashSet JavaDoc();
51     private final List JavaDoc connectionEventListeners = Collections.synchronizedList(new ArrayList JavaDoc());
52
53     private boolean destroyed;
54     private PrintWriter JavaDoc logWriter;
55
56     public MockManagedConnection(MockManagedConnectionFactory managedConnectionFactory, Subject JavaDoc 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 JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc connectionRequestInfo) throws ResourceException JavaDoc {
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 JavaDoc subject, ConnectionRequestInfo JavaDoc 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 JavaDoc {
78         destroyed = true;
79         cleanup();
80     }
81
82     public void cleanup() throws ResourceException JavaDoc {
83         for (Iterator JavaDoc iterator = new HashSet JavaDoc(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 JavaDoc connection) throws ResourceException JavaDoc {
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 JavaDoc listener) {
100         connectionEventListeners.add(listener);
101     }
102
103     public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener) {
104         connectionEventListeners.remove(listener);
105     }
106
107     public XAResource JavaDoc getXAResource() throws ResourceException JavaDoc {
108         return mockXAResource;
109     }
110
111     public LocalTransaction JavaDoc getLocalTransaction() throws ResourceException JavaDoc {
112         return new MockSPILocalTransaction();
113     }
114
115     public ManagedConnectionMetaData JavaDoc getMetaData() throws ResourceException JavaDoc {
116         return null;
117     }
118
119     public void setLogWriter(PrintWriter JavaDoc logWriter) throws ResourceException JavaDoc {
120         this.logWriter = logWriter;
121     }
122
123     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
124         return logWriter;
125     }
126
127     public Subject JavaDoc 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 JavaDoc getConnections() {
144         return connections;
145     }
146
147     public List JavaDoc getConnectionEventListeners() {
148         return connectionEventListeners;
149     }
150
151     public boolean isDestroyed() {
152         return destroyed;
153     }
154
155     public void closedEvent(MockConnection mockConnection) {
156         ConnectionEvent JavaDoc connectionEvent = new ConnectionEvent JavaDoc(this, ConnectionEvent.CONNECTION_CLOSED);
157         connectionEvent.setConnectionHandle(mockConnection);
158         for (Iterator JavaDoc iterator = new ArrayList JavaDoc(connectionEventListeners).iterator(); iterator.hasNext();) {
159             ConnectionEventListener JavaDoc connectionEventListener = (ConnectionEventListener JavaDoc) iterator.next();
160             connectionEventListener.connectionClosed(connectionEvent);
161         }
162     }
163
164     public void errorEvent(MockConnection mockConnection) {
165         ConnectionEvent JavaDoc connectionEvent = new ConnectionEvent JavaDoc(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED);
166         connectionEvent.setConnectionHandle(mockConnection);
167         for (Iterator JavaDoc iterator = new ArrayList JavaDoc(connectionEventListeners).iterator(); iterator.hasNext();) {
168             ConnectionEventListener JavaDoc connectionEventListener = (ConnectionEventListener JavaDoc) iterator.next();
169             connectionEventListener.connectionErrorOccurred(connectionEvent);
170         }
171     }
172
173     public void localTransactionStartedEvent(MockConnection mockConnection) {
174         ConnectionEvent JavaDoc connectionEvent = new ConnectionEvent JavaDoc(this, ConnectionEvent.LOCAL_TRANSACTION_STARTED);
175         connectionEvent.setConnectionHandle(mockConnection);
176         for (Iterator JavaDoc iterator = new ArrayList JavaDoc(connectionEventListeners).iterator(); iterator.hasNext();) {
177             ConnectionEventListener JavaDoc connectionEventListener = (ConnectionEventListener JavaDoc) iterator.next();
178             connectionEventListener.localTransactionStarted(connectionEvent);
179         }
180     }
181
182     public void localTransactionCommittedEvent(MockConnection mockConnection) {
183         ConnectionEvent JavaDoc connectionEvent = new ConnectionEvent JavaDoc(this, ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
184         connectionEvent.setConnectionHandle(mockConnection);
185         for (Iterator JavaDoc iterator = new ArrayList JavaDoc(connectionEventListeners).iterator(); iterator.hasNext();) {
186             ConnectionEventListener JavaDoc connectionEventListener = (ConnectionEventListener JavaDoc) iterator.next();
187             connectionEventListener.localTransactionCommitted(connectionEvent);
188         }
189     }
190
191     public void localTransactionRolledBackEvent(MockConnection mockConnection) {
192         ConnectionEvent JavaDoc connectionEvent = new ConnectionEvent JavaDoc(this, ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
193         connectionEvent.setConnectionHandle(mockConnection);
194         for (Iterator JavaDoc iterator = new ArrayList JavaDoc(connectionEventListeners).iterator(); iterator.hasNext();) {
195             ConnectionEventListener JavaDoc connectionEventListener = (ConnectionEventListener JavaDoc) iterator.next();
196             connectionEventListener.localTransactionRolledback(connectionEvent);
197         }
198     }
199 }
200
Popular Tags